Skip to main content
2 of 6
Fixed some formatting and removed unnecessary tags
Ram RS
  • 2.5k
  • 11
  • 29

How to get coefficient tables from multiple regression model result!

I've done multiple regression modeling in R using the below script. Now I'm wondering how can I make a table for the coefficients of each model of all models from lm, glm, and rf separately.

X09cPaC_w20_inter <- read_delim("09cPaC_w20_inter.bed", 
                                delim = "\t", escape_double = FALSE, 
                                col_names = FALSE, trim_ws = TRUE)

PaC09c_w20_dat <- X09cPaC_w20_inter[, -c(3, 39: 41)] 

colnames(PaC09c_w20_dat) <- c("chr","start",
                              "EE88290", "EE88291", "EE88292", "EE88293", "EE88294",
                              "EE88295", "EE88296", "EE88297", "EE88298", "EE88299",
                              "EE88300", "EE88301", "EE88302", "EE88303", "EE88304",
                              "EE88305", "EE88306", "EE88307", "EE88308", "EE88309",
                              "EE88310", "EE88311", "EE88312", "EE88313", "EE88314",
                              "EE88315", "EE88316", "EE88317", "EE88318", "EE88319",
                              "EE88320", "EE88321", "EE88322", "EE88323", "EE88324",
                              "blood_vessel_w20", "adrenal_gland_w20",
                              "bone_element_w20", "brain_w20", "bronchus_w20",
                              "esophagus_w20", "extraembryonic_structure_w20", "eye_w20",
                              "gonad_w20", "heart_w20", "kidney_w20", "large_intestine_w20",
                              "liver_w20", "lung_w20", "lymphatic_vessel_w20", "lymphoblast_w20",
                              "mammary_gland_w20", "mouth_w20", "muscle_organ_w20", "pancreas_w20",
                              "prostate_gland_w20", "skin_w20", "spinal_cord_w20", "stomach_w20",
                              "thyroid_gland_w20", "tongue_w20", "urinary_bladder_w20")

PaC09c_w20_dat2 <- sample_n(PaC09c_w20_dat, 100000)

for (i in names(PaC09c_w20_dat2)[grep("EE", names(PaC09c_w20_dat2))]){
  PaC09c_w20_dat2[, paste0(i, "ln1")] <- log(PaC09c_w20_dat2[ , i] + 1) / max(PaC09c_w20_dat2[ , i])
}

PaC09c_w20_dat3 <- PaC09c_w20_dat2[,c(65:99,38:64)]

all_variables <- names(PaC09c_w20_dat3)
response_variables <- all_variables[c(1:35)]
predictors <- all_variables[-c(1:35)]

lm_model <- lapply(
  response_variables,
  function(x) lm(reformulate(termlabels = predictors, response = x), data = PaC09c_w20_dat3)
) |>
  setNames(response_variables)

glm_model <- lapply(
  response_variables,
  function(x) glm(reformulate(termlabels = predictors, response = x), data = PaC09c_w20_dat3)
) |>
  setNames(response_variables)

rf_ranger <- lapply(
  response_variables,
  function(x) ranger(reformulate(termlabels = predictors, response = x), data = PaC09c_w20_dat3, importance="impurity")
) |>
  setNames(response_variables)

save.image(file="PaC09c_w20_Reg.RData")
Deb
  • 319
  • 1
  • 5