2016-11-10 4 views
-1

나는 다음과 같은 한 소스 데이터를 결과 : enter image description hereR - 돌리 예측이

지난 1백56주을 바탕으로, 나는 다음 52주에 대한 예측을하고 싶습니다. 내가 지금하고 싶은 무엇

  Point Forecast  Lo 80  Hi 80  Lo 95  Hi 95 
2003.000  1637.7675 -8.610502 3284.146 -880.15039 4155.685 
2003.019  1453.9059 -195.169681 3102.981 -1068.13753 3975.949 
2003.038  8668.6921 7016.923492 10320.461 6142.53000 11194.854 
2003.058  5851.0741 4196.616771 7505.531 3320.79997 8381.348 
2003.077  4333.9240 2676.782333 5991.066 1799.54453 6868.303 
2003.096  4284.5899 2624.768291 5944.412 1746.11178 6823.068 

은 다음과 같습니다 :을 설정

  1. 이 결과로 다시 제품 & 위치를 추가하고 다음 코드는 잘

    my.ds <- myDS[1, -c(3,4,5,6)] #reading my source file 
    my.start <- myDS[1, c(3)] 
    my.product <- myDS[1, c("Product")] 
    my.product <- myDS[1, c("Location")] 
    my.result <- melt(my.ds, id = c("Product","Location")) 
    my.result[order(my.result$variable),] 
    my.ts <- ts(my.result$value, frequency=52, start=c(my.start,1)) 
    my.fc <- forecast(my.ts, h=52) 
    my.fc 
    

    예측이 나에게 다음과 같은 출력을 제공 작동

  2. 계산 된 열 추가 : (하이 95) - (포인트 예측) (포인트 예측 열과 마찬가지로)
  3. 0 다시 테이블
  4. 피벗

enter image description here

여기 모양 변경을 시도 다음,하지만 결과는 테이블 형식으로 보이지 않기 때문에 그것을 수행하는 방법을 정말 확실하지로.

Link to download source file in csv

+0

myDS''무엇입니까? – Sotos

+0

그리고 전제 데이터 객체의 구조는 무엇입니까? * my.result * and * my.ts *? * my.fc *와 같은 행/열 길이? – Parfait

+0

@Sotos myDS는 CSV 파일 별 소스 데이터 집합입니다. –

답변

0

내 원하는 출력을 제공 않습니다 다음은

library(forecast) 
library(reshape) 
library(plyr) 

#exclude non required columns 
my.ds <- myDS[, -c(3,4,5,6)] 
#set the required date, Product, Location 
my.start <- myDS[1, c(3)] 
my.product <- myDS[1, c("Product")] 
my.location <- myDS[1, c("Location")] 
#unpivot the table 
my.result <- melt(my.ds, id = c("Product","Location")) 
#run forecasting 
# set the CIs we want to use here, so we can reuse this vector 
cis <- c(80, 95) 
# generate the forecast using those ci levels 
my.ts <- ts(my.result$value, frequency=52, start=c(my.start,1)) 
f <- forecast(my.ts, h=52, level=cis) 
# make a data frame containing the forecast information, including the index 
z <- as.data.frame(cbind(seq(1:52), 
        f$mean, 
        Reduce(cbind, lapply(seq_along(cis), function(i) cbind(f$lower[,i], f$upper[,i]))))) 
# give the columns better names 
names(z) <- c("index", "mean", paste(rep(c("lower", "upper"), times = length(cis)), rep(cis, each = 2), sep = ".")) 
# manipulate the results as you describe 
zw <- z %>% 
    # keep only the variable you want and its index 
    mutate(sssf = upper.95 - mean) %>% 
    select(index, mean, sssf) %>% 
    # add product and location info 
    mutate(product = my.product, 
    location = my.location) %>% 
# rearrange columns so it's easier to read 
select(product, location, index, mean, sssf) 
zw <- melt(zw, id.vars = c("product", "location", "index"), measure.vars = c("mean","sssf")) 
data.set <- cast(zw, product + location ~ index + variable, value = "value")