I recently ran into the following:> x <- c(1,2,4,3) > lm(x ~ 1:length(x))Error in model.frame(formula, rownames, variables, varnames, extras, extranames, : variable lengths differ but> lm(x ~ c(1:length(x)))Call: lm(formula = x ~ c(1:length(x))) Coefficients: (Intercept) c(1:length(x)) 0.5 0.8 and> identical(1:4, c(1:4))[1] TRUE Should I report this as a bug? G?ran -- G?ran Brostr?m tel: +46 90 786 5223 Department of Statistics fax: +46 90 786 6614 Ume? University http://www.stat.umu.se/egna/gb/ SE-90187 Ume?, Sweden e-mail: gb at stat.umu.se
G?ran Brostr?m wrote:> I recently ran into the following: > > >>x <- c(1,2,4,3) >>lm(x ~ 1:length(x)) > > Error in model.frame(formula, rownames, variables, varnames, extras, extranames, : > variable lengths differ > > but > > >>lm(x ~ c(1:length(x))) > > > Call: > lm(formula = x ~ c(1:length(x))) > > Coefficients: > (Intercept) c(1:length(x)) > 0.5 0.8 > > and > > >>identical(1:4, c(1:4)) > > [1] TRUE > > Should I report this as a bug?NO!!! The ":" operator is used to model interactions in lm() and friends! Use lm(x ~ I(1:length(x))) in order to perform the analysis above. Uwe Ligges> G?ran
On Wed, 2003-12-10 at 10:34, G?ran Brostr?m wrote:> I recently ran into the following: > > > x <- c(1,2,4,3) > > lm(x ~ 1:length(x)) > Error in model.frame(formula, rownames, variables, varnames, extras, extranames, : > variable lengths differ > > but > > > lm(x ~ c(1:length(x))) > > Call: > lm(formula = x ~ c(1:length(x))) > > Coefficients: > (Intercept) c(1:length(x)) > 0.5 0.8 > > and > > > identical(1:4, c(1:4)) > [1] TRUE > > Should I report this as a bug?No. In a formula ":" is interpreted differently. It is used to represent interactions. See help(formula). It is probably good practice to generate variables outside of the formula and then refer to them by name. Martyn
On 0, Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote:> G?ran Brostr?m wrote: > >I recently ran into the following: > > > > > >>x <- c(1,2,4,3) > >>lm(x ~ 1:length(x)) > > > >Error in model.frame(formula, rownames, variables, varnames, extras, > >extranames, : variable lengths differ > > > >but > > > > > >>lm(x ~ c(1:length(x))) > > > > > >Call: > >lm(formula = x ~ c(1:length(x))) > > > >Coefficients: > > (Intercept) c(1:length(x)) > > 0.5 0.8 > > > >and > > > > > >>identical(1:4, c(1:4)) > > > >[1] TRUE > > > >Should I report this as a bug? > > NO!!! > The ":" operator is used to model interactions in lm() and friends!I forgot that! Thanks to all who remembered. G?ran