Output Regression statistics for each variable one at a time in R

后端 未结 3 1201
说谎
说谎 2021-01-15 11:35

I have a data frame that looks like this. names and number of columns will NOT be consistent (sometimes \'C\' will not be present, other times \"D\', \'E\', \'F\' may be pre

3条回答
  •  时光说笑
    2021-01-15 11:57

    This how I do this kind of modeling. Following example assumes I am varying different outcomes, and different exposures for a given set of covariates.

    I first define my outcomes and exposures I want to test (I think in terms of epidemiology but you can extend).

    outcomes <- c("a","b","c","d")

    exposures <- c("exp1","exp2","exp3")

    The assumption is that each element specified in those vectors exist as column names in your dataset (as well as the covariates listed below after the "~").

    final_lm_data <- data.frame() #initialize empty dataframe to hold results
    for (j in 1:length(exposures){
      for (i in 1:length(outcomes){
        mylm <- lm(formula(paste(outcomes[i], "~", "continuous.cov.1 + 
            continuous.cov.2 + factor(categorical.variable.1)", "+",
                                 exposure[j])), data=mydata)
    
        coefficent.table <- as.data.frame(coef(summary(mylm)))
    
        mylm_data <- as.data.frame(cbind(ctable,Variable = rownames(ctable),
                                         Outcome = outcomes[i],
                                         Exposure = exposures[j],
                                         Model_N = paste(length(mylm$residuals))))
        names(mylm_data)[4] <- "Pvalue"  # renaming the "Pr(>|t|)"
        rownames(mylm_data) <- NULL # important because we are creating stacked output dataset
        final_lm_data <- rbind(final_lm_data,mylm_data)
      }
    }
    

    This will give you a final_lm_data that contains your estimates, std.errors, tstatistics, pvalues for each variable in your model, and also keep track of the iteration of Outcome and Exposure (first and last elements of your model). Lastly, it has the N used after dropping data records for missing values. You can modify the mylm_data creation to capture more information from the model (such as rsq etc..).

    Finally, if covariates also vary from run to run, I am not sure how to automate that part.

提交回复
热议问题