r - How to apply predict function to number of data frames in a list? -



r - How to apply predict function to number of data frames in a list? -

i have model object , apply predict values number (e.g. 5) of new info stored list. info of same length , contain same 3 predictor variables. in end, have predicted values stored matrix or list has many columns (or elements) have datasets used in prediction (e.g. 5).

being total newbie in r programming, haven’t figured out working solution problem.

this simple illustration question: firstly, let's generate info , create model object, in example, linear model:

training <- data.frame(y=rnorm(10), x1=rnorm(10), x2=rnorm(10)) model <- lm(y~., data=training)

then, let's generating info in list predictions:

testing <- list() (i in 1:5){ testing[[i]] <- data.frame(x1=rnorm(10), x2=rnorm(10)) }

finally, apply prediction function lapply every new info in list , unlist predictions' list matrix:

predictions <- lapply(testing, function(x){predict(model, newdata=x)}) predictions_matrix <- matrix(unlist(predictions), nrow=5)

r apply prediction lapply predict

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -