How to Loop/Repeat a Linear Regression in R

You want to run 22,000 linear regressions and extract the coefficients? That’s simple to do from a coding standpoint. set.seed(1) # number of columns in the Lung and Blood data.frames. 22,000 for you? n <- 5 # dummy data obs <- 50 # observations Lung <- data.frame(matrix(rnorm(obs*n), ncol=n)) Blood <- data.frame(matrix(rnorm(obs*n), ncol=n)) Age <- sample(20:80, … Read more

Is there a better alternative than string manipulation to programmatically build formulas?

reformulate will do what you want. reformulate(termlabels = c(‘x’,’z’), response=”y”) ## y ~ x + z Or without an intercept reformulate(termlabels = c(‘x’,’z’), response=”y”, intercept = FALSE) ## y ~ x + z – 1 Note that you cannot construct formulae with multiple reponses such as x+y ~z+b reformulate(termlabels = c(‘x’,’y’), response = c(‘z’,’b’)) z … Read more