简体   繁体   English

我如何计算 t.-statistic 值并使用 R 找到两侧的 P 值?

[英]how do I calculate t.-statistic value and find two-sided P value using R?

Below is my dataset下面是我的数据集

dput(ex0112)

Dataset:数据集:

structure(list(BP = c(8L, 12L, 10L, 14L, 2L, 0L, 0L, -6L, 0L, 
1L, 2L, -3L, -4L, 2L), Diet = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("FishOil", "RegularOil"
), class = "factor")), class = "data.frame", row.names = c(NA, 
-14L))

To find the t-statistic for entire (column BP) I have achieved it using the below R code.为了找到整个(BP 列)的 t 统计量,我使用下面的 R 代码实现了它。

library(Sleuth3)
t.test(BP~Diet, data=ex0112)

But how do I calculate For the hypothesis that mu is zero and construct the t -statistic for (column BP) for only Regular Oil Diet and also how to Find the two-sided p-value as the proportion of values from a t-distribution farther from 0 than this value using R?但是我如何计算对于 mu 为零的假设,并为(BP 列)构建 t 统计量,仅适用于常规油性饮食,以及如何找到两侧 p 值作为来自 t 分布的值的比例使用 R 比这个值离 0 更远?

I would suggest next approach.我会建议下一个方法。 You can use one variable in t.test() which has the options for mu parameter and the two-sided alternative you want.您可以在t.test()使用一个变量,该变量具有mu参数和您想要的two-sided替代选项。 Here the code using your dput() data as df :这里使用您的dput()数据作为df的代码:

#Test
test <- t.test(df$BP[df$Diet=='RegularOil'], mu = 0, alternative = "two.sided")
test
#Extract p-value
test$p.value

Output:输出:

One Sample t-test

data:  df$BP[df$Diet == "RegularOil"]
t = -0.94943, df = 6, p-value = 0.3791
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -4.088292  1.802578
sample estimates:
mean of x 
-1.142857 

And p-val:和 p-val:

[1] 0.3790617

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM