组内的一个样本T-TEST IN R [英] One sample T-TEST IN R within groups
问题描述
我一直在R中做一个样本t检验,但是今天我遇到了一个很大的挑战.我将数据按一个变量分组,我想对每个组执行一次样本t检验.我可以在SPSS中完美地做到这一点,但现在R令人头疼,无论谁知道如何做到这一点都可以提供帮助.示例场景
I have been doing one sample t-tests in R but today I have got one big challenge. I have data grouped in by one variable and I want to perform a one sample t-test per group. I can do this perfectly well in SPSS but it's now a headache in R, whoever knows how to do this to assist.Sample scenario
Location=rep(c("Area_A","Area_B"),4)
temp=rnorm(length(Location),34,5)
sample_data=data.frame(Location,ph)
sample_data
Location temp
1 Area_A 32.73782
2 Area_B 26.29996
3 Area_A 40.75101
4 Area_B 26.68309
5 Area_A 33.94259
6 Area_B 26.48326
7 Area_A 37.92506
8 Area_B 29.22532
假设上例中的假设均值为35,则t检验为一个样本,
Say the hypothesised mean in the above example is 35 ,the one sample t test would be,
t.test(sample_data$temp,mu=35)
这给了我
One Sample t-test
data: sample_data$ph
t = -1.6578, df = 7, p-value = 0.1413
alternative hypothesis: true mean is not equal to 35
95 percent confidence interval:
27.12898 36.38304
sample estimates:
mean of x
31.75601
但这是针对所有合并的组的.我可以在SPSS中做到这一点.有什么办法可以在R中用一行代码来完成此操作,或者如果不可能用一行代码来执行此操作,谁可以为我执行此操作.预先感谢.
But this is for all the groups combined. I can do it in SPSS. Is there any way to do this in R with a line of code or if not possible with a single line of code, who can do this for me. Thanks in advance.
推荐答案
一种解决方案是将每个组的t.test结果保存为列表:
One solution is to save t.test results per group as a list:
# reproducible results
set.seed(8)
# example data
Location=rep(c("Area_A","Area_B"),4)
temp=rnorm(length(Location),34,5)
sample_data=data.frame(Location,temp)
library(dplyr)
dt_res = sample_data %>%
group_by(Location) %>% # for each group
summarise(res = list(t.test(temp, mu=35))) # run t.test and save results as a list
# see the list of results
dt_res$res
# [[1]]
#
# One Sample t-test
#
# data: temp
# t = -0.76098, df = 3, p-value = 0.502
# alternative hypothesis: true mean is not equal to 35
# 95 percent confidence interval:
# 29.93251 38.11170
# sample estimates:
# mean of x
# 34.0221
#
#
# [[2]]
#
# One Sample t-test
#
# data: temp
# t = -1.045, df = 3, p-value = 0.3728
# alternative hypothesis: true mean is not equal to 35
# 95 percent confidence interval:
# 26.37007 39.36331
# sample estimates:
# mean of x
# 32.86669
另一种解决方案是将每个组的t.test结果保存为数据框:
Another solution is to save t.test results per group as a dataframe:
library(dplyr)
library(tidyr)
library(broom)
sample_data %>%
group_by(Location) %>%
summarise(res = list(tidy(t.test(temp, mu=35)))) %>%
unnest()
# # A tibble: 2 x 9
# Location estimate statistic p.value parameter conf.low conf.high method alternative
# <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
# 1 Area_A 34.0 -0.761 0.502 3 29.9 38.1 One Sample t-test two.sided
# 2 Area_B 32.9 -1.05 0.373 3 26.4 39.4 One Sample t-test two.sided
两种方法的原理是相同的.您按Location
分组,然后对每个组执行t.test.这完全取决于您希望拥有哪种输出.
The philosophy in both approaches is the same. You group by Location
and you perform a t.test for each group. It's all about what kind of output you prefer to have.
这篇关于组内的一个样本T-TEST IN R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!