I'm trying to use this function I made
ev <- function(x, mu0 = y, alt = "two-sided", lev = 0.95) {
ts<- ((mean(x)-mu0)/(sd(x)/sqrt(length(x))))
if(alt == "two-sided") {
pval <- 2*pt(ts, length(x-1))
ci<- c((mu0- pval*(length(x)/ts)), mu0 pval*(length(x)/ts))
}
else if(alt == "greater"){
pval <- (1- pt(ts, length(x-1)))
ci<- c((mu0- pval*(length(x)/ts)), mu0 pval*(length(x)/ts))
}
else if(alt == "less") {
pval <- pt(ts, length(x-1))
ci<- c((mu0- pval*(length(x)/ts)), mu0 pval*(length(x)/ts))
}
return(list(c(ts,pval, ci)))
}
to solve the following problem.
I created the datasets using
g<- matrix(rnorm(5000*20),nrow = 20, ncol = 5000)
my.list<- split(g, rep(1:ncol(g), each= nrow(g)))
But am a little confused about what to do from here. I tried just plugging in my.list, but that gives me this error
ev(my.list,0.2,"less")
Error in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) :
is.atomic(x) is not TRUE
In addition: Warning message:
In mean.default(x) :
Any help to get me going would be appreciated. I'm also aware my formulas are probably wrong, but I want to focus on at least getting some numbers back before worrying about if they're right or wrong. I will give any more information you need if you ask.
CodePudding user response:
First, I would just have it return a vector, so something like:
return(c(ts = ts,
pval = pval,
lwr = ci[1],
upr = ci[2]))
Then, you could do something like the following:
res <- t(sapply(1:5000, function(x)ev(rnorm(20,0,1), mu0=0, alt="two-sided")))
head(res)
ts pval lwr upr
[1,] 1.8754644 1.9245952 -20.160528 20.887336
[2,] 0.8501353 1.5946874 -37.338262 37.693908
[3,] 1.0377915 1.6882478 -32.304612 32.766179
[4,] -1.3111520 0.2046614 2.856899 -3.386813
[5,] 1.5704836 1.8680108 -23.406800 24.171175
[6,] 0.7584952 1.5429994 -40.459755 40.911854
As someone who teaches stats, I don't want to just give the answers to you, but I would encourage you to look at a couple of things. First, make sure that the confidence intervals are calculated in the way you intended. Second, make sure that the p-value calculations are as you intend, particularly for the two-sided test. You'll want to make sure that the results you do get make sense.