Reputation: 1
I'm trying to comput t-test columunwise and fixe the true value of mean (mu
) to 0.9. The follwing is my code chunk.
data set
vec1 <- c(.5,.1, .03, .2, .09, .02,.3,.4,.6,.8)
vec2 <- c(5, 4,3, 8, 3, 1, 4,1, 10, 5)
vec3 <- round(rnorm(10), digits = 2)
tab <- cbind(vec1, vec2, vec3)
tab
code
I used the following apply
function
k <- apply(tab, 2, t.test)
k
But this do not allow to fixe the mu
. It computs the t-test with the default mu
.
Then, I tried to create a loop like this:
for (i in 1:ncol(tab)) {
h=list()
h[[i]]=t.test(tab[,i], mu=0.9)
print(h)
}
I got a list of length 3 as I want. But something strange is happening in the output.
h [[1]]
One Sample t-test
data: tab[, i] t = -7.0941, df = 9, p-value = 5.703e-05 alternative hypothesis: true mean is not equal to 0.9 95 percent confidence interval: 0.1139489 0.4940511 sample estimates: mean of x 0.304
[[1]] NULL
[[2]]
One Sample t-test
data: tab[, i] t = 3.9023, df = 9, p-value = 0.003607 alternative hypothesis: true mean is not equal to 0.9 95 percent confidence interval: 2.371053 6.428947 sample estimates: mean of x 4.4
[[1]] NULL
[2]] NULL
[[3]]
One Sample t-test
data: tab[, i] t = -2.6103, df = 9, p-value = 0.02826 alternative hypothesis: true mean is not equal to 0.9 95 percent confidence interval: -0.5485048 0.7965048 sample estimates: mean of x 0.124
With is output, when I aske for the first or second element from the list like this :
h[[1]]
h[[2]]
I get
NULL as result.
What is going wrong in my code?
Upvotes: 0
Views: 40
Reputation: 10637
You can use partial evaluation of the function t.test
to overwrite default arguments like mu
:
vec1 <- c(.5,.1, .03, .2, .09, .02,.3,.4,.6,.8)
vec2 <- c(5, 4,3, 8, 3, 1, 4,1, 10, 5)
vec3 <- round(rnorm(10), digits = 2)
tab <- cbind(vec1, vec2, vec3)
tab
#> vec1 vec2 vec3
#> [1,] 0.50 5 0.84
#> [2,] 0.10 4 1.48
#> [3,] 0.03 3 -0.93
#> [4,] 0.20 8 -0.43
#> [5,] 0.09 3 -2.91
#> [6,] 0.02 1 0.33
#> [7,] 0.30 4 -0.90
#> [8,] 0.40 1 -0.20
#> [9,] 0.60 10 0.75
#> [10,] 0.80 5 0.74
apply(tab, 2, purrr::partial(t.test, mu = 0.9))
#> $vec1
#>
#> One Sample t-test
#>
#> data: newX[, i]
#> t = -7.0941, df = 9, p-value = 5.703e-05
#> alternative hypothesis: true mean is not equal to 0.9
#> 95 percent confidence interval:
#> 0.1139489 0.4940511
#> sample estimates:
#> mean of x
#> 0.304
#>
#>
#> $vec2
#>
#> One Sample t-test
#>
#> data: newX[, i]
#> t = 3.9023, df = 9, p-value = 0.003607
#> alternative hypothesis: true mean is not equal to 0.9
#> 95 percent confidence interval:
#> 2.371053 6.428947
#> sample estimates:
#> mean of x
#> 4.4
#>
#>
#> $vec3
#>
#> One Sample t-test
#>
#> data: newX[, i]
#> t = -2.5636, df = 9, p-value = 0.03051
#> alternative hypothesis: true mean is not equal to 0.9
#> 95 percent confidence interval:
#> -1.0257116 0.7797116
#> sample estimates:
#> mean of x
#> -0.123
Created on 2021-11-09 by the reprex package (v2.0.1)
Upvotes: 0