I was experimenting a bit with regression through the origin.
lm(Petal.Width ~ Petal.Length -1, iris)
Then I tried to simply subtract the individual values from both Petal.Width
and Petal.Length
from their respective means so that their means are zero.
lm(Petal.Width-mean(Petal.Width) ~ Petal.Length-mean(Petal.Length) -1, iris)
That gives me the following error message:
Error in model.frame.default(formula = Petal.Width - mean(Petal.Width) ~ :
variable lengths differ (found for 'mean(Petal.Length)')
Now I know that I have to use the I()
function to make the code work.
lm(I(Petal.Width-mean(Petal.Width)) ~ I(Petal.Length-mean(Petal.Length)) -1, iris)
Question: But why does the error message say "variable lengths differ"? This by itself didn't really help me to figure out what was going on since the variable lengths are the same.
Since +
and -
don't have their usual meaning in formulas, Petal.Length
and mean(Petal.Length)
are interpreted as two separate variables. Here , Petal.Length
has length 150 but mean(Petal.Length)
has length 1 (the mean collapses it down to a single value). Therefore you get the message about the different variable lengths.
If you really want to dig into it, the error actually comes from model.frame()
, specifically
model.frame.default(Petal.Width-mean(Petal.Width) ~ Petal.Length-mean(Petal.Length), iris[-1])
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments