0
votes

Mtcars has 32 observations of 11 variables. Assume that "mpg", "drat", "qsec" are the dependent variables of interest. Assume that "cyl" and "hp" are the independent variables for model type 1 and "disp" is the independent variable for model type 2. I want to automate some regressions but can't do Step (2) below.

What do I want to do?

On my actual dataframe, I have many more dependent variables of interest than independent variables.

  1. I want to run "lm" or "glm" for each of the following:

    • mpg~cyl+hp,
    • drat~cyl+hp,
    • qsec~cyl+hp,
    • mpg~disp,
    • drat~disp, and
    • qsec~disp
  2. This seems to be my biggest current problem. I want to make a new dataframe (estimates) that holds coefficient Estimate and Pr(>|t|), e.g. (assuming this were filled out),

    IV DV mpg.Est mpg.Pr drat.Est drat.Pr qsec.Est qsec.Pr cyl -2.26 0.00 .. .. .. .. hp -0.02 0.21 .. .. .. .. disp -0.04 0.00 .. .. .. ..

  3. Then I want to append columns to "estimates" that describe the Pr values of each IV (cyl, hp, disp), e.g. (assuming this were filled out),

    IV stat mean.Pr median.Pr min.Pr max.Pr cyl 0.03 0.02 0.00 0.18 hp .. .. .. .. disp .. .. .. ..

Attempts

##### Step (1)
## Make the formulae
##   For scale, it would be great to use varlists here:
##     dvvarlist <- c("mpg", "drat", "qsec")
##     ivvarlist <- c("cyl + hp", "disp")
models <- lapply(paste(c("mpg", "mpg", "drat", "drat", "qsec", "qsec"),
    c("cyl + hp", "disp"), sep = "~"), formula)

## Run the regressions
res.models <- lapply(models, FUN = function(x) 
    {summary(lm(formula = x, data = mtcars))})

##### Step (2)
## Spot the coefficients
coefficients(res.models[[1]])

## How to automate grab coefficients from all models?

## How to automate place coefficients in proper location in new dataframe?

##### Step (3)
## Append columns to "estimates"
##   For scale, could again use dvvarlist <- c("mpg", "drat", "qsec")
estimates$mean.Pr <- rowMeans(estimates[ , c("mpg.Est", "drat.Est", "qsec.Est")])

Relevant Links?

1
You can do all models with the same IVs at once. lm supports more than one DV. Just combine the DVs in the formula with cbind.Roland

1 Answers

3
votes

using base R:

 data("mtcars")
 y=c("mpg","drat","qsec")
 x=c("cyl+hp","disp")

 A=Map(function(i,j)
   summary(lm(as.formula(paste0(i,"~",j)),data=mtcars))$coef[,c(1,4)],
   rep(y,each=length(x)),x)   

 B=do.call(cbind.data.frame,
      tapply(A,rep(y,each=length(x)),
       function(s){a=do.call(rbind,s);a[row.names(a)!="(Intercept)",]}))
 B
        drat.Estimate drat.Pr(>|t|) mpg.Estimate mpg.Pr(>|t|) qsec.Estimate qsec.Pr(>|t|)
   cyl   -0.318242238  5.528430e-05  -2.26469360 4.803752e-04  -0.005485698   0.981671077
   hp     0.003401029  6.262861e-02  -0.01912170 2.125285e-01  -0.018339365   0.005865329
   disp  -0.003063904  5.282022e-06  -0.04121512 9.380327e-10  -0.006253039   0.013144036

It is still not clear to me what the third step needs. I hope you can elaborate further. Although I looked at your code and it seems you are looking for the mean of the coefficients, the median of the coefficients etc.. I do not know if you are looking for the mean ,max, etc of the probabilities also,but I just computed them in case you need them:

  C=split(data.frame(t(B)),rep(c("Estimate","Pr(>|t|)"),length(y)))

  D=lapply(C,function(f)
         matrix(mapply(function(i,j) i(j),
                          rep(c(mean,median,min,max),each=length(f)),f),length(f)))

   cbind(B,do.call(cbind.data.frame,lapply(D,`colnames<-`,c("mean","median","min","max"))))


     drat.Estimate drat.Pr(>|t|) mpg.Estimate mpg.Pr(>|t|) qsec.Estimate qsec.Pr(>|t|) Estimate.mean
cyl   -0.318242238  5.528430e-05  -2.26469360 4.803752e-04  -0.005485698   0.981671077   -0.86280718
hp     0.003401029  6.262861e-02  -0.01912170 2.125285e-01  -0.018339365   0.005865329   -0.01135334
disp  -0.003063904  5.282022e-06  -0.04121512 9.380327e-10  -0.006253039   0.013144036   -0.01684402
       Estimate.median Estimate.min Estimate.max Pr(>|t|).mean Pr(>|t|).median Pr(>|t|).min Pr(>|t|).max
  cyl     -0.318242238  -2.26469360 -0.005485698   0.327402245    4.803752e-04 5.528430e-05   0.98167108
  hp      -0.018339365  -0.01912170  0.003401029   0.093674136    6.262861e-02 5.865329e-03   0.21252847
  disp    -0.006253039  -0.04121512 -0.003063904   0.004383106    5.282022e-06 9.380327e-10   0.01314404

I believe you can transpose this to see it in one screen instead of scrolling left/right. If this helps let us know. Thank you