lm拟合data.frame的多个子集

时间:2012-06-18 05:08:12

标签: r plyr lm reshape2

我正在运行一些模拟,我希望将线性模型拟合到我的数据子集中:

library(reshape2)
library(plyr)

all <- mutate(iris, mean_width = ave(Petal.Width, Petal.Length))
str(all)

## want to minimise sum(|y*polynomial(x) - z|^2) for each id
## in the region where x != exclude

weighted_difference <- function(d, n=4, exclude = c(2.5, 3), ...){

  sub <- subset(d, !(Sepal.Width > exclude[1] &
                     Sepal.Width < exclude[2]))
  fit <-  lm(mean_width ~ I(poly(Petal.Length, n, raw=TRUE)*Petal.Width) + Petal.Width - 1, data = sub)
  mutate(d, predict = predict(fit, d),
         difference = Petal.Width - predict )
}

results <- ddply(all, "Species", weighted_difference)

这有效,但我想使用一种更简单的方法,我首先为拟合创建一个新的data.frame,

  exclude <- c(3, 6)
  sub <- subset(all, !(x > exclude[1] & x < exclude[2]))

适合所有情况,

 fits <- lm(z ~ I(poly(x, n, raw=TRUE)*y) + y - 1 | id, data = sub)

(这... | id显然是无效的语法)

并立即对完整数据使用预测,

all <- mutate(all, predict = predict(fits, all), difference = y - predict )

是否有一些使用lm()这样的技巧?还是更好的解决方案?感谢。

1 个答案:

答案 0 :(得分:2)

lmList(来自nlme)是否符合您的要求?

library(nlme)
fits <- lmList(z ~ I(poly(x, n, raw=TRUE)*y) + y - 1 | id, data = sub)