R语言-配对t检验

2021-12-22  本文已影响0人  超级可爱的懂事长鸭

准备data demo

rm(list=ls())
install.packages("datarium")
data("mice2", package = "datarium")
# Transform into long data: 
# gather the before and after values in the same column
library(tidyr)
mice2.long <- mice2 %>%
  gather(key = "group", value = "weight", before, after)
head(mice2.long, 3)

方法一:把配对的数据分别保存在两个数值型向量里(确保数值的配对位置)

before <- mice2$before
after <- mice2$after
# Compute t-test
res <- t.test(before, after, paired = TRUE)
res

方法二:把配对的数据分别保存在一个数据框里(确保数值的配对位置)

# Compute t-test
res <- t.test(weight ~ group, data = mice2.long, paired = TRUE)
res
mice2_dataframe.png

结果解析

Paired t-test

data:  weight by group
t = 25.546, df = 9, p-value = 1.039e-09   #检验统计量,自由度,P值
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:  #95%可信区间
 181.8158 217.1442
sample estimates:
mean of the differences 
                 199.48 

https://www.datanovia.com/en/lessons/how-to-do-a-t-test-in-r-calculation-and-reporting/how-to-do-paired-t-test-in-r/
https://www.statology.org/paired-samples-t-test-r/

此处加个私货推荐,果子老师的统计方法整理,看了之后能解决大部分问题

https://mp.weixin.qq.com/s?__biz=MzIyMzA2MTcwMg==&mid=2650733796&idx=1&sn=76b2b657f96290be622337c4a10d3036&scene=21#wechat_redirect
https://mp.weixin.qq.com/s/IF4F0W2ghWRq4ILVP3T49A

上一篇 下一篇

猜你喜欢

热点阅读