x軸で等間隔.
discretize関数を使う.
library(infotheo) > x<-rnorm(1000) > y<-2*x+0.01*rnorm(1000) > l1<-discretize(x,disc="equalwidth",nbins=15) > tapply(x,l1[[1]],"mean") 1 2 3 4 5 6 -2.87785661 -2.50220873 -2.05626538 -1.63706018 -1.23389800 -0.82092985 7 8 9 10 11 12 -0.39442093 0.02223587 0.43716950 0.84044260 1.24452194 1.69533068 13 14 15 2.08133816 2.56408614 2.99144001 > tapply(y,l1[[1]],"mean") 1 2 3 4 5 6 -5.75150547 -5.00125774 -4.11329206 -3.27428585 -2.46890734 -1.64181904 7 8 9 10 11 12 -0.78788607 0.04436817 0.87491706 1.68059046 2.48817460 3.39174340 13 14 15 4.16523638 5.12734043 5.98799473
等頻度で分割
> l1 <- discretize(x, disc="equalfreq") > tapply(x,l1[[1]],"mean") 1 2 3 4 5 -1.721129572 -0.983218310 -0.586808704 -0.267970223 0.002757198 6 7 8 9 0.271883811 0.580655952 0.973125901 1.728366027 > tapply(y,l1[[1]],"mean") 1 2 3 4 5 -3.442330531 -1.966558533 -1.173101432 -0.535350363 0.004887793 6 7 8 9 0.544374761 1.162258190 1.945280966 3.457134216