# DISCARD DATA MORE THAN K STANDARD DEVIATIONS FROM MEAN. Takes a
# data vector and k as its arguments, and returns the data vector in
# which all points more than k times the sample standard deviation
# from the sample mean have been discarded.
trim.data <- function (x, k)
{
m <- mean(x)
s <- sd(x)
x[abs(x-m)<=k*s]
}
# CHECK THE EFFECT OF TRIMMING DATA ON THE VALIDITY OF THE T TEST.
# Simulates the distribution of p-values for a t test when the null
# hypothesis is true, but the data is trimmed at k standard deviations
# before doing the test. The sample size is given as the second argument,
# and the number of data sets to simulate as the third argument.
sim.trim <- function (k, n, r)
{
p.values <- numeric(r)
for (i in 1:r)
{ x <- trim.data(rnorm(n),k)
p.values[i] <- t.test(x)$p.value
}
p.values
}