繁体   English   中英

R 中动态组的线性回归

[英]Linear regression on dynamic groups in R

我有一个 data.table data_dt我想在其上运行线性回归,以便用户可以使用变量n_col选择G1G2组中的列数。 以下代码运行良好,但由于创建矩阵花费了额外的时间,所以速度很慢。 为了提高下面代码的性能,有没有办法通过调整lm function 的公式来完全删除步骤 1、2 和 3,并且仍然得到相同的结果?

library(timeSeries)
library(data.table)
data_dt = as.data.table(LPP2005REC[, -1])
n_col = 3 # Choose a number from 1 to 3
######### Step 1 ######### Create independent variable
xx <- as.matrix(data_dt[, "SPI"]) 
######### Step 2 ######### Create Group 1 of dependent variables
G1 <- as.matrix(data_dt[, .SD, .SDcols=c(1:n_col + 2)]) 
######### Step 3 ######### Create Group 2 of dependent variables
G2 <- as.matrix(data_dt[, .SD, .SDcols=c(1:n_col + 2 + n_col)]) 
lm(xx ~ G1 + G2)

结果 -

summary(lm(xx ~ G1 + G2))
Call:
lm(formula = xx ~ G1 + G2)

Residuals:
       Min         1Q     Median         3Q        Max 
-3.763e-07 -4.130e-09  3.000e-09  9.840e-09  4.401e-07 

Coefficients:
              Estimate Std. Error    t value Pr(>|t|)    
(Intercept) -4.931e-09  3.038e-09 -1.623e+00   0.1054    
G1LMI       -5.000e-01  4.083e-06 -1.225e+05   <2e-16 ***
G1MPI       -2.000e+00  4.014e-06 -4.982e+05   <2e-16 ***
G1ALT       -1.500e+00  5.556e-06 -2.700e+05   <2e-16 ***
G2LPP25      3.071e-04  1.407e-04  2.184e+00   0.0296 *  
G2LPP40     -5.001e+00  2.360e-04 -2.119e+04   <2e-16 ***
G2LPP60      1.000e+01  8.704e-05  1.149e+05   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 5.762e-08 on 370 degrees of freedom
Multiple R-squared:      1, Adjusted R-squared:      1 
F-statistic: 1.104e+12 on 6 and 370 DF,  p-value: < 2.2e-16

只需使用reformulate公式创建公式,这可能会更容易

out <- lm(reformulate(names(data_dt)[c(1:n_col + 2, 1:n_col + 2 + n_col)], 
     response = 'SPI'), data = data_dt)

-检查

> summary(out)

Call:
lm(formula = reformulate(names(data_dt)[c(1:n_col + 2, 1:n_col + 
    2 + n_col)], response = "SPI"), data = data_dt)

Residuals:
       Min         1Q     Median         3Q        Max 
-3.763e-07 -4.130e-09  3.000e-09  9.840e-09  4.401e-07 

Coefficients:
              Estimate Std. Error    t value Pr(>|t|)    
(Intercept) -4.931e-09  3.038e-09 -1.623e+00   0.1054    
LMI         -5.000e-01  4.083e-06 -1.225e+05   <2e-16 ***
MPI         -2.000e+00  4.014e-06 -4.982e+05   <2e-16 ***
ALT         -1.500e+00  5.556e-06 -2.700e+05   <2e-16 ***
LPP25        3.071e-04  1.407e-04  2.184e+00   0.0296 *  
LPP40       -5.001e+00  2.360e-04 -2.119e+04   <2e-16 ***
LPP60        1.000e+01  8.704e-05  1.149e+05   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 5.762e-08 on 370 degrees of freedom
Multiple R-squared:      1, Adjusted R-squared:      1 
F-statistic: 1.104e+12 on 6 and 370 DF,  p-value: < 2.2e-16

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM