简体   繁体   English

循环线性回归不同的预测变量和结果变量

[英]Loop linear regression different predictor and outcome variables

I'm new to R but am slowly learning it to analyse a data set. 我是R的新手,但正在慢慢学习它来分析数据集。

Let's say I have a data frame which contains 8 variables and 20 observations. 假设我有一个数据框,其中包含8个变量和20个观察值。 Of the 8 variables, V1 - V3 are predictors and V4 - V8 are outcomes. 在8个变量中,V1-V3是预测变量,V4-V8是结果。

B = matrix(c(1:160),
nrow = 20,
ncol = 8,)

df <- as.data.frame(B)

Using the car package, to perform a simple linear regression, display summary and confidence intervals is: 使用car包装,执行简单的线性回归,显示摘要和置信区间为:

fit <- lm(V4 ~ V1, data = df)
summary(fit)
confint(fit)

How can I write code ( loop or apply ) so that R regresses each predictor on each outcome individually and extracts the coefficients and confidence intervals? 如何编写代码( loopapply ),以使R分别对每个结果的每个预测变量进行回归,并提取系数和置信区间? I realise I'm probably trying to run before I can walk but any help would be really appreciated. 我意识到我可能会在跑步之前尝试跑步,但是任何帮助将不胜感激。

You could wrap your lines in a lapply call and train a linear model for each of your predictors (excluding the target, of course). 您可以将行包裹起来,并为每个预测变量(当然,不包括目标变量)训练线性模型。

my.target <- 4
my.predictors <- 1:8[-my.target]

lapply(my.predictors, (function(i){
  fit <- lm(df[,my.target] ~ df[,i])
  list(summary= summary(fit), confint = confint(fit))
}))

You obtain a list of lists. 您获得列表列表。

So, the code in my own data that returns the error is: 因此,我自己的数据中返回错误的代码是:

my.target <- metabdata[c(34)]
my.predictors <- metabdata[c(18 : 23)]

lapply(my.predictors, (function(i){
   fit <- lm(metabdata[, my.target] ~ metabdata[, i])
   list(summary = summary(fit), confint = confint(fit))
   }))

Returns: 返回值:

Error: Unsupported index type: tbl_df

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

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