在因子水平上拟合和预测模型

时间:2017-06-02 07:43:44

标签: r glm predict

我想在因子级别拟合模型,并在飞行中使用那些拟合的模型名称来预测这种匹配因子级别的新数据。我在这个逻辑中没有预测,有人可以考虑下面这个案例吗?

Aa <- data.frame(amount=c(1,2,1,2,1,1,2,2,1,1,1,2,2,2,1), cat1=sample(letters[21:24], 15,rep=TRUE),cat2=sample(letters[11:18], 5,rep=TRUE),
                   card=c("a","b","c","a","c","b","a","c","b","a","b","c","a","c","a"), delay=sample(c(1,1,0,0,0),5,rep=TRUE))

ModelFit<-sapply(as.character(unique(Aa[["card"]])), function(x)glm(delay~amount+cat1+cat2, family = "binomial", data = subset(Aa, card==x)), simplify = FALSE, USE.NAMES = TRUE)

Bb<-Aa[-(which(names(Aa) %in% "delay"))]

sapply(unique(Aa[["card"]]), function(x,y) predict(seq_along(x=ModelFit), newdata=DataOPEN[DataOPEN$SubsidiaryName],type="response"))

1 个答案:

答案 0 :(得分:0)

为了简单起见,我把它变成了一个循环。预测会发出警告,但似乎有效。您未提供DataOPEN数据集,因此我只使用原始Aa(新列pred)计算了预测值。预测的最终舍入版本显示在pred.round列中。

Aa <- data.frame(amount=c(1,2,1,2,1,1,2,2,1,1,1,2,2,2,1), cat1=sample(letters[21:24], 15,rep=TRUE),cat2=sample(letters[11:18], 5,rep=TRUE),
                   card=c("a","b","c","a","c","b","a","c","b","a","b","c","a","c","a"), delay=sample(c(1,1,0,0,0),5,rep=TRUE))

ModelFit <- sapply(as.character(unique(Aa[["card"]])), function(x)glm(delay~amount+cat1+cat2, family = "binomial", data = subset(Aa, card==x)), simplify = FALSE, USE.NAMES = TRUE)

Aa$pred <- NaN # create a new variable for prediction

for(i in levels(Aa$card)){
  newdat <- subset(Aa, subset=card==i)
  newdat$pred <- predict(ModelFit[[i]], newdata=newdat,type="response")
  Aa$pred[match(rownames(newdat), rownames(Aa))] <- newdat$pred
}

Aa$pred.round <- round(Aa$pred) # a rounded prediction
Aa

输出:

> Aa
   amount cat1 cat2 card delay         pred pred.round
1       1    u    p    a     0 1.170226e-09          0
2       2    x    o    b     1 1.000000e+00          1
3       1    x    o    c     0 2.143345e-11          0
4       2    w    m    a     0 1.170226e-09          0
5       1    v    n    c     0 2.143345e-11          0
6       1    x    p    b     0 5.826215e-11          0
7       2    u    o    a     1 5.000000e-01          0
8       2    x    o    c     0 2.143345e-11          0
9       1    w    m    b     0 5.826215e-11          0
10      1    w    n    a     0 1.170226e-09          0
11      1    w    p    b     0 5.826215e-11          0
12      2    w    o    c     1 1.000000e+00          1
13      2    u    o    a     0 5.000000e-01          0
14      2    u    m    c     0 2.143345e-11          0
15      1    w    n    a     0 1.170226e-09          0