如何针对所有不同的解释变量组合在时间序列数据上的R,Excel/VBA中运行不同的多元线性回归? [英] How to run different multiple linear regressions in R, Excel/VBA on a time series data for all different combinations of Explanatory Variables?

查看:302
本文介绍了如何针对所有不同的解释变量组合在时间序列数据上的R,Excel/VBA中运行不同的多元线性回归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码和R的新手,希望获得您的帮助.为了进行分析,我试图对具有1个因变量(Y)和4个自变量(X1,X2,X3,X4)的时间序列数据进行回归分析.所有这些变量(Y和X)具有4个不同的转换(例如,对于X1-X1,SQRT(X1),Square(X1)和Ln(X1)).我想对Y的所有可能组合(Y,SQRT(Y),Square(Y),Ln(Y))和X值的所有组合进行回归分析,以便最终我可以通过查看R的平方值,选择要在哪个变量中进行变换.

I am new to coding and R and would like your help. For my analysis, I am trying to run regression on a time series data with 1 dependent variable (Y) and 4 Independent Variables (X1, X2, X3, X4). All these variables (Y and X) have 4 different transformations (For example for X1 - X1, SQRT(X1), Square(X1) and Ln(X1)). I want to run the regressions for all the possible combinations of Y (Y, SQRT(Y), Square(Y), Ln(Y)) and all the combinations of X values so that in the end I can decide by looking at the R squared value which variable to choose in which of its transformation.

我目前正在使用R中的代码进行线性回归,并手动更改变量,这会花费很多时间.也许有一个循环或者我可以用于回归的东西?等待您的帮助.谢谢

I am currently using the code in R for linear regression and changing the variables manually which is taking a lot of time. Maybe there is a loop or something I can use for the regressions? Waiting for your kind help. Thanks

lm(Y ~ X1 + X2 + X3 + X4)
lm(SQRT(Y) ~ X1 + X2 + X3 + X4)
lm(Square(Y) ~ X1 + X2 + X3 + X4)
lm(Ln(Y) ~ 1 + X2 + X3 + X4)

lm(Y ~ SQRT(X1) + X2 + X3 + X4)
lm(Y ~ Square(X1) + X2 + X3 + X4)
.... 
lm(ln(Y)~ ln(X1) + ln(X2) + ln(X3) + ln(X4))

这是我的原始代码.

Regression10 <- lm(Final_Data_v2$`10 KW Installations (MW)`~Final_Data_v2$`10 KW Prio Installations (MW)`+Final_Data_v2$`FiT 10 KW (Cent/kWh)`+Final_Data_v2$`Electricity Prices 10 kW Cent/kW`+Final_Data_v2$`PV System Price  (Eur/W)`)
summary(Regression10)
Regressionsqrt10 <- lm(Final_Data_v2$`SQRT(10 KW Installations (MW))`~Final_Data_v2$`10 KW Prio Installations (MW)`+Final_Data_v2$`FiT 10 KW (Cent/kWh)`+Final_Data_v2$`Electricity Prices 10 kW Cent/kW`+Final_Data_v2$`PV System Price  (Eur/W)`)
summary(Regressionsqrt10) 

以此类推.

以下是指向我的数据的链接: LINK

Here is the link to my DATA: LINK

推荐答案

考虑系数的所有组合expand.grid,并使用grep对每个列名称进行过滤.然后调用模型函数,该模型函数使用带有Map(包装到mapply的动态公式)的动态公式来构建N = 1,024项的lm对象列表(等于系数的所有组合).

Consider expand.grid for all combinations of coefficients, filtering on each column name using grep. Then call model function that takes a dynamic formula with Map (wrapper to mapply) to build list of lm objects (equal to all combinations of coefficients) at N=1,024 items.

以下对平方根和平方运行等效多项式运算.注意:grep仅是对实际变量名称的必要调整.

Below runs the equivalent polynomial operations for square root and squared. Note: grep is only adjustment required to actual variable names.

coeffs <- c(names(Final_Data_v2),
            paste0("I(", names(Final_Data_v2), "^(1/2))"),
            paste0("I(", names(Final_Data_v2), "^2)"),
            paste0("log(", names(Final_Data_v2), ")"))         

# BUILD DATA FRAME OF ALL COMBNS OF VARIABLE AND TRANSFORMATION TYPES
all_combns <- expand.grid(y_var = coeffs[grep("10 KW Installations (MW)", coeffs)],
                          x_var1 = coeffs[grep("10 KW Prio Installations (MW)", coeffs)],
                          x_var2 = coeffs[grep("FiT 10 KW (Cent/kWh)", coeffs)],
                          x_var3 = coeffs[grep("Electricity Prices 10 kW Cent/kW", coeffs)],
                          x_var4 = coeffs[grep("PV System Price  (Eur/W)", coeffs)],
                          stringsAsFactors = FALSE)

# FUNCTION WITH DYNAMIC FORMULA TO RECEIVE ALL POLYNOMIAL TYPES
proc_model <- function(y, x1, x2, x3, x4) {
     myformula <- paste0("`",y,"`~`",x1,"`+`",x2,"`+`",x3,"`+`",x4,"`")
     summary(lm(as.formula(myformula), data=Final_Data_v2))
}

# MAP CALL PASSING COLUMN VALUES ELEMENTWISE AS FUNCTION PARAMS
lm_list <- with(all_combns, Map(proc_model, y_var, x_var1, x_var2, x_var3, x_var4))

这篇关于如何针对所有不同的解释变量组合在时间序列数据上的R,Excel/VBA中运行不同的多元线性回归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆