Rの時系列の基礎処理、週ごと、月ごとの平均やそのプロット(ggplot2)のサンプル
q2<-read.csv("siawase_ss.csv",header=F) z<-q2[,3][1:length(medi1_food)]/medi1_food plot(q2[,3]/medi1_food[1:length(q2[,3])],type="l") t1<-as.Date("2006-10-31")+1:length(z) #month<-as.Date(months(t1)) month<-as.integer(format(t1,"%b")) date1<-as.integer(format(t1,"%d")) date0<-as.integer(format(t1,"%j")) week1<-as.integer(format(t1,"%w")) #z<-factor(month) v<-list() n<-list() for(i in 1:length(z)){ v[[ paste(month[i]) ]]<-c(z[i],v[[ paste(month[i]) ]]) n[[ paste(month[i]) ]]<-c(1,n[[ paste(month[i]) ]]) } medi_month<-NULL mean_month<-NULL q05_month<-NULL q95_month<-NULL week1<-NULL for(i in 1:12){ medi_month[[paste(i)]]<-median(v[[paste(i)]]) mean_month[[paste(i)]]<-mean(v[[paste(i)]]) q05_month[[paste(i)]]<-quantile(v[[paste(i)]],0.25) q95_month[[paste(i)]]<-quantile(v[[paste(i)]],0.75) } #plot(medi_month,type="b") qplot(1:length(medi_month),medi_month,geom=c("line","point"),cex.axis=1.5,xlab="Month",ylab="Median of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=seq(1,12)) qplot(1:length(mean_month),mean_month,geom=c("line","point"),cex.axis=1.5,xlab="Month",ylab="Mean of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=seq(1,12)) #plot(medi_month,type="b",ylim=c(1,30000)) points(q05_month,col=2) points(q95_month,col=3) ####Hiduke################################ v<-list() n<-list() v_Dec<-list() n_Dec<-list() v_Dec_Jan<-list() v_Dec_Jan_week<-list() n_Dec_Jan<-list() v_Dec_Jan_Dec<-list() n_Dec_Jan_Dec<-list() v_week<-list() for(i in 1:length(z)){ v[[ paste(date1[i]) ]]<-c(z[i],v[[ paste(date1[i]) ]]) n[[ paste(date1[i]) ]]<-c(1,n[[ paste(date1[i]) ]]) if(month[i]!=12){ v_Dec[[paste(date1[i])]]<-c(z[i],v_Dec[[paste(date1[i])]]) n_Dec[[paste(date1[i])]]<-c(n[i],n_Dec[[paste(date1[i])]]) } if(!(month[i]==12 | month[i]==1)){ v_Dec_Jan[[paste(date1[i])]]<-c(z[i],v_Dec_Jan[[paste(date1[i])]]) n_Dec_Jan[[paste(date1[i])]]<-c(n[i],n_Dec_Jan[[paste(date1[i])]]) zz<-z[i]/week_weight[week1[i]+1]*mean(week_weight) v_Dec_Jan_week[[paste(date1[i])]]<-c(zz,v_Dec_Jan_week[[paste(date1[i])]]) } if(!(month[i]==12 | month[i]==1 | month[i]==2)){ v_Dec_Jan_Feb[[paste(date1[i])]]<-c(z[i],v_Dec_Jan_Feb[[paste(date1[i])]]) n_Dec_Jan_Feb[[paste(date1[i])]]<-c(n[i],n_Dec_Jan_Feb[[paste(date1[i])]]) } zz<-z[i]*week_weight[week1[i]+1]/mean(week_weight) v_week[[paste(date1[i])]]<-c(zz,v_week[[paste(date1[i])]]) #n_Dec_Jan_Feb[[paste(date1[i])]]<-c(n[i],n_Dec_Jan_Feb[[paste(date1[i])]]) } medi_month<-NULL mean_month_week<-NULL mean_month_01<-NULL mean_month_Dec<-NULL mean_month_Dec_Jan<-NULL mean_month_Dec_Jan_01<-NULL mean_month_Dec_Jan_01_week<-NULL mean_month_Dec_Jan<-NULL mean_month_Dec_Jan_Feb<-NULL medi_month_week<-NULL q05_month<-NULL q95_month<-NULL for(i in 1:length(v)){ medi_month[[paste(i)]]<-median(v[[paste(i)]]) mean_month[[paste(i)]]<-mean(v[[paste(i)]]) mean_month_01[[paste(i)]]<-mean(v[[paste(i)]],trim=0.1) q05_month[[paste(i)]]<-quantile(v[[paste(i)]],0.25) q95_month[[paste(i)]]<-quantile(v[[paste(i)]],0.75) mean_month_Dec[[paste(i)]]<-mean(v_Dec[[paste(i)]]) mean_month_Dec_Jan[[paste(i)]]<-mean(v_Dec_Jan[[paste(i)]]) mean_month_Dec_Jan_01[[paste(i)]]<-mean(v_Dec_Jan[[paste(i)]],trim=0.1) mean_month_Dec_Jan_01_week[[paste(i)]]<-mean(v_Dec_Jan_week[[paste(i)]],trim=0.1) mean_month_Dec_Jan_Feb[[paste(i)]]<-mean(v_Dec_Jan_Feb[[paste(i)]]) medi_month_week[[paste(i)]]<-median(v_week[[paste(i)]]) } #plot(medi_month,type="b") qplot(1:length(medi_month),medi_month,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Median of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=1:31,labels=1:31) qplot(1:length(mean_month),mean_month,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Mean of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=1:31,labels=1:31) qplot(1:length(mean_month_Dec),mean_month_Dec,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Mean of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=1:31,labels=1:31) qplot(1:length(mean_month_Dec_Jan),mean_month_Dec_Jan,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Mean of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=1:31,labels=1:31) qplot(1:length(mean_month_Dec_Jan_Feb),mean_month_Dec_Jan_Feb,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Mean of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=1:31,labels=1:31) qplot(1:length(medi_month_week),medi_month_week,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Median of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=1:31,labels=1:31) qplot(1:length(mean_month_01),mean_month_01,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Median of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=1:31,labels=1:31) qplot(1:length(mean_month_Dec_Jan_01),mean_month_Dec_Jan_01,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Mean of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=1:31,labels=1:31) qplot(1:length(mean_month_Dec_Jan_01_week),mean_month_Dec_Jan_01_week,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Mean of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=1:31,labels=1:31) #plot(medi_month,type="b",ylim=c(1,30000)) #points(q05_month,col=2) #points(q95_month,col=3) #v<-ts(z,frequency=365.25,start=as.Date("2006-11-01")) #v<-ts(z,frequency=365.25,start=as.Date("2006-11-01")) ######## ####Week v<-list() n<-list() for(i in 1:length(z)){ v[[ paste(week1[i]) ]]<-c(z[i],v[[ paste(week1[i]) ]]) n[[ paste(week1[i]) ]]<-c(1,n[[ paste(week1[i]) ]]) } medi_month<-NULL mean_month<-NULL q05_month<-NULL q95_month<-NULL week_weight<-NULL for(i in 1:length(v)){ medi_month[[paste(i-1)]]<-median(v[[paste(i-1)]]) mean_month[[paste(i-1)]]<-mean(v[[paste(i-1)]]) q05_month[[paste(i-1)]]<-quantile(v[[paste(i-1)]],0.05) q95_month[[paste(i-1)]]<-quantile(v[[paste(i-1)]],0.95) week_weight[i]<-mean(v[[paste(i-1)]],trim=0.05) } qplot(1:length(medi_month),medi_month,geom=c("line","point"),cex.axis=1.5,xlab="Week",ylab="Median of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=c(1,2,3,4,5,6,7),labels=c("日","月","火","水","木","金","土")) qplot(1:length(mean_month),mean_month,geom=c("line","point"),cex.axis=1.5,xlab="Week",ylab="Mean of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=c(1,2,3,4,5,6,7),labels=c("日","月","火","水","木","金","土")) plot(medi_month,type="b",ylim=c(min(q05_month),max(q95_month))) points(q05_month,col=2) points(q95_month,col=3) #v<-ts(z,frequency=365.25,start=as.Date("2006-11-01")) medi_month<-NULL mean_month<-NULL q05_month<-NULL q95_month<-NULL for(i in 1:12){ medi_month[[paste(i)]]<-median(v[[paste(i)]]) mean_month[[paste(i)]]<-mean(v[[paste(i)]]) q05_month[[paste(i)]]<-quantile(v[[paste(i)]],0.25) q95_month[[paste(i)]]<-quantile(v[[paste(i)]],0.75) } #plot(medi_month,type="b") qplot(1:length(medi_month),medi_month,geom=c("line","point"),cex.axis=1.5,xlab="Month",ylab="Mean of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=seq(1,12)) qplot(1:length(mean_month),mean_month,geom=c("line","point"),cex.axis=1.5,xlab="Month",ylab="Median of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=seq(1,12)) #plot(medi_month,type="b",ylim=c(1,30000)) points(q05_month,col=2) points(q95_month,col=3) v<-list() n<-list() for(i in 1:length(z)){ v[[ paste(date0[i]) ]]<-c(z[i],v[[ paste(date0[i]) ]]) n[[ paste(date0[i]) ]]<-c(1,n[[ paste(date0[i]) ]]) } medi_month<-NULL mean_month<-NULL q05_month<-NULL q95_month<-NULL for(i in 1:length(v)){ medi_month[[paste(i-1)]]<-median(v[[paste(i-1)]]) mean_month[[paste(i-1)]]<-mean(v[[paste(i-1)]]) q05_month[[paste(i-1)]]<-quantile(v[[paste(i-1)]],0.05) q95_month[[paste(i-1)]]<-quantile(v[[paste(i-1)]],0.95) } qplot(1:length(medi_month),medi_month,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Median of Count")+theme_bw(base_size=18)+scale_x_discrete(breaks=1:365,labels=c(1:365)) #qplot(as.Date("2005-12-31")+1:length(medi_month),medi_month,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Median of Count")+theme_bw(base_size=18)+ scale_x_date(labels = date_format("%m-%d")) +xlim(c(as.Date("2006-06-15"),as.Date("2006-07-30")))+ylim(c(15000,20000)) v1<-qplot(as.Date("2005-12-31")+1:length(medi_month),medi_month,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Median of Count")+theme_bw(base_size=18)+ scale_x_date(labels = date_format("%m-%d")) +xlim(c(as.Date("2006-02-01"),as.Date("2006-02-25")))+ylim(c(15000,19000)) v1<-qplot(as.Date("2005-12-31")+1:length(medi_month),medi_month,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Median of Count")+theme_bw(base_size=18)+ scale_x_date(labels = date_format("%m-%d")) +xlim(c(as.Date("2006-02-01"),as.Date("2006-02-25")))+ylim(c(15000,19000)) v2<-v1+geom_line(aes(as.Date("2005-12-31")+1:length(medi_month),medi_month)) print(v2) breaks<-as.Date() qplot(as.Date("2005-12-31")+1:length(mean_month),mean_month,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Mean of Count")+theme_bw(base_size=18)+ scale_x_date(breaks=as.Date(paste("2006-",1:12,"-01",sep="")),labels = date_format("%m")) #qplot(as.Date("2005-12-31")+1:length(mean_month),mean_month,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Mean of Count")+theme_bw(base_size=18)+ scale_x_date(labels = date_format("%m-%d")) +xlim(c(as.Date("2006-06-15"),as.Date("2006-07-30")))+ylim(c(15000,20000)) qplot(as.Date("2005-12-31")+1:length(mean_month),mean_month,geom=c("line","point"),cex.axis=1.5,xlab="Date",ylab="Mean of Count")+theme_bw(base_size=18)+ scale_x_date(labels = date_format("%m-%d")) +xlim(c(as.Date("2006-02-01"),as.Date("2006-02-25")))+ylim(c(15000,20000)) mean_monthb<-mean_month[-1] write.table(data.frame(1:364, (as.Date("2005-12-31")+1+1:length(mean_monthb[-1]) )[order(mean_monthb[-1]/mean_monthb[-length(mean_monthb)],decreasing=T)], sort(mean_monthb[-1]/mean_monthb[-length(mean_monthb)],decreasing=T) ),row=F) medi_monthb<-medi_month write.table(data.frame(1:364, (as.Date("2005-12-31")+1+1:length(medi_monthb[-1]) )[order(medi_monthb[-1]/medi_monthb[-length(medi_monthb)],decreasing=T)], sort(medi_monthb[-1]/medi_monthb[-length(medi_monthb)],decreasing=T) ),row=F)