# NOT RUN {
rng = function(data, ...) {
ret = range(data)
names(ret) = c("min", "max")
ret
}
# call runif(n=1), runif(n=2), runif(n=3)
# and range on the three "datasets"
# generated by runif(n=1), runif(n=2), runif(n=3)
eg = evalGrids(
expandGrid(fun="runif", n=1:3),
expandGrid(proc="rng"),
rep=10
)
eg
# summarizing the results in a data.frame
as.data.frame(eg)
# we now generate data for a regression
# and fit different regression models
# not that we use SD and not sd (the
# reason for this is the cast() call below)
regData = function(n, SD){
data.frame(
x=seq(0,1,length=n),
y=rnorm(n, sd=SD))
}
eg = evalGrids(
expandGrid(fun="regData", n=20, SD=1:2),
expandGrid(proc="lm", formula=c("y~x", "y~I(x^2)")),
replications=2)
# can not be converted to data.frame, because
# an object of class "lm" can not converted to
# a data.frame
try(as.data.frame(eg))
# for the data.frame we just extract the r.squared
# from the fitted model
as.data.frame(eg, convert.result.fun=function(fit) c(rsq=summary(fit)$r.squared))
# for the data.frame we just extract the coefficients
# from the fitted model
df = as.data.frame(eg, convert.result.fun=coef)
# since we have done 2 replication we can calculate
# sum summary statistics
library("reshape")
df$replication=NULL
mdf = melt(df, id=1:7, na.rm=TRUE)
cast(mdf, ... ~ ., c(mean, length, sd))
# note if the data.frame would contain the column
# named "sd" instead of "SD" the cast will generate
# an error
names(df)[5] = "sd"
mdf = melt(df, id=1:7, na.rm=TRUE)
try(cast(mdf, ... ~ ., c(mean, length, sd)))
# extracting the summary of the fitted.model
as.data.frame(eg, convert.result.fun=function(x) {
ret = coef(summary(x))
data.frame(valueName = rownames(ret), ret, check.names=FALSE)
})
# we now compare to methods for
# calculating quantiles
# the functions and parameters
# that generate the data
N = c(10, 50, 100)
library("plyr")
dg = rbind.fill(
expandGrid(fun="rbeta", n=N, shape1=4, shape2=4),
expandGrid(fun="rnorm", n=N))
# definition of the two quantile methods
emp.q = function(data, probs) c(quantile(data, probs=probs))
nor.q = function(data, probs) {
ret = qnorm(probs, mean=mean(data), sd=sd(data))
names(ret) = names(quantile(1, probs=probs))
ret
}
# the functions and parameters that are
# applied to the generate data
pg = rbind.fill(expandGrid(proc=c("emp.q", "nor.q"), probs=c(0.01, 0.025, 0.05)))
# generate data and apply quantile methods
set.seed(1234)
eg = evalGrids(dg, pg, replication=50, progress=TRUE)
# convert the results to a data.frame
df = as.data.frame(eg)
df$replication=NULL
mdf = melt(df, id=1:8, na.rm=TRUE)
# calculate, print and plot summary statistics
require("ggplot2")
print(a <- arrange(cast(mdf, ... ~ ., c(mean, sd)), n))
ggplot(a, aes(x=fun, y=mean, color=proc)) + geom_point(size=I(3)) + facet_grid(probs ~ n)
# }
Run the code above in your browser using DataLab