0

I have built a model and try to use the model to plot "Histogram for residual". But I am keep getting error (Can't rename variables in this context.) enter image description here

     output$histogram_residuals <-renderPlot({
  mod <- getModel()
  data <-getData()
  
  data$Yhat <- predict(mod, data)
  data$Residual <- data$DEATH_RATE - data$Yhat
  
  ggplot(data = data, 
         mapping = aes(x = Residual)) +
         geom_histogram(binwidth = 1) +
        labs(title = "Histogram of residuals showing outliers")
})

getModel <-  reactive({
  d <-getData()
  subIndex <- getPartition()
  train <- d[subIndex,]
  test <- d[-subIndex,]
  
  rec <- recipes::recipe(DEATH_RATE ~., data = train) %>%
    update_role(CODE, new_role = "id") %>%    #id is not a predictor
    step_naomit(DEATH_RATE, skip = TRUE) %>%
    step_naomit(POPULATION, skip = TRUE) %>%
    step_center(all_numeric(), -has_role("outcome")) %>%
    step_scale(all_numeric(), -has_role("outcome")) %>%
    step_knnimpute(all_numeric(), neighbours = 3) %>%
    step_dummy(all_nominal())
  
  model <- caret::train(rec, 
                        data = train,
                        method = "glmnet",
                        trControl = trainControl(method = "cv", number = 10)
  )
  model
})
June
  • 955
  • 5
  • 20
  • 33
  • 1
    If you don't get a good response, consider following this guidelines: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1. For instance, there isn't a dataset and I think you can remove Shiny from the example. It shouldn't have anything to do glmnet and NAs here. – wibeasley Aug 26 '21 at 05:30

0 Answers0