我如何在R中拟合此线性趋势函数?

问题描述 投票:1回答:1

我想将此线性趋势函数拟合到我的数据:

Y t = a + bt + X t

这是基于时间序列数据。我相信写lm(y ~ time)将返回Y t = a + X t的等效项,但是我很困惑如何将bt包含到R中的此线性趋势函数中。

r time-series linear-regression trend
1个回答
0
投票
library("data.table")
d <- data.table(id = letters)
d <- d[, .(year=1:15), by=id]

fe_y <- data.table(year = 1:15)
fe_y[, trend := sapply(year, function(x) x + runif(1))]
d <- setkey(fe_y, year)[setkey(d, year)]
d[, x1 := runif(390)]
d[, e := rnorm(390, 100)]
d[, y := 3.5*x1 + trend + e  ]

# run the regression
f <- lm(y~ x1 + year, d)
summary(f)

Call:
lm(formula = y ~ x1 + year, data = d)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.62098 -0.67775  0.05316  0.70530  2.77632 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 100.69702    0.13541  743.65   <2e-16 ***
x1            3.34206    0.17379   19.23   <2e-16 ***
year          0.99142    0.01162   85.32   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.9913 on 387 degrees of freedom
Multiple R-squared:  0.9522,    Adjusted R-squared:  0.952 
F-statistic:  3857 on 2 and 387 DF,  p-value: < 2.2e-16
© www.soinside.com 2019 - 2024. All rights reserved.