# NOT RUN {
some.data <- data.frame(x=rnorm(n=100))
some.data <- within(some.data,{
f <- factor(rep(1:4,each=25),labels=letters[1:4])
g <- factor(rep(1:5,each=4,5),labels=LETTERS[1:5])
y <- x + rep(1:4,each=25) + 0.75*rep(1:5,each=4,5)
})
# For demonstration purposes, we create an
# 'empty' group:
some.data <- subset(some.data,
f!="a" | g!="C")
some.grouped.data <- Groups(some.data,
~f+g)
# Computing the means of y for each combination f and g
group.means <- with(some.grouped.data,
mean(y))
group.means
# Obtaining a groupwise centered variant of y
some.grouped.data <- within(some.grouped.data,{
y.cent <- y - mean(y)
},recombine=FALSE)
# The groupwise centered variable should have zero mean
# whithin each group
group.means <- with(some.grouped.data,
round(mean(y.cent),15))
group.means
# The following demonstrates the use of n_, N_, and i_
# An external copy of y
y1 <- some.data$y
group.means.n <- with(some.grouped.data,
c(mean(y), # Group means for y
n_, # Group sizes
sum(y)/n_,# Group means for y
n_/N_, # Relative group sizes
sum(y1)/N_,# NOT the grand mean
sum(y1[i_])/n_)) # Group mean for y1
group.means.n
# Names can be attached to the groupwise results
with(some.grouped.data,
c(Centered=round(mean(y.cent),15),
Uncentered=mean(y)))
some.data.ungrouped <- recombine(some.grouped.data)
str(some.data.ungrouped)
# It all works with "data.set" objects
some.dataset <- as.data.set(some.data)
some.grouped.dataset <- Groups(some.dataset,~f+g)
with(some.grouped.dataset,
c(Mean=mean(y),
Variance=var(y)))
# The following two expressions are equivalent:
with(Groups(some.data,~f+g),mean(y))
withGroups(some.data,~f+g,mean(y))
# The following two expressions are equivalent:
some.data <- within(Groups(some.data,~f+g),{
y.cent <- y - mean(y)
y.cent.1 <- y - sum(y)/n_
})
some.data <- withinGroups(some.data,~f+g,{
y.cent <- y - mean(y)
y.cent.1 <- y - sum(y)/n_
})
# Both variants of groupwise centred varaibles should
# have zero groupwise means:
withGroups(some.data,~f+g,{
c(round(mean(y.cent),15),
round(mean(y.cent.1),15))
})
# }
Run the code above in your browser using DataLab