# genss: generate SS for model and error from repeated simulations genss <- function(mu=c(1,2,3),n=4,sigma=1,trial=50) { param_cbind(mu,sigma,n) warning(paste(trial,"trials with",sum(param[,3]),"variates", "will take time!") gennorm <- function(x) { y <- rnorm(x[3],x[1],x[2]) c(mean(y),var(y),x[3]) } ss <- matrix(0,trial,2) dimnames(ss) <- list(NULL,c("SSmodel","SSerror")) for (i in 1:trial) { stats <- apply(param,1,gennorm) grmean <- sum(stats[3,] * stats[1,]) / sum(stats[3,]) ss[i,"SSmodel"] <- sum(stats[3,] * (stats[1,] - grmean) ^ 2) ss[i,"SSerror"] <- sum((stats[3,]-1) * stats[2,]) } ss }