【问题标题】:I am predicting population for the next few years, how to I append predicted data to my original data set and then plot the full data set?我正在预测未来几年的人口,如何将预测数据附加到我的原始数据集,然后绘制完整的数据集?
【发布时间】:2020-08-18 23:29:39
【问题描述】:

- 此代码运行良好

 FL.POP = read.csv("C:\\Users\\jevans\\Desktop\\Jessica School\\Florida population 2010-2019.csv", header = TRUE )
    Year <- as.numeric(FL.POP$Year)
    Population <- as.numeric(FL.POP$Population_Estimation)
    model<-lm(Population~Year)
    model
    plot(formula=(Population~Year),data=FL.POP,main='Florida Population')
    abline(reg=lm(formula=(Population~Year),data=FL.POP),col='red')
    summary(FL.POP)

    new.years<- data.frame(Year = c(2019,2020,2021,2022,2023,2024,2025))
    NewData<-predict(model,newdata = new.years)
    NewData
    new.pop<- data.frame(Population = c(NewData))
    new.pop
    new.years

-我无法将新的预测和新年附加到我的原始数据中,以便绘制出来。

【问题讨论】:

  • 你试过rbind.data.frame(old_dataframe, new_dataframe)吗?

标签: r append predict


【解决方案1】:

试试这个:

FL.POP = read.csv("C:\\Users\\jevans\\Desktop\\Jessica School\\Florida population 2010-2019.csv", header = TRUE )

model<-lm(Population_Estimation ~ Year, data = FL.POP)
model
plot(Population_Estimation ~ Year, data=FL.POP, main = 'Florida Population')
abline(reg = model, col='red')

newdata<- data.frame(Year = c(2019,2020,2021,2022,2023,2024,2025))
pred <- predict(model, newdata = newdata)
newdata$Population_Estimation <- pred

FL.POP <- rbind(FL.POP, newdata)

如果您想覆盖 csv 文件以及预测,

write.csv(FL.POP, "C:\\Users\\jevans\\Desktop\\Jessica School\\Florida population 2010-2019.csv")

【讨论】:

  • 我收到以下错误: eval 中的错误(predvars,data,env): numeric 'envir' arg not of length one
  • 更新了答案
猜你喜欢
  • 1970-01-01
  • 2020-02-23
  • 2018-01-02
  • 2013-08-10
  • 2014-01-16
  • 2023-04-01
  • 1970-01-01
  • 2022-02-18
  • 1970-01-01
相关资源
最近更新 更多