Я работаю над простой моделью ML на R с использованием блестящих приложений, структура приложения будет выглядеть так:
1) Загрузка данных из локального файла 2) Обучение модели с загруженными данными3) Составьте график результатов
Моя проблема находится на этапе 2 , я могу нанести входные данные с помощью этого кода:
output$plot1 <- renderPlot({
ggplot(mydata(), aes(x=LotArea, y=SalePrice)) + geom_point()
})
Но так какпрогнозируемые значения не в исходном DF, мне нужно сначала добавить их.
Код, который я использую для этого:
obsB <- reactive({
set.seed(0)
xgb_model = train(
mydata()["LotArea"], as.vector(t(mydata()["SalePrice"])),
trControl = xgb_trcontrol,
tuneGrid = xgbGrid,
method = "xgbTree"
)
predicted = predict(xgb_model, mydata()["LotArea"])
mydata()["predicted"] = predicted
})
Это ошибка, которую я получаю:
Warning: Error in FUN: object 'predicted' not found
Это происходит, когда я меняю "LotArea" на "предсказанный"
output$plot1 <- renderPlot({
ggplot(mydata(), aes(x=predicted, y=SalePrice)) + geom_point()
})
Это полный код, который у меня есть:
library(shiny)
library(readxl)
library(tidyverse)
library(xgboost)
library(caret)
library(iml)
#### UI
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
mainPanel(
#tableOutput("contents"),
plotOutput("plot1", click = "plot_brush")
)
)
)
server <- function(input, output) {
mydata <- reactive({
req(input$file1, input$header, file.exists(input$file1$datapath))
read.csv(input$file1$datapath, header = input$header)
})
output$contents <- renderTable({
req(mydata())
#mydata()
})
### test
xgb_trcontrol = trainControl(
method = "cv",
number = 5,
allowParallel = TRUE,
verboseIter = FALSE,
returnData = FALSE
)
#I am specifing the same parameters with the same values as I did for Python above. The hyperparameters to optimize are found in the website.
xgbGrid <- expand.grid(nrounds = c(10,14), # this is n_estimators in the python code above
max_depth = c(10, 15, 20, 25),
colsample_bytree = seq(0.5, 0.9, length.out = 5),
## The values below are default values in the sklearn-api.
eta = 0.1,
gamma=0,
min_child_weight = 1,
subsample = 1
)
obsB <- reactive({
set.seed(0)
xgb_model = train(
mydata()["LotArea"], as.vector(t(mydata()["SalePrice"])),
trControl = xgb_trcontrol,
tuneGrid = xgbGrid,
method = "xgbTree"
)
predicted = predict(xgb_model, mydata()["LotArea"])
mydata()["predicted"] = predicted
})
output$plot1 <- renderPlot({
ggplot(mydata(), aes(x=predicted, y=SalePrice)) + geom_point()
})
}
shinyApp(ui, server)
РЕДАКТИРОВАТЬ:
Я изменил:
mydata()["predicted"] = predicted
для:
data = mydata()
data["predicted"] = predicted
Но знайте, что я получаю другую ошибку:
Warning: Error in : You're passing a function as global data.
Have you misspelled the `data` argument in `ggplot()
РЕДАКТИРОВАТЬ 2: Этообразец данных, которые я использую:
https://drive.google.com/file/d/1R8GA0fW0pOgG8Cpykc8mAThvKOCRCVl0/view?usp=sharing