闪亮的应用程序多元回归

时间:2018-04-24 13:27:49

标签: r shiny

library(shiny)


ui <- fluidPage(

  titlePanel("Linear model DARP"),


  sidebarLayout(
  sidebarPanel(

     sliderInput(inputId = "area",
                 "select the service region area:",
                 min= 170,
                 max= 8000,
                 value=1001),
     sliderInput(inputId = "crit..peak",
                 label="Choose Peak demand:",
                 min=10,
                 max=150,
                 value=39)
  ),


  mainPanel(
     tableOutput("table")
  )
  )
  )


server <- function(input, output) {

   output$table <- renderTable({

    df_ln<-read.csv("F:/Project/Programme/ML/DAR Machine Learning TR Part A/train_darp_ln.csv")
Linearmodel_DARP<-lm(veh~area+crit..peak,data = df_ln)
 new_demand1<-data.frame(area=input$area)
 new_demand2<-data.frame(crit..peak=input$crit..peak
 fleetsize<-predict(Linearmodel_DARP,newdata=c(new_demand1,new_demand2))
 round(exp(fleetsize),0)
})
}

shinyApp(ui = ui, server = server)  我在运行应用程序时找不到错误对象crit ..peak 应用程序应该通过滑块从用户获取两个输入,并且基于多重回归,它将给出预测命令的预测 请帮忙,因为我需要尽快完成一个项目

structure(list(area = c(2217.7, 6537.4, 1705.5, 5634, 1260.5, 
4797.7), density = c(0.13753, 0.016826, 0.18469, 0.021477, 0.25862, 
0.027305), crit..CV = c(0.63954, 0.81437, 0.49909, 0.33935, 0.39148, 
0.17489), crit..peak = c(49L, 26L, 41L, 20L, 39L, 18L), TW = c(21L, 
47L, 54L, 48L, 17L, 41L), L = c(569L, 576L, 391L, 390L, 458L, 
392L), s = c(7L, 3L, 3L, 6L, 3L, 2L), speed = c(18L, 26L, 20L, 
30L, 24L, 33L), circuity = c(1.3284, 1.1494, 1.4597, 1.2725, 
1.0486, 1.0792), cap = c(9L, 9L, 5L, 8L, 5L, 7L), mrt = c(1.5452, 
2.3743, 1.5962, 2.6065, 2.1278, 2.6228), veh = c(4.605170186, 
3.433987204, 4.718498871, 3.951243719, 4.060443011, 3.526360525
), veh.hrs = c(6.665569062, 5.523778231, 6.496186582, 5.71857256, 
5.816843267, 5.256713817), veh.km = c(9.555940819, 8.781874769, 
9.491918855, 9.119769942, 8.994897097, 8.753221378)), .Names = c("area", 
"density", "crit..CV", "crit..peak", "TW", "L", "s", "speed", 
"circuity", "cap", "mrt", "veh", "veh.hrs", "veh.km"), row.names = c(NA, 
 6L), class = "data.frame")

2 个答案:

答案 0 :(得分:0)

好的,所以您的问题可能是由于您尝试制作新数据框的方式。您创建了两个单独的1维数据帧,然后将它们连接起来,生成了一个数据帧列表。要创建包含两个或更多变量的数据框,请在数据框定义中定义它们,或使用cbind将数据框连接在一起:

 new_demand <- data.frame(area = input$area,
                          crit..peak = input$crit..peak)
 fleetsize <- predict(Linearmodel_DARP, newdata = new_demand)

这应该可以解决您的问题。将来,当你遇到像error object ... is not found这样的错误时,要做的第一件事就是检查你生成的对象是否符合你的想法。 class函数会告诉您c(new_demand1, new_demand2)list而不是data.frame

答案 1 :(得分:0)

错误原因可能是read.csv,因为它缺少header = T

让我们试试这段代码

server <- function(input, output) {
  output$table <- renderTable({
    df_ln <- read.csv("F:/Project/Programme/ML/DAR Machine Learning TR Part A/train_darp_ln.csv", header = T)
    Linearmodel_DARP <- lm(veh~area+chrit..peak, data = df_ln)
    new_demand1 <- data.frame(area=input$area)
    new_demand2 <- data.frame(crit..peak=input$crit..peak)
    fleetsize <- predict(Linearmodel_DARP, newdata=c(new_demand1, new_demand2))
    round(exp(fleetsize), 0)
  })
}