简体   繁体   中英

How to do statistics and save results in a loop in R

In modeling it is helpful to do univariate regressions of a dependent on an independent in linear, quadratic, cubic and quaternary (?) forms to see which captures the basic shape of the statistical data. I'm a fairly new R programmer and need some help.

Here's pseudocode:

for i in 1:ncol(data)
  data[,ncol(data) + i] <- data[, i]^2     # create squared term
  data[,ncol(data) + i] <- data[, i]^3     # create cubed term
  ...and similarly for cubed and fourth power terms

# now do four regressions starting with linear and including one higher order term        each time and display for each i the form of regression that has the highest adj R2.

 lm(y ~ data[,i], ... )
 # retrieve R2 and save indexed for linear case in vector in row i
 lm(y tilda data[,i], data[,ncol(data) + i, ...]
 # retrieve R2 and save...

Result is a dataframe indexed by i with column name in data of original x variable and results for each of the four regressions (all run with an intercept term).

Ordinarily we do this by looking at plots but where you have 800 variables that is not feasible.

If you really want to help out write code to automatically insert the required number of exponentiated variables into data.

And this doesn't even take care of the kinky variables that come clumped up in a couple of clusters or only relevant at one value, etc.

I'd say the best way to do this is by using the polynomial function in R, poly() . Imagine you have an independent numeric variable, x , and a numeric response variable, y .

models=list()
for (i in 1:4)
    models[[i]]=lm(y~poly(x,i),raw=TRUE)

The raw=TRUE part ensures that the model uses the raw polynomials, and not the orthogonal polynomials.

When you want to get one of the models, just type in models[[1]] or models[[2]] , etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM