statByIndex               package:plw               R Documentation

_C_o_m_p_u_t_e_s _s_t_a_t_i_s_t_i_c_s _b_y _i_n_d_e_x _o_r _b_y _r_o_w

_D_e_s_c_r_i_p_t_i_o_n:

     These function give the same result as  by(x,index,mad)
     by(x,index,mean) by(x,index,median) but are much faster. NOTE: The
     index vector is assumed to be SORTED and should contain INTEGER
     values only.

     The function meanSdByRow computes mean and standard deviation for
     each row of the matrix mat. A list with mean and sd is returned
     and gives the the same result as:

     list(mean=apply(mat,1,mean),sd=apply(mat,1,sd))

_U_s_a_g_e:

     madByIndex(x,index)
     meanByIndex(x,index)
     medianByIndex(x,index)
     orderStatByIndex(x,index,orderStat)
     sdByIndex(x,index)
     meanSdByRow(mat)

_A_r_g_u_m_e_n_t_s:

       x: Data vector

   index: Index vector

orderStat: Which order statistic to compute

     mat: Matrix

_D_e_t_a_i_l_s:

     See the definition (R-code) of each function for details.

_V_a_l_u_e:

     All but the last function: A vector with the statistic for each
     level if index. meanSdByRow: A list with items mean and sd.

_A_u_t_h_o_r(_s):

     Magnus Astrand

_S_e_e _A_l_s_o:

     by, apply

_E_x_a_m_p_l_e_s:

     ## Example 1
     ## Computing, mad, mean and median by index.
     ## Compares with the result obtained using by(...) 

     n<-10000
     x<-rnorm(n)
     index<-sort(round(runif(n,0.5,10.5)))

     mad1<-madByIndex(x,index)
     mad2<-by(x,index,mad)

     mean1<-meanByIndex(x,index)
     mean2<-by(x,index,mean)

     median1<-medianByIndex(x,index)
     median2<-by(x,index,median)

     par(mfrow=c(2,2),mar=c(4,4,1.5,.5),mgp=c(1.5,.25, 0))
     plot(mad1,mad2,main="Comparing mad",pch=19)
     abline(a=0,b=1,col=2)
     plot(mean1,mean2,main="Comparing mean",pch=19)
     abline(a=0,b=1,col=2)
     plot(median1,median2,main="Comparing median",pch=19)
     abline(a=0,b=1,col=2)

     ## Example 2
     ## Computing, median by index
     ## Compares with the running time when using by(...)
     n<-200000
     x<-rnorm(n)
     index<-sort(round(runif(n,0.5,10.5)))

     system.time(median1<-medianByIndex(x,index))

     system.time(median2<-by(x,index,median))

     ## Example 3
     ## Computing, mean and sd by row 
     ## Compares with using apply
     nrow<-5000
     ncol<-20
     mat<-matrix(rnorm(ncol*nrow),nrow,ncol)

     system.time(res1<-meanSdByRow(mat))
     system.time(res2<-list(mean=apply(mat,1,mean),sd=apply(mat,1,sd)))

     par(mfrow=c(1,2),mar=c(4,4,1.5,.5),mgp=c(1.5,.25, 0))
     plot(res1$mean,res2$mean,pch='.')
     plot(res1$sd,res2$sd,pch='.')

