简体   繁体   中英

Can't predict a single value in R shiny

I am creating a dashboard with R shiny, but due to some problem during simulation prediction, it won't work. Strange thing is that exactly the same code works for another prediction:

sim_pred_data <- na.omit(sim_pred_data)

sim_pred_data <- as.matrix(sim_pred_data)

sim_predicted_output <-predict(lasso_best, s=lambda_best, newx=sim_pred_data)

During execution i get the following error:

Warning: Error in h: error in evaluating the argument 'x' in selecting a method for function 'as.matrix': invalid class 'NA' to dup_mMatrix_as_dgeMatrix

I also get the same error when I delete the as.matrix line here, but need that line for the second mentioned case.

The model is already prefitted.

ALteratively the following code throws another error:

Warning: Error in contrasts<-: contrasts can be applied only to factors with 2 or more levels

sim_pred_data <- na.omit(sim_pred_data)

sim_pred_data <- as.matrix(sim_pred_data)
sim_dummy <- dummyVars(" ~ .", data=sim_pred_data)

sim_pred_data <<- data.frame(predict(sim_dummy, newdata =          
sim_pred_data))

sim_predicted_output <-predict(lasso_best, s=lambda_best, 
newx=sim_pred_data)

The provided data has only one observation and named columns, to predict that single value.

I hope someone can help. Thanks

Try this instead of using omit() :

if (is.na(sim_pred_data)){}
else {
  sim_pred_data <- as.matrix(sim_pred_data)
  sim_predicted_output <-predict(lasso_best, s=lambda_best, 
  newx=sim_pred_data)
}

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