statistics - Polynomial feature expansion in R -


i'd polynomial feature expansion data frame -- example, quadratic expansion of df (x1, x2, x3) should give df (x1, x2, x3, x1^2, x2^2, x3^2, x1x2, x1x3, x2x3). i'm using poly(df$x1, df$x2, df$x3, degree=2, raw=t) requires unnecessary amount of typing if have large number of columns. (and poly(df[,1:20], degree=2, raw=t) doesn't work.) what's best way this?

edit: have many columns poly (vector large error). got work simple for loop:

polyexp = function(df){   df.polyexp = df   colnames = colnames(df)   (i in 1:ncol(df)){     (j in i:ncol(df)){       colnames = c(colnames, paste0(names(df)[i],'.',names(df)[j]))       df.polyexp = cbind(df.polyexp, df[,i]*df[,j])     }   }   names(df.polyexp) = colnames   return(df.polyexp) } 

just add additional loops compute higher-order terms.

you do.call:

do.call(poly, c(lapply(1:20, function(x) dat[,x]), degree=2, raw=t)) 

basically do.call takes first argument function called (poly in case) , second argument list. each element of list passed argument function. here make list containing of columns want process (i've used lapply list without typing) followed 2 additional arguments want pass.

to see working on simple example:

dat <- data.frame(x=1:5, y=1:5, z=2:6) do.call(poly, c(lapply(1:3, function(x) dat[,x]), degree=2, raw=t)) #      1.0.0 2.0.0 0.1.0 1.1.0 0.2.0 0.0.1 1.0.1 0.1.1 0.0.2 # [1,]     1     1     1     1     1     2     2     2     4 # [2,]     2     4     2     4     4     3     6     6     9 # [3,]     3     9     3     9     9     4    12    12    16 # [4,]     4    16     4    16    16     5    20    20    25 # [5,]     5    25     5    25    25     6    30    30    36 # attr(,"degree") # [1] 1 2 1 2 2 1 2 2 2 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -