繁体   English   中英

R-如何在回归模型中旋转/互换预测变量(非逐步方法)

[英]R - How to rotate/interchange predictors in a regression model (a not-stepwise approach)

给定的数据集具有一系列的预测变量,应在简单(尽管是多元变量)回归模型中一一使用。 我无法掌握是否需要循环通过(预测变量的名称)或类似lapply()变量。

创建函数在创建输出之前需要一个参数,但是我不知道如何在给定的模型公式中合并for循环。

Some data
---
df <- data.frame(y1=runif(100,1,10),
    y2=runif(100,1,10),
    x1= runif(100,1,5),
    x2= runif(100,1,5), 
    x3= runif(100,1,5))

Y = cbind( df$y1 , df$y2 )

我觉得这有点像:

list_pred <- for ( x in 1:colnames(pred)) {
  print(paste(x))
}

但是for循环并不是真的想要一起工作。 因此,我认为我可能必须创建一个也包含lm()参数的函数。

not_stepwise <- matrix( 0 , predictor , 1 ) # pre-allocation?
for (x in 1:predictor) {
 lm.dd <- lm( Y ~ [x] , data = df ] )
}

但是到现在为止,我不确定在哪里寻找,Google或StackOverflow对此都有一些广泛的信息(统计意义除外,但我已经涵盖了)。

更新 :为澄清起见,我正在寻找模型本身(和/或信号预测器)的R²值的概述,以确定该模型是否甚至具有重要的预测器,例如有意义的模型。

更新2 :我的数据集的外观(没有DV)

'data.frame':   100 obs. of  35 variables:
 $ Minuten             : int  72 30 102 212 37 57 120 146 143 189 ...
 $ Teamsize            : int  3 3 4 3 2 4 5 6 5 3 ...
 $ Exp                 : num  6.67 6.67 5.5 5.33 10.5 ...
 $ Chirurg1            : int  10 10 1 2 4 2 3 3 2 9 ...
 $ Chirurg2            : int  11 11 2 NA NA NA NA NA 9 2 ...
 $ NG                  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ NG.Ratio            : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Specialisme         : int  2 2 1 3 1 3 1 1 3 3 ...
 $ Observaties         : int  43 21 55 132 22 90 90 64 100 129 ...
 $ UniqueCom           : int  9 6 9 12 4 9 9 12 16 12 ...
 $ G.Ratio             : num  0.333 0.667 0.25 0.667 0.5 ...
 $ Bewustwording       : int  1 0 1 1 0 0 0 0 1 1 ...
 $ Confrontatie        : int  0 1 0 2 0 0 0 1 1 2 ...
 $ Confrontatie.Outside: int  0 0 0 0 0 0 0 0 0 0 ...
 $ Coordinerend        : int  1 3 6 17 2 4 10 6 14 9 ...
 $ Delegerend          : int  6 3 2 22 0 9 6 1 15 11 ...
 $ Goedaardig          : int  3 0 5 4 0 7 3 2 9 1 ...
 $ Grappig             : int  0 1 0 0 0 2 0 1 1 1 ...
 $ Hofmaken            : int  0 0 0 0 0 1 1 2 1 0 ...
 $ Instruerend         : int  9 0 7 13 0 7 3 9 7 13 ...
 $ Onderwijzend        : int  6 5 3 21 9 2 14 5 8 22 ...
 $ Ontbindend          : int  1 1 0 0 1 0 1 1 2 1 ...
 $ Protest             : int  0 0 0 0 0 0 0 0 1 0 ...
 $ Reactief            : int  0 0 0 0 0 0 0 0 1 0 ...
 $ Respons.Negatief    : int  0 0 1 1 0 0 1 1 0 0 ...
 $ Respons.Neutraal    : int  0 0 0 0 0 0 0 0 0 2 ...
 $ Respons.Positief    : int  1 0 1 2 1 1 0 1 2 8 ...
 $ Sign.out            : int  1 0 1 1 0 1 0 1 1 0 ...
 $ Time.out            : int  0 0 0 1 0 0 0 0 0 0 ...
 $ Volgzaam            : int  0 0 0 0 0 0 0 0 1 0 ...
 $ Vragend             : int  0 0 0 3 0 0 1 0 1 1 ...
 $ rank_sum            : int  27 11 24 80 12 33 37 25 58 65 ...
 $ rank_sum.60s        : num  0.375 0.367 0.235 0.377 0.324 ...
 $ ranking             : int  43 56 46 11 55 37 35 45 21 17 ...
 $ ranking.60s         : int  30 34 72 29 49 1 58 92 21 41 ...

第一个简单的解决方案

# Generate a dataset
X <- data.frame(matrix(runif(1000), ncol=20))
y <- rnorm(nrow(X))
dts <- data.frame(y, X)

lms <- vector(ncol(X), mode="list")
k <- 1
for (x in names(X)) {
   # Create formula with the k-th x variabile
   frml <- as.formula(paste0("y ~", x))
   # Use the formula in a linear model
   lms[[k]] <- lm(frml, data=dts)
   k <- k+1
}
# This is the output of the linear model with the 15-th x variable
summary(lms[[15]])
# A matrix with R-squared and adjusted R-squared
r2 <- function(x) c(summary(x)$r.squared, summary(x)$adj.r.squared)
t(sapply(lms, r2))

更加优雅和灵活的解决方案

R2 <- function(x, data) {
     frml <- as.formula(paste0("y ~", paste(unlist(x), collapse="+"))) 
     lmfit <- lm(frml, data=data)
     lmsum <- summary(lmfit)
     data.frame(R2=lmsum$r.squared, adj.R2=lmsum$adj.r.squared)
}
R2 <- Vectorize(R2, "x")

# The R-squared for all the univariate models
R2(names(X), dts)

# The R-squared for all the bivariate models 
k <- 2   
xcouples <- apply(combn(names(X), k), 2, list)
names(xcouples) <- lapply(xcouples, function(x) paste(unlist(x), collapse="_"))
t(R2(xcouples, dts))

要查找数据集中所有可能的回归输出(包括多个组合),以下代码可能会有所帮助。

# To find all combinations of the predictors.

predictors <- names(df)[-1]
all_comb <- sapply(seq(predictors) ,function(i) {t(combn(predictors,i))})


# Calculating the regression outputs and putting into a list called result.

result <- list()

    for(x in 1:length(all_comb)){

        for(i in 1:nrow(all_comb[[x]])) {

            name <- paste(all_comb[[x]][i,], collapse = '_')
            group <- paste0("Y ~ ",paste0(all_comb[[x]][i,],collapse =" + "))
            result[[name]] <- lm(group, data =df )          

        }

     }

调用result给出

...
  ...

$x1_x3

Call:
lm(formula = group, data = df)

Coefficients:
(Intercept)           x1           x3  
     6.6647      -0.3864      -0.0954  


$x2_x3

Call:
lm(formula = group, data = df)

Coefficients:
(Intercept)           x2           x3  
     5.3037       0.1438      -0.1459  


$x1_x2_x3

Call:
lm(formula = group, data = df)

Coefficients:
(Intercept)           x1           x2           x3  
    6.16101     -0.39160      0.15794     -0.07796  

数据:

df <- data.frame(Y=runif(100,1,10),
    x1= runif(100,1,5),
    x2= runif(100,1,5), 
    x3= runif(100,1,5))

暂无
暂无

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

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