如何使我的train()函数发挥作用,以评估模型性能?

时间:2019-12-12 01:37:09

标签: r shiny

我正在一个项目上创建一个闪亮的应用程序,该应用程序允许用户进行一些建模,选择模型并做出一些预测。

当我在数据上的r控制台中运行train()函数时,它可以正常工作。当我在闪亮的应用程序中运行它时,我始终会遇到一个错误:对比只能应用于2个或更多级别的因子。我的数据框没有任何因素。无法弄清楚为什么它可以在控制台中工作但不能闪亮。任何帮助将不胜感激!

这是我的代码:

library(shiny)

shinyUI(fluidPage(

    # Application title
    titlePanel("Variables Impacting Hate Crime Rates"),

    # define main panel layout
        mainPanel(
            tabsetPanel(type = "tabs",
                        tabPanel("Information",
                                 textOutput("introduction")),
                        tabPanel("Data Exploration",
                                 textOutput("data"),
                                 selectInput("x","Explanatory variable:",
                                             choices = colnames(crimes),
                                             selected = "median_household_income"),
                                 selectInput("y","Response variable:",
                                             selected = "median_household_income",
                                            choices = colnames(crimes)),
                                 plotOutput("scatterplot"),
                                 selectInput("y2", "Variable to compare:",
                                             selected = "median_household_income",
                                             choices = colnames(crimes)),
                                 plotOutput("boxplot"),
                                 selectInput("var","Summary variable:",
                                             choices = colnames(crimes),
                                             selected = "median_household_income"),
                                 verbatimTextOutput("summary"))
                        ,
                        tabPanel("Unsupervised Learning",
                                 selectInput("k","Number of clusters:",
                                             choices = c(1,2,3),
                                             selected = 1),
                                 plotOutput("cluster"),
                                 selectInput("dmethod","Distance method:",
                                             choices = c("euclidian","binary","minkowski",
                                                         "canberra","manhattan","maximum"),
                                             selected = "euclidian"),
                                 selectInput("cmethod", "Cluster method:",
                                             choices = c("single","complete"),
                                             selected = "single"),
                                 plotOutput("tree")),
                        tabPanel("Modeling",
                                 selectInput("xvar", "x variable:",
                                            choices = colnames(crimes),
                                            selected = "gini_index"),
                                 selectInput("yvar", "y variable:",
                                             choices = colnames(crimes),
                                             selected = "median_household_income"),
                                 verbatimTextOutput("model")),
                        tabPanel("Data",
                                 DT::dataTableOutput("mytable1"))
        )
    )
))


source("C:/Users/W447075/Documents/ST558/Comora_final/helpers.R")

shinyServer(function(input, output,session){
     output$introduction <- renderText({
        "This is my introduction"
    })

     output$data <- renderText({
       "This page allows you to create some basic graphical
       and numeric summaries including a scatter plot, boxplot,
       and statistical summary for any of the variables in the 
       'crimes' dataset."
     })

    selectedData <- reactive({
      crimes %>% filter(region ==input$region)
    })
    selectData2 <- reactive({
      crimes[ , c("region",input$y)]
    })

    selectData3 <- reactive({
      crimes[ , c("region", input$y2)]
    })

    mydata <- reactive({
      crimes[ ,input$var]
    })

    mycluster <- reactive({
      kmeans(df_scale, centers = input$k, nstart = 25)
    })

    d <- reactive({
      dist(df, method = input$dmethod)
    })

    clusterplot <- reactive({
      plot(hclust(d(), method = input$cmethod))
    })

    model <- reactive({
      train(input$yvar ~ input$xvar, crimes,
            method = "lm",
            trControl = trainControl(
              method = "cv", number = 10,
              verboseIter = TRUE))
    })

    #render a barplot
    output$scatterplot <- renderPlot({
      ggplot(crimes, aes_string(x = input$x,
                                        y = input$y)) + 
        geom_point(size = 3) + 
        theme(axis.text.x = element_blank(),
              axis.ticks.x = element_blank(),
              axis.text.y = element_blank())
    })

    output$boxplot <- renderPlot({
      ggplot(selectData3(), aes(x = region, y = selectData3()[ ,input$y2])) + 
        geom_boxplot(aes(fill = region)) + ylab(input$y2)+
        theme(axis.text.x = element_blank(),
              axis.ticks.x = element_blank(),
              axis.text.y = element_blank())
    })

    output$summary <- renderPrint({
      dataset <- na.omit(mydata())
      summary(dataset)
    })

    output$cluster <- renderPlot({
      fviz_cluster(mycluster(),data = df)
    })

    output$tree <- renderPlot({
      clusterplot()
    })

    output$mytable1 <- DT::renderDataTable({
      DT::datatable(df)
    })

    output$model <- renderText({
      model()
    })
    })


0 个答案:

没有答案