-5

I currently have two data tables one of which contains independent and control variables in columns while the other contains rows of dependent variables.

Can anybody help in creating a method to do linear models from the two tables that repeats for each row in the dependent value table?

4

1 回答 1

2

您没有提供也没有可重现的示例,也没有提供所需的输出,所以我不得不猜测

如果这是您的列名向量

vec <- LETTERS[1:3]

这是你的数据集

set.seed(1)
df <- data.frame(A = sample(10, 10), 
                 B = sample(20, 10),
                 C = sample(30, 10))

然后你可以尝试类似的东西

lapply(vec, 
       function(x) lm(as.formula(paste(x, "~", 
                                       paste(setdiff(names(df), x), 
                                                     collapse = "+"))), 
                      data = df))

哪个会给

# [[1]]
# 
# Call:
#   lm(formula = as.formula(paste(x, "~", paste(setdiff(names(df), 
#                                                       x), collapse = "+"))), data = df)
# 
# Coefficients:
#   (Intercept)            B            C  
# 4.9687       0.2410      -0.1565  
# 
# 
# [[2]]
# 
# Call:
#   lm(formula = as.formula(paste(x, "~", paste(setdiff(names(df), 
#                                                       x), collapse = "+"))), data = df)
# 
# Coefficients:
#   (Intercept)            A            C  
# 2.7975       0.8182       0.2775  
# 
# 
# [[3]]
# 
# Call:
#   lm(formula = as.formula(paste(x, "~", paste(setdiff(names(df), 
#                                                       x), collapse = "+"))), data = df)
# 
# Coefficients:
#   (Intercept)            A            B  
# 13.200       -1.675        0.875  
于 2014-09-01T21:06:32.427 回答