使用 R 向量化 t.test(x, m = hu) 函数中的平均值
Vectorize the Mean Value in t.test(x, m = hu) Function Using R
我有一个向量,我想在其中向量化一组假设平均值。
这就是我的意思,如果我有以下情况:
n <- c(10,15,20,25,30,40,50,100,250) # Sample size
m <- 100 # Mean of the generated normal variable
vv <- 25 # variance of the generated normal variable
s <- sqrt(vv)
x <- rnorm(n, m, s)
hu <- c(85, 87.5, 90, 92.5, 95, 97.5, 100, 102.5, 105, 107.5, 110, 112.5, 115) # Hypothesized value of Mean of the generated normal variable
t.test(x, m = 115)$p.value # here I chose a scalar hypothesised mean value
# [1] 2.080525e-07
但是当m是c(85, 87.5, 90, 92.5, 95, 97.5, 100, 102.5, 105, 107.5, 110, 112.5, 115)
的向量时
t.test(x, m = hu)$p.value # here I chose a vector of hypothesised mean value
# Error in t.test.default(x, m = hu) : 'mu' must be a single number
我收到上面的错误信息。
我想要什么
我想 运行 t.test(x, m = hu)$p.value
其中 hu
是 c(85, 87.5, 90, 92.5, 95, 97.5, 100, 102.5, 105, 107.5, 110, 112.5, 115)
的向量,这样我就会有一个 vector output
我们可以使用循环,因为 mu
in ?t.test
将单个值作为输入
mu - a number indicating the true value of the mean (or difference in means if you are performing a two sample test).
sapply(hu, function(u) t.test(x, mu = u)$p.value)
-输出
[1] 1.053213e-05 3.227459e-05 1.161963e-04 5.126058e-04 2.903163e-03 2.146785e-02 1.777964e-01 9.192412e-01 2.403805e-01 2.966405e-02 3.873822e-03 6.563008e-04
[13] 1.436296e-04
我有一个向量,我想在其中向量化一组假设平均值。
这就是我的意思,如果我有以下情况:
n <- c(10,15,20,25,30,40,50,100,250) # Sample size
m <- 100 # Mean of the generated normal variable
vv <- 25 # variance of the generated normal variable
s <- sqrt(vv)
x <- rnorm(n, m, s)
hu <- c(85, 87.5, 90, 92.5, 95, 97.5, 100, 102.5, 105, 107.5, 110, 112.5, 115) # Hypothesized value of Mean of the generated normal variable
t.test(x, m = 115)$p.value # here I chose a scalar hypothesised mean value
# [1] 2.080525e-07
但是当m是c(85, 87.5, 90, 92.5, 95, 97.5, 100, 102.5, 105, 107.5, 110, 112.5, 115)
的向量时
t.test(x, m = hu)$p.value # here I chose a vector of hypothesised mean value
# Error in t.test.default(x, m = hu) : 'mu' must be a single number
我收到上面的错误信息。
我想要什么
我想 运行 t.test(x, m = hu)$p.value
其中 hu
是 c(85, 87.5, 90, 92.5, 95, 97.5, 100, 102.5, 105, 107.5, 110, 112.5, 115)
的向量,这样我就会有一个 vector output
我们可以使用循环,因为 mu
in ?t.test
将单个值作为输入
mu - a number indicating the true value of the mean (or difference in means if you are performing a two sample test).
sapply(hu, function(u) t.test(x, mu = u)$p.value)
-输出
[1] 1.053213e-05 3.227459e-05 1.161963e-04 5.126058e-04 2.903163e-03 2.146785e-02 1.777964e-01 9.192412e-01 2.403805e-01 2.966405e-02 3.873822e-03 6.563008e-04
[13] 1.436296e-04