【问题标题】:How would I fit this linear trend function in R?我如何在 R 中拟合这个线性趋势函数?
【发布时间】:2020-04-17 14:35:22
【问题描述】:

我想将这个线性趋势函数拟合到我的数据中:

Yt=a+bt+Xt

这是基于时间序列数据。 我相信写lm(y ~ time) 将返回相当于 Yt=a+Xt 但我很困惑如何将 bt 包含到 R 中的这个线性趋势函数中。

【问题讨论】:

    标签: r time-series linear-regression trend


    【解决方案1】:

    您可以简单地将其作为解释变量包含在内

    library(data.table)
    d <- data.table(id = 1)
    d <- d[, .(year=1:200), by=id]
    d[, x1 := runif(200)]
    
    # add an erros
    d[, e := rnorm(200, 23, 7)]
    
    # add the dependent variable
    d[, y := 3.5*x1 + 0.5*year + e  ]
    
    m <- lm(y ~ x1 + year, d)
    summary(m)
    
    Call:
    lm(formula = y ~ x1 + year, data = d)
    
    Residuals:
         Min       1Q   Median       3Q      Max 
    -19.2008  -4.4356   0.3986   5.2283  16.6819 
    
    Coefficients:
                 Estimate Std. Error t value Pr(>|t|)    
    (Intercept) 20.064776   1.519766  13.203   <2e-16 ***
    x1           3.114048   1.914318   1.627    0.105    
    year         0.523195   0.009187  56.947   <2e-16 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    Residual standard error: 7.469 on 197 degrees of freedom
    Multiple R-squared:  0.943, Adjusted R-squared:  0.9424 
    F-statistic:  1628 on 2 and 197 DF,  p-value: < 2.2e-16
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 2021-06-17
      • 2013-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-05
      相关资源
      最近更新 更多