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
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
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)))
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)})
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:
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)
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
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