0
votes

Say I have two dataframes, one with a set of explanatory variables and another with a set of response variables.

Explanatory <- as.data.frame(matrix(sample(0:15, 4*20, replace=TRUE), ncol=4))
Response <- as.data.frame(matrix(sample(0:15, 4*20, replace=TRUE), ncol=4))

How do I conduct several second-order polynomial regressions between all individual explanatory and response variables and output a matrix of p-values?

I can do this for a normal linear regression...

Linear <- lapply(Explanatory, function (x) lm(x~., data = Response))
sapply(Linear, function(f) summary(f)$coefficients[,4])

But when I do the same thing for a polynomial...

Polynomial <- lapply(Explanatory, function (x) lm(x~poly(.,2), data = Response))

...I get this error message ' Error in poly(., 2) : anyNA() applied to non-(list or vector) of type 'closure' '

Thank you in advance for any help!

1

1 Answers

1
votes

You will need to manually add in the polynomial terms using the I function:

lapply(Explanatory, function (x) lm(x~.+I(V1^2), data = Response))

While it is a shortcut to use the . operator, it is safer to specify your desired function. Note the difference between:

lapply(Explanatory, function (x) lm(x~V1+V2+V3+V4, data = Response))
lapply(Explanatory, function (x) lm(x~V1*V2*V3*V4, data = Response))