0

Hello I would like to regress each of the first 685 columns in a data set on a specified column contained in the data set. For example, I use lapply to collect the regression of the first 685 variables in the data set on variable mktrfbeta: So, if we have column vectors a, b, c, d, e, f, g, h,

I want to collect a~h b~h c~h . . . g~h

I should be able to do this using the lappy function. In fact, I can do it using the lm function:

lapply(1:685, function(x) lm((testing[,x]) ~  0 + mktrfbeta, data=testing))

However, when I try to use the gls() function in place of the lm function, it does not work:

lapply(1:685, function(x) gls((testing[,x]) ~  0 + mktrfbeta,data=testing))

I get this error:

Error in model.frame.default(formula = ~testing + x + mktrfbeta :  invalid type (list) for variable 'testing'

And I am not sure what to do. Any help would be appreciated.

  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Sep 28 '20 at 21:38
  • 1
    @MrFlick that seems high-enough-probability that I'm tempted to close-as-dupe and wait for OP to come back and say it *doesn't* work (and give [mcve]), in which case we can reopen ... ? – Ben Bolker Sep 28 '20 at 21:47
  • im trying it now – Cairn Hearties Sep 28 '20 at 21:48
  • @BenBolker OK. I've dup-ed it. OP, you should find that the answers for `lme` will also work for `gls` as they are from the same package. If it doesn't work, then please update your question with more details so we can run and test it. – MrFlick Sep 28 '20 at 21:48
  • I could not figure out how to apply the solution in that other stack exchange. – Cairn Hearties Sep 28 '20 at 22:47

0 Answers0