準(zhǔn)備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)
方法一:把配對(duì)的數(shù)據(jù)分別保存在兩個(gè)數(shù)值型向量里(確保數(shù)值的配對(duì)位置)
before <- mice2$before
after <- mice2$after
# Compute t-test
res <- t.test(before, after, paired = TRUE)
res
方法二:把配對(duì)的數(shù)據(jù)分別保存在一個(gè)數(shù)據(jù)框里(確保數(shù)值的配對(duì)位置)
# Compute t-test
res <- t.test(weight ~ group, data = mice2.long, paired = TRUE)
res
結(jié)果解析
Paired t-test
data: weight by group
t = 25.546, df = 9, p-value = 1.039e-09 #檢驗(yàn)統(tǒng)計(jì)量鲫售,自由度荤傲,P值
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval: #95%可信區(qū)間
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/
此處加個(gè)私貨推薦,果子老師的統(tǒng)計(jì)方法整理蚓庭,看了之后能解決大部分問(wèn)題
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