[R] Formula in lm inside lapply

2007-08-15 Thread Li, Yan \(IED\)
I am trying to run separate regressions for different groups of observations using the lapply function. It works fine when I write the formula inside the lm() function. But I would like to pass formulae into lm(), so I can do multiple models more easily. I got an error message when I tried to do

Re: [R] Formula in lm inside lapply

2007-08-15 Thread Henrique Dallazuanna
try this: lapply(levels(df$group), function(x)lm(formula1, data=df[group==x,])) -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O On 15/08/07, Li, Yan (IED) [EMAIL PROTECTED] wrote: I am trying to run separate regressions for different groups of observations using the

Re: [R] Formula in lm inside lapply

2007-08-15 Thread Gabor Grothendieck
It can't find x since the environment of formula1 and of formula2 is the Global Environment and x is not there -- its local to the function. Try this: #generating data set.seed(1) DF - data.frame(y = rnorm(100, 1), x1 = rnorm(100, 1), x2 = rnorm(100, 1), group = rep(c(A, B), c(40, 60)))

Re: [R] Formula in lm inside lapply

2007-08-15 Thread Weiwei Shi
try this: x = predict(z, Iris[-train, ]) x1 - rnorm(100,1) x2 - rnorm(100,1) y - rnorm(100,1) group - rep(c(A,B),c(40,60)) group - factor(group) df - data.frame(y,x1,x2,group) resf1 - lapply(levels(df$group),function(x) {formula1 - as.formula(y~x1); lm(formula1,df, subset =group ==x)})

Re: [R] Formula in lm inside lapply

2007-08-15 Thread Gabor Grothendieck
Here is another solution that gets around the non-standard way that subset= is handled in lm. It has the advantage that unlike the previous solution where formula1 and group == x appear literally in the output, in this one the formula appears written out and group == A and group == B appear:

Re: [R] formula and lm

2004-07-16 Thread Petr Pikal
Hallo On 15 Jul 2004 at 13:46, [EMAIL PROTECTED] wrote: Hi, don' t understand why the function fomula have this error, i enclose the parameter a with the function I() Thank Ruben x-1:5 y-c( 2 ,4 , 6 , 8 ,11) formu-y~I(a*x) form-formula(formu) dummy-data.frame(x=x,y=y)

[R] formula and lm

2004-07-15 Thread solares
Hi, don' t understand why the function fomula have this error, i enclose the parameter a with the function I() Thank Ruben x-1:5 y-c( 2 ,4 , 6 , 8 ,11) formu-y~I(a*x) form-formula(formu) dummy-data.frame(x=x,y=y) fm-lm(form,data=dummy) Error in unique(c(AsIs, oldClass(x))) : Object a not found

Re: [R] formula and lm

2004-07-15 Thread Spencer Graves
It wants to compute a*x given a and x, and then use ordinarly least squares to estimate b0 and b1 in y = b0 + b1*I(a*x). If that is what you intend, you must supply a. If you want to estimate a, e.g., with no constant, use y~x-1. Does this answer the question? hope this helps. spencer