linear regression - How to replace the fitted value in multiple columns in R -


i have dataframe called new.cars. need apply linear regression formula columns in dataframe. there thousands of columns in new.cars, indicating each of them not possible in formula. there 4 columns, pcas remain same in formula other columns (columns other pcas) in want apply formula.

the formula first column (column mercedes) is

fit1 <- lm(mercedes ~ pca1 + pca2+pca3+pca4, data=new.cars) new.cars[,"mercedes"] <-fit1$fitted.values 

and forth other car columns.. best way replace column values fitted value (and omitting na values in column, means don't want change nas--as empty cells , need not fitted)?

  new.cars<- structure(list(mercedes = c(1, 1, 1, 1), vw = c(1, 2, 0, na),              camry = c(2, 0, 0, na), civic = c(4, 1, 1, 1), ferari = c(2,              2, 2, 0), pca1 = c(0.021122, 0.019087, 0.022184, 0.021464             ), pca2 = c(0.023872, 0.024295, 0.022471, 0.027509), pca3 = c(0.000784,              0.001996, 0.003911, 0.006119), pca4 = c(-0.004811, -0.003296,              0.001868, -0.001636)), .names = c("mercedes", "vw", "camry",          "civic", "ferari", "pca1", "pca2", "pca3", "pca4"), row.names = c("s05-f13-p01.gt",          "s08-f10-p01.gt", "s08-f11-p01.gt", "s09-f66-p01.gt"), class = "data.frame") 

we may loop through names of 'new.cars' (that not pca), create formula paste using 'pca' variables independent variables, extract 'fitted values' in list ('lst'). create new dataset ('new1.cars') subsetting non-pca columns 'new.cars'. pad na list elements have shorter length 'maximum' length of elements in 'lst' , assign output new dataset.

lst <- lapply(names(new.cars)[1:5], function(x)    lm(formula(paste(x, '~', paste0("pca", 1:4, collapse="+"))), data= new.cars)$fitted.values) new1.cars <- new.cars[1:5] new1.cars[] <- lapply(lst, `length<-`,max(lengths(lst))) 

update

if there columns na values, can create exception avoid doing lm on that

lst <- lapply(names(new.cars)[1:5], function(x) {                  x1 <- new.cars[[x]]             if(all(is.na(x1))){                na } else lm(formula(paste(x, '~', paste0("pca", 1:4, collapse="+"))),              data= new.cars)$fitted.values          }) 

the rest of steps same above.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -