我正试着在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)它没有给我提供相同的t分数。
你能告诉我我哪里做错了吗?
发布于 2017-04-11 10:18:15
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.635627https://stackoverflow.com/questions/43335596
复制相似问题