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.)
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
})