In [1]:
require('ggplot2')
require('reshape')
In [2]:
trials <- 100
In [3]:
epsilons <- seq(0, 0.2, 0.01)
epsilons
Out[3]:
In [4]:
rcontnorm <- function(n=1, mean=0, sd=1, eps=0.1, sdc=10) {
# Generate random numbers distributed according to a contaminated normal distribution.
#
# Arguments:
# n: number of observations.
# mean: vector of means.
# sd: vector of standard deviations for base.
# eps: factor of contamination.
# sdc: vector of standard deviations for contamination.
t_choice <- rbinom(n, 1, 1-eps)
t_norm <- rnorm(n)
t_contam <- rnorm(n, sd=sdc)
return (t_choice * t_norm + (1-t_choice) * t_contam)
}
In [15]:
df <- data.frame(trial=1:trials, sapply(epsilons, rcontnorm, n=trials, mean=0, sd=1, sdc=10))
colnames(df)[2:(length(epsilons)+1)] <- epsilons
melt_df <- melt(df, id.vars='trial', variable_name='eps')
In [16]:
ggplot(melt_df, aes(trial, value, color=eps)) + geom_point()
In [20]:
colMeans(df)
Out[20]:
In [ ]: