日期在闪亮的应用程序中转换为月,日,星期等

时间:2016-07-22 06:21:51

标签: r shiny

我是闪亮应用的新手。我已经使用R建立了客户预测算法(线性回归)。在这种情况下,我将日期提取为月,日,周等,并视为自变量。现在,我想建立相同的闪亮应用程序。应用程序将采用日期作为输入和输出将是预测的客户编号。我有明确的历史客户数据。为了分析,我将月,周,日作为自变量,客户数是我的因变量。我训练线性回归用于预测目的。我遇到了将新输入日期转换为月,日,周等问题。 这样我就可以预测客户数量的新日期。请在这方面帮助我。

预测

    library(caret)
    mydata <- read.csv("main.csv", header = TRUE)

    mydata$date <- as.Date(mydata$date, format = "%m/%d/%Y")
    mydata$month <- strftime(mydata$date, "%m")
    mydata$day <- strftime(mydata$date, "%d")
    mydata$week <- strftime(mydata$date, "%w")
    mydata$week_year <- strftime(mydata$date, "%W")

    mydata$month <- as.factor(mydata$month)
    mydata$day <- as.factor(mydata$day)
    mydata$week <- as.factor(mydata$week)
    mydata$week_year <- as.factor(mydata$week_year)

    mydata <- mydata[c(1, 3, 4, 5, 6)]

    ind <- sample(2, nrow(mydata), replace = TRUE, prob=c(0.7, 0.3))
    trainset = mydata[ind == 1,]
    testset = mydata[ind == 2,]

    pred_cus <-glm(no_customer~month+week+day, 
                   data = trainset, 
                   family = gaussian)
    testset$prediction <- predict(pred_cus, testset)
    RMSE(testset$prediction, testset$no_customer)

structure(list(
  Customer = c(94L, 61L, 51L, 28L, 29L, 56L, 99L, 87L, 88L, 71L, 40L, 33L,
               57L, 71L, 84L, 81L, 57L, 31L, 28L, 77L, 84L, 69L, 76L, 65L,
               36L, 26L, 60L, 70L, 82L, 81L, 49L, 54L, 18L, 66L, 89L, 69L,
               61L, 88L, 40L, 25L, 82L, 77L, 88L, 72L, 75L, 40L, 24L, 79L,
               67L, 82L, 55L, 78L, 44L, 14L, 76L, 89L, 87L, 93L, 64L, 23L,
               34L, 65L, 83L, 92L, 87L, 105L, 40L, 32L, 80L, 76L, 83L, 76L,
               70L, 43L, 33L, 75L, 75L, 70L, 55L, 70L, 36L, 13L, 64L, 72L,
               79L, 62L, 52L, 30L, 32L, 85L, 87L, 84L, 93L, 73L, 21L, 19L,
               101L, ''''''''''''''''''''''''''''''''
  Label = c("1/1/2016", "1/10/2016", "1/11/2016", "1/12/2016", "1/13/2016", 
            "1/14/2016", "1/15/2016", "1/16/2016", "1/17/2016", "1/18/2016", 
            "1/19/2016", "1/2/2016", "1/20/2016", "1/21/2016", "1/22/2016", 
            "1/23/2016", "1/24/2016", "1/25/2016", "1/26/2016", "1/27/2016", 
            "1/28/2016", "1/29/2016", "1/3/2016", "1/30/2016", "1/31/2016", 
            "1/4/2016", "1/5/2016", "1/6/2016", "1/7/2016", "1/8/2016",
            "1/9/2016", "10/1/2015", "10/10/2015", "10/11/2015", "10/12/2015", 
            "10/13/2015", "10/14/2015", "10/15/2015", "10/16/2015", "10/17/2015", 
            "10/18/2015", "10/19/2015", "10/2/2015", "10/20/2015", "10/21/2015", 
            "10/22/2015", "10/23/2015", "10/24/2015",
            class = "factor")
  ), 
  .Names = c("Customer", date"), 
  class = "data.frame",
  row.names = c(NA, -457L)
)

0 个答案:

没有答案