为数据框的子集创建预测并附加到原始文件

时间:2017-07-27 05:55:47

标签: r loops dataframe append apply

我使用的是R 3.3.2。

我想根据前几年的分数预测各种分类机构的分数。然后我需要将这些预测分数作为新行添加到原始数据帧。我的输入是一个csv文件

我想使用最小二乘线性模型,发现" lm"和"预测"完全符合我的需要。

我知道这是一个非常初学的问题,但希望有人可以帮助我。请参阅下面的数据和代码,我已经开始使用两种解决方案。

score<-c(63.6,  60.3,   60.4,   53.4,   46.5,   65.8,   45.8,   65.9,   
44.9,   60, 83.5,   81.7,   81.2,   78.8,   83.3,   79.4,   83.2,   77.3,   
79.4)

year<-c(2013,   2014,   2015,   2016,   2014,   2014,   2015,   2015,   
2016,   2016,   2011,   2012,   2013,   2014,   2014,   2015,   2015,   
2016,   2016)

institution<-c(1422,    1422,   1422,   1422,   1384,   1422,   1384,   
1422,   1384,   1422,   1384,   1384,   1384,   1422,   1384,   1422,   
1384,   1422,   1384)

subranking<-c('CMP',    'CMP',  'CMP',  'CMP',  'SSC',  'SSC',  'SSC',  
'SSC',  'SSC',  'SSC',  'ETC',  'ETC',  'ETC',  'ETC',  'ETC',  'ETC',  
'ETC',  'ETC',  'ETC')

d <- data.frame(score, year, institution,subranking)


#-----------SOLUTION 1 -------------------

p<- unique(d$institution)
for (i in (1:length(p))){
  x<- d$score[d$institution==p[i]]
  y<- d$year[d$institution==p[i]]
  model<- lm(x~y)
  result<-predict(model, data.frame(y = c(2017,2018,2019,2020)))
  z<- cbind(result,data.frame(y = c(2017,2018,2019,2020)))
  print(z)
}

##----------SOLUTION 2 -------------------

calculate_predicted_scores <- function(scores, years) {predicted_scores <-0
mod = lm(scores ~ years)
predicted_scores<-predict(mod, data.frame(years = c(2017,2018,2019,2020)))
return(predicted_scores)
}

为了说明这一点,我想在最后得到 - 黄色行是预测:

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以按照这个非常有用的answer

中的说明尝试使用扫帚进行dplyr
library(dplyr)
library(broom)
pred_per_group = d %>% group_by(subranking, institution) %>%
  do(predicted_scores=predict(lm(score ~ year, data=.), data.frame(year = c(2017,2018,2019, 2020))))
pred_df = tidy(pred_per_group, predicted_scores)

然后,使用rbind将带有谓词的结果数据框添加到您的。

pred_df <- data.frame(score=pred_df$x, year=rep(c(2017,2018,2019,2020), 5), institution=pred_df$institution, subranking=pred_df$subranking)
result <- rbind(d, pred_df)

8月3日编辑:因为你想结束自己对编码的追求,我会按如下方式进行:

p<- unique(d$institution)
r <- unique(d$subranking)
for (i in (1:length(p))){
  for(j in seq_along(r)){
  score<- d$score[d$institution==p[i] & d$subranking==r[j]]
  year<- d$year[d$institution==p[i] & d$subranking==r[j]]
  if(length(score)== 0){
    print(sprintf("No level for the following combination: Institution: %s and Subrank: %s", p[i], r[j]))
  } else{
  model<- lm(score~year)
  result<-predict(model, data.frame(year = c(2017,2018,2019,2020)))
  z<- cbind(result,data.frame(year = c(2017,2018,2019,2020)))
  print(sprintf("For Institution: %s and Subrank: %s the Score is:",p[i], r[j]))
  print(z)
  }
  }
}

[1] "For Institution: 1422 and Subrank: CMP the Score is:"
  result year
1  51.80 2017
2  48.75 2018
3  45.70 2019
4  42.65 2020
[1] "For Institution: 1422 and Subrank: SSC the Score is:"
  result year
1   58.1 2017
2   55.2 2018
3   52.3 2019
4   49.4 2020
[1] "For Institution: 1422 and Subrank: ETC the Score is:"
  result year
1  77.00 2017
2  76.25 2018
3  75.50 2019
4  74.75 2020
[1] "No level for the following combination: Institution: 1384 and Subrank: CMP"
[1] "For Institution: 1384 and Subrank: SSC the Score is:"
    result year
1 44.13333 2017
2 43.33333 2018
3 42.53333 2019
4 41.73333 2020
[1] "For Institution: 1384 and Subrank: ETC the Score is:"
     result year
1 80.66000 2017
2 80.26286 2018
3 79.86571 2019
4 79.46857 2020