简体   繁体   English

回归 model 与 r 中所有可能的双向交互项

[英]Regression model with all possible two way interaction terms in r

I have a data set with 8 variables.我有一个包含 8 个变量的数据集。 I need all possible two way interaction terms along with the seven predictors in each model.我需要所有可能的双向交互项以及每个 model 中的七个预测变量。 So, in my case there will be total 7C2 = 21 models, each of them containing the 7 predictors and a two way interaction term at a time.因此,在我的例子中,总共将有 7C2 = 21 个模型,每个模型一次包含 7 个预测变量和一个双向交互项。

I have tried to produce the 21 models using for loop but the code seems to fail at the lm() function when I try to use that inside the for loop.我尝试使用 for 循环生成 21 个模型,但是当我尝试在 for 循环中使用该代码时,代码似乎在 lm() function 处失败。 In my problem return is the response variable at the 5-th column of my data.在我的问题中,返回的是我数据第 5 列的响应变量。

colnames(dt) = c("assets","turnover_ratio","SD","sharpe_ratio","return",
                 "expense_ratio","fund_dummy","risk_dummy")
vars=colnames(dt)[-5] 
for (i in vars)  {
  for (j in vars) {
    if (i != j) {
      factor= paste(i,j,sep='*')}
    lm.fit <- lm(paste("return ~", factor), data=dt)
    print(summary(lm.fit))
  }}

The error message is given below for the code:下面给出了代码的错误消息:

Error in paste("return ~", factor): cannot coerce type 'closure' to vector of type 'character'粘贴错误(“return ~”,因子):无法将“闭包”类型强制转换为“字符”类型的向量

This is my data set:这是我的数据集:数据集

The output below should be the desired output and 20 more such models are needed with other possible two way interaction terms.下面的 output 应该是所需的 output 并且需要另外 20 个这样的模型以及其他可能的双向交互项。 All the 7 predictors should be present in each model.每个 model 中都应存在所有 7 个预测变量。 The only thing that should change is the two way interaction term.唯一应该改变的是双向交互项。

This is my desired output among the 21 required:这是所需的 21 个中我想要的 output:21 个所需输出中的一个所需输出

Your problem is the end of the if statement.您的问题是 if 语句的结尾。 This code should work:此代码应该可以工作:

colnames(dt) = c("assets","turnover_ratio","SD","sharpe_ratio","return",
                 "expense_ratio","fund_dummy","risk_dummy")
vars=colnames(dt)[-5] 
for (i in vars)  {
  for (j in vars) {
    if (i != j) {
      factor= paste(i,j,sep='*')
      lm.fit <- lm(paste0("return ~", factor), data=dt)
      print(summary(lm.fit))
    }
  }
}

The problem was that for the first iteration the variable factor was not define.问题是对于第一次迭代,变量因子没有定义。 Also try not to name a variable factor, since factor is a function in R.也尽量不要命名变量因子,因为因子是 R 中的 function。

The following apply loop gets all pairwise interactions between the 7 variables.以下apply循环获取 7 个变量之间的所有成对交互。 The 21 pairs are first obtained with combn .首先使用combn获得 21 对。

vars <- colnames(dt)[-5] 
resp <- colnames(dt)[5] 

cmb <- combn(vars, 2)

lm_list <- apply(cmb, 2, function(regrs){
  inter_regrs <- paste(regrs, collapse = "*")
  other_regrs <- setdiff(vars, regrs)
  all_regrs <- paste(other_regrs, collapse = "+")
  all_regrs <- paste(all_regrs, inter_regrs, sep = "+")
  fmla <- as.formula(paste(resp, all_regrs, sep = "~"))
  lm(fmla, data = dt)
})

lapply(lm_list, summary)

Data creation code.数据创建代码。

set.seed(1234)
dt <- replicate(8, rnorm(100))
dt <- as.data.frame(dt)

colnames(dt) <- c("assets","turnover_ratio","SD",
              "sharpe_ratio","return","expense_ratio",
              "fund_dummy","risk_dummy")

I think this should work and allow you to get rid of the loops:我认为这应该可以让你摆脱循环:

lm.fit = lm(return ~ (.)^2, data=dt)

暂无
暂无

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

相关问题 如何在 R 的线性模型中包含所有可能的双向交互项? - How to include all possible two-way interaction terms in a linear model in R? R中的轨道回归模型中的交互作用项和随机效应 - Interaction terms and random effects in tobit regression model in R 如何 plot 与 R 中的逻辑回归 model 中的两个分类预测变量进行双向交互? - How to plot a two-way interaction with two categorical predictors from a logistic regression model in R? 线性回归 model 和交互项问题 - Linear regression model and interaction's terms problem 在 R 中使用交互项进行线性回归预测 - Linear regression prediction using interaction terms in R Linear Model:计算所有可能的变量组合与交互:交互项必须在主效应中 - Linear Model: Calculate all possible combinations of variables with interaction: interaction terms must be in main effects 如何处理 R 中线性回归的 output 中的 NA,特别是针对三向交互项的 NA? - How to deal with NA in the output of the linear regression in R, specifically NA for the three-way interaction terms? 具有二元接触和相互作用项的广义线性回归模型 - Generalized Linear Regression Model With Binary Exposure and Interaction Terms R 具有交互项的线性回归的平均偏效应公式 - R formula for average partial effect of linear regression with interaction terms 在regsubsets()或其他函数中,具有较高交互作用项并包含主要影响的R中的详尽模型选择是否可行? - Is exhaustive model selection in R with high interaction terms and inclusion of main effects possible with regsubsets() or other functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM