Repeat regression with varying dependent variable

后端 未结 3 1619
北海茫月
北海茫月 2021-01-22 17:58

I\'ve searched both Stack and google for a solution, none found to solve my problem.

I have about 40 dependent variables, for which I aim to obtain adjusted means (lsmea

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-22 18:00

    There were a few typos and things, but I think this is what you want:

    # Examplified here with 2 outcome variables
    outcome1 <- c(2, 4, 6, 8, 10, 12, 14, 16)
    outcome2 <- c(1, 2, 3, 4, 5, 6, 7, 8)
    var1 <- c("a", "a", "a", "a", "b", "b", "b", "b")
    var2 <- c(10, 11, 12, 9, 14, 9, 5, 8)
    var3 <- c(100, 101, 120, 90, 140, 90, 50, 80)
    
    df <- data.frame(outcome1, outcome2, var1, var2, var3)
    
    dependents <- c("outcome1", "outcome2")
    
    library(lsmeans) #install.packages("lsmeans")
    
    results <- list()
    for (i in seq_along(dependents)) {
      eq <- paste(dependents[i],"~ var1 + var2 + var3")
      fit <- lm(as.formula(eq), data= df)
      summary <- summary(lsmeans(fit, "var1"))
      summary$outcome <- i
      results[[i]] <- summary
    }
    

提交回复
热议问题