if (FALSE) {
#############################################################################
# EXAMPLE 1: Group means and standard deviations for data.ma02
#############################################################################
data(data.ma02, package="miceadds" )
dat <- data.ma02[[1]] # select first dataset
#--- group means for read and math
GroupMean( dat[, c("read","math") ], group=dat$idschool )
# using rowsum
a1 <- base::rowsum( dat[, c("read","math") ], dat$idschool )
a2 <- base::rowsum( 1+0*dat[, c("read","math") ], dat$idschool )
(a1/a2)[1:10,]
# using aggregate
stats::aggregate( dat[, c("read","math") ], list(dat$idschool), mean )[1:10,]
#--- extend group means to original dataset
GroupMean( dat[, c("read","math") ], group=dat$idschool, extend=TRUE )
# using ave
stats::ave( dat[, "read" ], dat$idschool )
stats::ave( dat[, "read" ], dat$idschool, FUN=mean )
#--- group standard deviations
GroupSD( dat[, c("read","math") ], group=dat$idschool)[1:10,]
# using aggregate
stats::aggregate( dat[, c("read","math") ], list(dat$idschool), sd )[1:10,]
#############################################################################
# EXAMPLE 2: Calculating group means and group mean centering
#############################################################################
data(data.ma07, package="miceadds")
dat <- data.ma07
# compute group means
miceadds::gm( dat$x1, dat$id2 )
# centering within clusters
miceadds::cwc( dat$x1, dat$id2 )
# evaluate formula with model.matrix
X <- model.matrix( ~ I( miceadds::cwc(x1, id2) ) + I( miceadds::gm(x1,id2) ), data=dat )
head(X)
}
Run the code above in your browser using DataLab