[英]R error on timeseries
我有一個如下的腳本
visit.total[with(visit.total, order(year, month)), ]
產生像這樣的數據幀
year month visits
1 2013 1 342145
3 2013 2 273182
5 2013 3 257748
7 2013 4 210831
9 2013 5 221381
11 2013 6 207591
13 2013 7 205367
15 2013 8 145731
17 2013 9 109211
19 2013 10 65376
21 2013 11 64409
23 2013 12 58557
2 2014 1 65307
4 2014 2 36134
6 2014 3 79041
8 2014 4 110980
10 2014 5 107926
12 2014 6 79518
14 2014 7 98927
16 2014 8 113064
18 2014 9 60171
20 2014 10 43687
22 2014 11 47601
24 2014 12 47296
當我運行此代碼時:
visit.total <- aggregate(data$visits,by=list(year=data$year,month=data$month), FUN=sum) #aggregate total visit
colnames(visit.total)[3] <- "visits"
total.visit.ts <- ts(visit.total$visits, start=c(2013,1),frequency = 12)
total.visit.ts
它給我如下結果:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2013 342145 65307 273182 36134 257748 79041 210831 110980 221381 107926 207591 79518
2014 205367 98927 145731 113064 109211 60171 65376 43687 64409 47601 58557 47296
為什么我的數據與我執行時間序列功能后的第一次數據不同? 請指教
沒有更多有關您要做什么的信息很難說,但是我想根據您的代碼,您希望獲得2013年和2014年每月出勤的時間序列。代碼的作用是R可能根據數據框的行號來排列數據。 請注意,在您的時間序列中,2013年1月的數據是正確的,但2013年2月的數據實際上是2014年1月的數據。發生的是,時間序列按行號的順序進行讀取(請參閱最左列,其中01 / 2013是第一名,而01/2014是第二名。
此代碼在我復制了您的數據框的地方應該起作用:
year <- as.numeric(c(2013, 2014))
month <- as.numeric(c(1:12))
visits <- as.numeric(c(342145, 273182, 257748, 210831, 221381, 207591, 205367, 145731, 109211, 65376, 64409, 58557,
65307, 36134, 79041, 110980, 107926, 79518, 98927, 113064, 60171, 43687, 47601, 47296))
visit.total <- merge(year, month)
colnames(visit.total) <- c("year", "month")
visit.total <- visit.total[order(visit.total$year, visit.total$month), ]
visit.total <- cbind(visit.total, visits)
visit.total.ts <- ts(visit.total$visits, start = c(2013, 1), end = c(2014, 12), frequency = 12)
您應該看到按月和按年正確安排了每月訪問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.