2017-01-05 2 views
0

으로 변환 fpp 패키지를 사용하여 여러 고객의 여러 시간 시리즈를 동시에 예측합니다. 나는 이미 다른 쉬운 예측 방법 (snaive, meanf, 등)의 포인트 예측을 csv 문서로 추출 할 수 있습니다. 그러나, 나는 아직도 CSV 파일에 모든 시간 시리즈의 accuracy() 명령의 측정 값을 동시에 추출하는 방법을 찾으려고합니다.여러 시간 검색어의 수출 정확도를 CSV- 문서

# loading of the "fpp"-package into R 
install.packages("fpp") 
require("fpp") 

# Example customers 
customer1 <- c(0,3,1,3,0,5,1,4,8,9,1,0,1,2,6,0) 
customer2 <- c(1,3,0,1,7,8,2,0,1,3,6,8,2,5,0,0)  
customer3 <- c(1,6,9,9,3,1,5,0,5,2,0,3,2,6,4,2) 
customer4 <- c(1,4,8,0,3,5,2,3,0,0,0,0,3,2,4,5) 
customer5 <- c(0,0,0,0,4,9,0,1,3,0,0,2,0,0,1,3) 

#constructing the timeseries 
all <- ts(data.frame(customer1,customer2,customer3,customer4,customer5), 
      f=12, start=2015)  
train <- window(all, start=2015, end=2016-0.01) 
test <- window(all, start=2016) 
CustomerQuantity <- ncol(train) 

# Example of extracting easy forecast method into csv-document 
horizon <- 4 
fc_snaive <- matrix(NA, nrow=horizon, ncol=CustomerQuantity) 
for(i in 1:CustomerQuantity){   
    fc_snaive [,i] <- snaive (train[,i], h=horizon)$mean 
} 
write.csv2(fc_snaive, file ="fc_snaive.csv") 

다음 부분은 정확히 내가 어떤 도움을 필요로 할 부분이다 - 나는 동시에 모든 csv 파일로 정확성-조치를 추출하고 싶습니다 :

나는 예를 건설했다. 내 실제 데이터 세트에는 4000 명의 고객이 있으며 5만이 아닙니다! 나는 루프와 lapply()을 사용하려했지만 불행히도 내 코드가 작동하지 않았다.

accuracy(fc_snaive[,1], test[,1]) 
accuracy(fc_snaive[,2], test[,2]) 
accuracy(fc_snaive[,3], test[,3]) 
accuracy(fc_snaive[,4], test[,4]) 
accuracy(fc_snaive[,5], test[,5]) 

답변

2

다음은 test의 해당 요소와 fc_snaive의 열의 수 (1)로부터 각 요소 accuracy를 실행 lapply을 이용한다.

그러면 do.call으로 결과를 행 (rbind)으로 바인딩하므로 결국 write.csv을 사용하여 내보낼 수있는 행렬로 끝납니다.

new_matrix <- do.call(what = rbind, 
         args = lapply(1:ncol(fc_snaive), function(x){ 
         accuracy(fc_snaive[, x], test[, x]) 
         })) 

write.csv(x = new_matrix, 
      file = "a_filename.csv") 
+0

고맙습니다. Juan :) !!! 코드가 완벽하게 작동합니다! 새 csv.file이 작성된 줄에는 하나의 작은 오타가 있습니다. "x = new_matrix"대신 "x = new.matrix"를 사용하십시오. – Jo91

+0

오타를 지적하여 주셔서 감사합니다! –