简体   繁体   中英

Loop linear regression different predictor and outcome variables

I'm new to R but am slowly learning it to analyse a data set.

Let's say I have a data frame which contains 8 variables and 20 observations. Of the 8 variables, V1 - V3 are predictors and V4 - V8 are outcomes.

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:

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? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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