外推时间序列 [英] Extrapolating time series
问题描述
我下载了过去 4 年 Google 的年收入:
I downloaded annual earnings by Google over past 4 years:
library(quantmod)
getFinancials(GOOG)
df<-viewFinancials(GOOG.f, type='IS', period='A',subset = NULL)['Net Income',]
df<-(as.data.frame(df))
以下是数据的显示方式:
Here is how the data is displayed:
2015-12-31 16348
2014-12-31 14136
2013-12-31 12733
2012-12-31 10737
我想将这些数据推断"为未来 10 年的平均线性增长,以这种方式:
I would like to "extrapolate" this data as an averaged linear growth over next 10 years, something in this fashion:
.
在 Excel 中,我只需要粘贴上述数据,从最旧到最新排序,选择它,然后将选择拉伸"超过 10 行,结果如下:
In Excel, all I need to paste the above data, sort from oldest to newest, select it, and "stretch" the selection over 10 additional rows, with this result:
12/31/2012 10737
12/31/2013 12733
12/31/2014 14136
12/31/2015 16348
12/31/2016 18048
12/31/2017 19871
12/31/2018 21695
12/31/2019 23518
12/31/2020 25342
12/31/2021 27166
12/31/2022 28989
12/31/2023 30813
12/31/2024 32636
12/31/2025 34460
我怎样才能在 R 中做同样的事情(或接近它的事情)?
How can I do the same (or something close to it) in R?
推荐答案
在 R 中需要一些额外的步骤.这是您的示例数据:
It takes a few additional steps in R. Here is your sample data:
date<-as.Date(c("2015-12-31", "2014-12-31", "2013-12-31", "2012-12-31"))
value<-c(16348, 14136, 12733, 10737)
假设未来呈线性增长.使用 lm 命令执行线性回归.变量model"存储拟合.
Assuming a linear growth into the future. Use the lm command to perform the linear regression. The variable "model" stores the fit.
#fit linear regression
model<-lm(value~date)
展望未来 10 年,创建未来 10 年的日期序列并存储为数据帧(预测命令所需)
Looking 10 years into the future, create a date sequence for the next 10 years and store as a dataframe (required for the predict command)
#build predict dataframe
dfuture<-data.frame(date=seq(as.Date("2016-12-31"), by="1 year", length.out = 10))
#predict the futurne
predict(model, dfuture, interval = "prediction")
上述模型假设线性增长.如果对增长有不同的预测,那么 lm 公式需要修改或使用 nlm 方程.我将省略有关在可用数据范围之外进行预测的警告.
The above model is assuming linear growth. If there is a different prediction of what the growth would be then lm formula needs modification or use the nlm equation. I will leave out the warnings about making predictions outside the range of available data.
这篇关于外推时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!