Hi again
If you are interested in cross-validation with R and in understanding of algo, please review the code below
library(dplyr)
library(nlme)
Subj=as.factor(rep(1:20, each = 2))
Seq=as.factor(c(rep(c("TR","RT"), each = 6),
rep(c("RT","TR"), each = 6),
rep(c("TR","RT"), each = 4),
rep(c("RT","TR"), each = 4)))
Per=as.factor((rep(c(1,2), 20)))
Trt=as.factor(c(rep(c(1,2), 6),
rep(c(2,1), 6),
rep(c(1,2), 4),
rep(c(2,1), 4)))
Stg=as.factor(c(rep(1,24),
rep(2,16)))
PK=c(4.8, 4.6, 1.4, 1.3, 3.9, 4.4, 1.5, 2.4, 3.8, 2.9, 3.3, 2.5, 1.7, 1.5, 1.3, 1.9, 2.1, 2.4, 11.4, 13.9, 1.5, 2.3, 2.6, 2.4,
4.0,4.2,0.7, 0.5, 10.3, 11.9,4.7, 4.8, 5.9,5.5, 3.8, 5.4, 4.3, 2.1, 3.6, 2.4)
M=lm(PK ~ Seq + Stg/Per + Stg:Seq/Subj + Trt)
newdat <- expand.grid(Trt = levels(Trt), Seq = levels(Seq), Subj = levels(Subj), Per = levels(Per), Stg = levels(Stg))
preddata <- cbind(newdat, predict(M, newdat))
dta <- cbind.data.frame(Trt, Seq, Subj, Per, Stg, PK)
preddatawoNA <- na.omit(left_join(preddata, dta))
preddatawoNA%>%
group_by(Trt, Seq, Per, Stg) %>%
summarize(Subjmean = mean(`predict(M, newdat)`)) %>%
group_by(Trt) %>%
summarize(lsmean = mean(Subjmean))
All the best,
Mittyright