我正试图在R中进行单样本t检验。
根据我的代码,t应为-4.979296:
sample.mean = 20
population.mean = 40
sd = 11
n = 30
t <- (sample.mean-population.mean)/(sd/sqrt(n))
然而,当我跑
时test <- c(rnorm(30, mean = 20, sd = 11))
t.test(test, mu = 40)
它不能为我提供相同的分数。
你能告诉我我做错了吗?答案 0 :(得分:1)
rnorm
每次生成随机偏差,生成值的平均值将非常接近指定的mean
但不完全相同。如果将生成的值存储在变量中并执行计算,您将看到两个值都相同。我还将使用set.seed()
,以便该示例可以为每个人重现。
set.seed(42)
test <- c(rnorm(30, mean = 20, sd = 11))
t.test(test, mu = 40)
# One Sample t-test
#data: test
#t = -7.6356, df = 29, p-value = 2.031e-08
#alternative hypothesis: true mean is not equal to 40
#95 percent confidence interval:
# 15.59947 25.90944
#sample estimates:
#mean of x
# 20.75446
mean(test)
#[1] 20.75446 #NOTE THIS IS CLOSE TO 20 BUT NOT 20
(mean(test)-40)/(sd(test)/sqrt(30))
#[1] -7.635627