2

What is the most efficient way to run regression models for a list of 20 independent variables (e.g. genetic variants, each of these genetic variants will be tested alone) and 40 dependent variables? I am a beginner to R! I found a solution but it would work only if I had 1 independent variable. Not sure how I would go about if I had many (http://techxhum.dk/loop-multiple-variables/)

Thanks for your time.

rkl
  • 47
  • 8
  • 1
    A few questions/clarifications. Do you want to test 20 independent variables, all with the same 40 dependent variables and the same set of observations? That is, you want to run 20 regressions each of which uses 40 DVs? How many observations do you have? Can you clarify the "80 dependent outcomes" in your title? – Ben Bolker Dec 14 '19 at 19:15
  • So basically I have 20 instruments and 80 dependent variables. I want to test each one instrument separately for each dependent variable separately (but yes I want to test each instrument with the same set of dependent variables). – rkl Dec 15 '19 at 13:18
  • So, 20*80 = 1600 different models overall? – Ben Bolker Dec 15 '19 at 13:50
  • yes that's correct. – rkl Dec 15 '19 at 14:10

1 Answers1

0

Here's a somewhat dense solution that uses the mfastLmCpp() function from the MESS package. It runs simple linear regression for multiple instruments and we just wrap it in an apply() call to get it to work with multiple dependent variables.

N <- 1000  # Number of observations
Nx <- 20   # Number of independent variables
Ny <- 80   # Number of dependent variables

# Simulate outcomes that are all standard Gaussians
Y <- matrix(rnorm(N*Ny), ncol=Ny)  
X <- matrix(rnorm(N*Nx), ncol=Nx)

# Now loop over each dependent variable and get a list of t test statistics
# for each independent variabel
apply(Y, 2, FUN=function(y) { MESS::mfastLmCpp(y=y, x=X) })

With the above setup it takes less than a second on my laptop.


Update: Added the functionality to the plr function in the MESS package.

devtools::install_github('ekstroem/MESS')
plr(Y, X)

et voila!

ekstroem
  • 5,957
  • 3
  • 22
  • 48