如何将变量名和另一个变量的数据框与数据进行回归分析?

时间:2018-08-19 03:21:27

标签: r regression formula linear-regression lm

我有两个数据框:

x = data.frame(Var1= c("A", "B", "C", "D","E"),Var2=c("F","G","H","I","J"),
    Value= c(11, 12, 13, 14,18))

y = data.frame(A= c(11, 12, 13, 14,18), B= c(15, 16, 17, 14,18),C= c(17, 22, 23, 24,18), D= c(11, 12, 13, 34,18),E= c(11, 5, 13, 55,18),  F= c(8, 12, 13, 14,18),G= c(7, 5, 13, 14,18),
    H= c(8, 12, 13, 14,18), I= c(9, 5, 13, 14,18), J= c(11, 12, 13, 14,18))

Var3 <- rep("time", each=length(x$Var1))

x=cbind(x,Var3)

time=seq(1:length(y[,1]))
y=cbind(y,time)

> x
  Var1 Var2 Value Var3
1    A    F    11 time
2    B    G    12 time
3    C    H    13 time
4    D    I    14 time
5    E    J    18 time
> y
   A  B  C  D  E  F  G  H  I  J time
1 11 15 17 11 11  8  7  8  9 11    1
2 12 16 22 12  5 12  5 12  5 12    2
3 13 17 23 13 13 13 13 13 13 13    3
4 14 14 24 34 55 14 14 14 14 14    4
5 18 18 18 18 18 18 18 18 18 18    5

x DF,我将变量AF作为第一行。我想在y DF中选择这两个变量并实现简单的回归:lm(A ~ F, data = y),然后将结果保存在列表的第一位置。我将对x DF的第二行执行回归lm(B ~ G, data = y)进行同样的操作。

如何将x中的变量名与y中的数据进行回归?


修订后的问题:更复杂的回归Var1 ~ Var2 + Var3怎么样?

1 个答案:

答案 0 :(得分:1)

x = data.frame(Var1= c("A", "B", "C", "D","E"),
               Var2=c("F","G","H","I","J"),
               Value= c(11, 12, 13, 14,18))

y = data.frame(A= c(11, 12, 13, 14,18),
               B= c(15, 16, 17, 14,18),
               C= c(17, 22, 23, 24,18),
               D= c(11, 12, 13, 34,18),
               E= c(11, 5, 13, 55,18),
               F= c(8, 12, 13, 14,18),
               G= c(7, 5, 13, 14,18),
               H= c(8, 12, 13, 14,18), 
               I= c(9, 5, 13, 14,18),
               J= c(11, 12, 13, 14,18))

我们可以使用

fitmodel <- function (RHS, LHS) do.call("lm", list(formula = reformulate(RHS, LHS),
                                              data = quote(y)))

modList <- Map(fitmodel, as.character(x$Var2), as.character(x$Var1))

modList[[1]]  ## for example
#Call:
#lm(formula = A ~ F, data = y)
#
#Coefficients:
#(Intercept)            F  
#     4.3500       0.7115  

备注:

  1. 使用do.call是为了确保reformulate在传递给lm时得到评估。这是理想的,因为它允许update之类的函数在模型对象上正常工作。参见Showing string in formula and not as variable in lm fit。为了进行比较:

    oo <- Map(function (RHS, LHS) lm(reformulate(RHS, LHS), data = y),
              as.character(x$Var2), as.character(x$Var1))
    oo[[1]]
    #Call:
    #lm(formula = reformulate(RHS, LHS), data = y)
    #
    #Coefficients:
    #(Intercept)            F  
    #     4.3500       0.7115  
    
  2. as.characterx$Var1上的x$Var2是必要的,因为这两个变量当前是“因数”变量而不是字符串,并且reformulate无法使用它们。如果在构建stringsAsFactors = FALSE时将data.frame放在x中,则不会出现此类问题。

  

对您有用吗?不是应该有一个“ for”循环吗?

Map函数隐藏该“ for”循环。它是mapply函数的包装。 R中的*apply系列函数为a syntactic sugar


有关您修订的问题的最新信息

您最初的问题是将模型公式构造为Var1 ~ Var2

您的新问题需要Var1 ~ Var2 + Var3

x$Var3 <- rep("time", each=length(x$Var1))
y$time <- seq(1:length(y[,1]))

## collect multiple RHS variables (using concatenation function `c`)
RHS <- Map(base::c, as.character(x$Var2), as.character(x$Var3))
#str(RHS)
#List of 5  ## oh this list has names! annoying!!
# $ F: chr [1:2] "F" "time"
# $ G: chr [1:2] "G" "time"
# $ H: chr [1:2] "H" "time"
# $ I: chr [1:2] "I" "time"
# $ J: chr [1:2] "J" "time"
LHS <- as.character(x$Var1)
modList <- Map(fitmodel, RHS, LHS)  ## `fitmodel` function unchanged
modList[[1]]  ## for example
#Call:
#lm(formula = A ~ F + time, data = y)
#
#Coefficients:
#(Intercept)            F         time  
#        5.6          0.5          0.5