简体   繁体   English

无法预测 R Shiny 中的单个值

[英]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.我正在用 R 闪亮创建一个仪表板,但由于模拟预测过程中的一些问题,它不起作用。 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警告:h 中的错误:在为函数“as.matrix”选择方法时评估参数“x”时出错:dup_mMatrix_as_dgeMatrix 的类“NA”无效

I also get the same error when I delete the as.matrix line here, but need that line for the second mentioned case.当我在此处删除 as.matrix 行时,我也会遇到相同的错误,但在提到的第二种情况下需要该行。

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警告:对比错误<-:对比只能应用于具有 2 个或更多级别的因子

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() :试试这个而不是使用 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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