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 that. Here is my sample code: #generating data 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) #write formula inside lm--works fine res1 <- lapply(levels(df$group), function(x) lm(y~x1,df, subset = group ==x)) res1 res2 <- lapply(levels(df$group),function(x) lm(y~x1+x2,df, subset group ==x)) res2 #try to pass formula into lm()--does not work formula1 <- as.formula(y~x1) formula2 <- as.formula(y~x1+x2) resf1 <- lapply(levels(df$group),function(x) lm(formula1,df, subset group ==x)) resf1 resf2 <- lapply(levels(df$group),function(x) lm(formula2,df, subset group ==x)) Resf2 The error message is 'Error in eval(expr, envir, enclos): object "x" not found' Any help is greatly appreciated! Yan -------------------------------------------------------- This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
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) <Yan.Y.Li@morganstanley.com> wrote:> > 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 that. Here is my sample code: > > #generating data > 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) > > #write formula inside lm--works fine > res1 <- lapply(levels(df$group), function(x) lm(y~x1,df, subset = group > ==x)) > res1 > res2 <- lapply(levels(df$group),function(x) lm(y~x1+x2,df, subset > group ==x)) > res2 > > #try to pass formula into lm()--does not work > formula1 <- as.formula(y~x1) > formula2 <- as.formula(y~x1+x2) > resf1 <- lapply(levels(df$group),function(x) lm(formula1,df, subset > group ==x)) > resf1 > resf2 <- lapply(levels(df$group),function(x) lm(formula2,df, subset > group ==x)) > Resf2 > > The error message is > 'Error in eval(expr, envir, enclos): object "x" not found' > > Any help is greatly appreciated! > > Yan > -------------------------------------------------------- > > This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
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))) formula1 <- as.formula(y ~ x1) lapply(levels(DF$group), function(x) { environment(formula1) <- environment() lm(formula1, DF, subset = group == x) }) formula2 <- as.formula(y ~ x1 + x2) lapply(levels(DF$group), function(x) { environment(formula2) <- environment() lm(formula2, DF, subset = group == x) }) On 8/15/07, Li, Yan (IED) <Yan.Y.Li at morganstanley.com> wrote:> 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 that. Here is my sample code: > > #generating data > 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) > > #write formula inside lm--works fine > res1 <- lapply(levels(df$group), function(x) lm(y~x1,df, subset = group > ==x)) > res1 > res2 <- lapply(levels(df$group),function(x) lm(y~x1+x2,df, subset > group ==x)) > res2 > > #try to pass formula into lm()--does not work > formula1 <- as.formula(y~x1) > formula2 <- as.formula(y~x1+x2) > resf1 <- lapply(levels(df$group),function(x) lm(formula1,df, subset > group ==x)) > resf1 > resf2 <- lapply(levels(df$group),function(x) lm(formula2,df, subset > group ==x)) > Resf2 > > The error message is > 'Error in eval(expr, envir, enclos): object "x" not found' > > Any help is greatly appreciated! > > Yan > -------------------------------------------------------- > > This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
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)}) # put the formula defn into function(x) > resf1[[1]] Call: lm(formula = formula1, data = df, subset = group == x) Coefficients: (Intercept) x1 0.8532 0.1189 [[2]] Call: lm(formula = formula1, data = df, subset = group == x) Coefficients: (Intercept) x1 0.7116 0.3398 HTH, Weiwei On 8/15/07, Li, Yan (IED) <Yan.Y.Li at morganstanley.com> wrote:> 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 that. Here is my sample code: > > #generating data > 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) > > #write formula inside lm--works fine > res1 <- lapply(levels(df$group), function(x) lm(y~x1,df, subset = group > ==x)) > res1 > res2 <- lapply(levels(df$group),function(x) lm(y~x1+x2,df, subset > group ==x)) > res2 > > #try to pass formula into lm()--does not work > formula1 <- as.formula(y~x1) > formula2 <- as.formula(y~x1+x2) > resf1 <- lapply(levels(df$group),function(x) lm(formula1,df, subset > group ==x)) > resf1 > resf2 <- lapply(levels(df$group),function(x) lm(formula2,df, subset > group ==x)) > Resf2 > > The error message is > 'Error in eval(expr, envir, enclos): object "x" not found' > > Any help is greatly appreciated! > > Yan > -------------------------------------------------------- > > This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III