18

我有我定期对其进行回归的数据。每个“块”数据都适合不同的回归。例如,每个状态可能有不同的函数来解释依赖值。这似乎是一个典型的“拆分应用组合”类型的问题,所以我使用的是 plyr 包。我可以轻松地创建一个lm()运行良好的对象列表。但是,我不能完全理解以后如何使用这些对象来预测单独 data.frame 中的值。

这是一个完全人为的示例,说明了我正在尝试做的事情:

# setting up some fake data
set.seed(1)
funct <- function(myState, myYear){
   rnorm(1, 100, 500) +  myState + (100 * myYear) 
}
state <- 50:60
year <- 10:40
myData <- expand.grid( year, state)
names(myData) <- c("year","state")
myData$value <- apply(myData, 1, function(x) funct(x[2], x[1]))
## ok, done with the fake data generation. 

require(plyr)

modelList <- dlply(myData, "state", function(x) lm(value ~ year, data=x))
## if you want to see the summaries of the lm() do this:  
    # lapply(modelList, summary)

state <- 50:60
year <- 50:60
newData <- expand.grid( year, state)
names(newData) <- c("year","state") 
## now how do I predict the values for newData$value 
   # using the regressions in modelList? 

那么如何使用其中lm()包含的对象modelList来预测使用年份和状态独立值的值newData

4

6 回答 6

9

这是我的尝试:

predNaughty <- ddply(newData, "state", transform,
  value=predict(modelList[[paste(piece$state[1])]], newdata=piece))
head(predNaughty)
#   year state    value
# 1   50    50 5176.326
# 2   51    50 5274.907
# 3   52    50 5373.487
# 4   53    50 5472.068
# 5   54    50 5570.649
# 6   55    50 5669.229
predDiggsApproved <- ddply(newData, "state", function(x)
  transform(x, value=predict(modelList[[paste(x$state[1])]], newdata=x)))
head(predDiggsApproved)
#   year state    value
# 1   50    50 5176.326
# 2   51    50 5274.907
# 3   52    50 5373.487
# 4   53    50 5472.068
# 5   54    50 5570.649
# 6   55    50 5669.229

JD龙编辑

我受到了足够的启发,想出了一个adply()选择:

pred3 <- adply(newData, 1,  function(x)
    predict(modelList[[paste(x$state)]], newdata=x))
head(pred3)
#   year state        1
# 1   50    50 5176.326
# 2   51    50 5274.907
# 3   52    50 5373.487
# 4   53    50 5472.068
# 5   54    50 5570.649
# 6   55    50 5669.229
于 2011-12-13T23:10:23.917 回答
6

仅使用 R 的解决方案base。输出的格式不同,但所有值都在那里。

models <- lapply(split(myData, myData$state), 'lm', formula = value ~ year)
pred4  <- mapply('predict', models, split(newData, newData$state))
于 2011-12-14T04:43:54.150 回答
6

您需要使用mdply为每个函数调用提供模型和数据:

dataList <- dlply(newData, "state")

preds <- mdply(cbind(mod = modelList, df = dataList), function(mod, df) {
  mutate(df, pred = predict(mod, newdata = df))
})
于 2011-12-16T12:56:45.910 回答
4

出什么问题了

lapply(modelList, predict, newData)

?

编辑:

感谢您解释这有什么问题。怎么样:

newData <- data.frame(year)
ldply(modelList, function(model) {
  data.frame(newData, predict=predict(model, newData))
})

迭代模型,并应用新数据(这对于每个状态都是相同的,因为您刚刚expand.grid创建了它)。

编辑2:

如果newData没有与示例中year的每个相同的值,state则可以使用更通用的方法。请注意,这使用 的原始定义newData,而不是第一次编辑中的定义。

ldply(state, function(s) {
  nd <- newData[newData$state==s,]
  data.frame(nd, predict=predict(modelList[[as.character(s)]], nd))
})

此输出的前 15 行:

   year state  predict
1    50    50 5176.326
2    51    50 5274.907
3    52    50 5373.487
4    53    50 5472.068
5    54    50 5570.649
6    55    50 5669.229
7    56    50 5767.810
8    57    50 5866.390
9    58    50 5964.971
10   59    50 6063.551
11   60    50 6162.132
12   50    51 5514.825
13   51    51 5626.160
14   52    51 5737.496
15   53    51 5848.832
于 2011-12-13T22:43:22.913 回答
2

我认为困难的部分是将每个状态newData与相应的模型相匹配。

大概是这样的?

predList <- dlply(newData, "state", function(x) {
  predict(modelList[[as.character(min(x$state))]], x) 
})

这里我使用了一种“hacky”的方式来提取相应的状态模型:as.character(min(x$state))

...可能有更好的方法?

输出:

> predList[1:2]
$`50`
       1        2        3        4        5        6        7        8        9       10       11 
5176.326 5274.907 5373.487 5472.068 5570.649 5669.229 5767.810 5866.390 5964.971 6063.551 6162.132 

$`51`
      12       13       14       15       16       17       18       19       20       21       22 
5514.825 5626.160 5737.496 5848.832 5960.167 6071.503 6182.838 6294.174 6405.510 6516.845 6628.181

或者,如果你想要一个data.frame作为输出:

predData <- ddply(newData, "state", function(x) {
  y <-predict(modelList[[as.character(min(x$state))]], x)
  data.frame(id=names(y), value=c(y))
})

输出:

head(predData)
  state id    value
1    50  1 5176.326
2    50  2 5274.907
3    50  3 5373.487
4    50  4 5472.068
5    50  5 5570.649
6    50  6 5669.229
于 2011-12-13T22:48:33.473 回答
1

也许我遗漏了一些东西,但我相信lmList这里是理想的工具,

library(nlme)
ll = lmList(value ~ year | state, data=myData)
predict(ll, newData)


## Or, to show that it produces the same results as the other proposed methods...
newData[["value"]] <- predict(ll, newData)
head(newData)
#   year state    value
# 1   50    50 5176.326
# 2   51    50 5274.907
# 3   52    50 5373.487
# 4   53    50 5472.068
# 5   54    50 5570.649
# 6   55    50 5669.229
于 2014-07-23T09:53:15.943 回答