我正在尝试对一个数据集执行方差分析测试,以便使用table1包比较表中不同组的平均值。在页面底部的this example中,作者执行t测试,将2个均值(男性与女性)与我在代码中粘贴的函数进行比较。
我想做同样的事情,但有多种方法,如下面的示例数据集所示。我想要年龄组的所有列,以及方差分析p值列。
我没有找到一个解决方案,所以如果有人能帮助我,我将非常感激!
library(tidyverse)
library(table1)
# Function to compute t-test
pvalue <- function(x, ...) {
# Construct vectors of data y, and groups (strata) g
y <- unlist(x)
g <- factor(rep(1:length(x), times=sapply(x, length)))
if (is.numeric(y)) {
# For numeric variables, perform a standard 2-sample t-test
p <- t.test(y ~ g)$p.value
} else {
# For categorical variables, perform a chi-squared test of independence
p <- chisq.test(table(y, g))$p.value
}
# Format the p-value, using an HTML entity for the less-than sign.
# The initial empty string places the output on the line below the variable label.
c("", sub("<", "<", format.pval(p, digits=3, eps=0.001)))
}
# Fake dataset
age_group = factor(c("10-20", "20-30", "30-40", "40-50", "10-20", "40-50", "40-50", "30-40", "30-40", "30-40"),
levels = c("10-20", "20-30", "30-40", "40-50"))
protein = c(25.3, 87.5, 35.1, 50.8, 50.4, 61.5, 76.7, 56.1, 59.2, 40.2)
fat = c(76, 45, 74, 34, 55, 100, 94, 81, 23, 45)
gender = c("female", "male", "male", "female", "female", "female", "male", "male", "female", "female")
mydata <- tibble(gender, age_group, protein, fat)发布于 2021-05-27 21:43:56
编辑:我解决了这个问题,其实很简单。这里是函数的新版本,以防有人正在寻找相同的功能:
pvalueANOVA <- function(x, ...) {
# Construct vectors of data y, and groups (strata) g
y <- unlist(x)
g <- factor(rep(1:length(x), times=sapply(x, length)))
if (is.numeric(y)) {
# For numeric variables, perform a standard 2-sample t-test
ano <- aov(y ~ g)
p <- summary(ano)[[1]][[5]][1]
} else {
# For categorical variables, perform a chi-squared test of independence
p <- chisq.test(table(y, g))$p.value
}
# Format the p-value, using an HTML entity for the less-than sign.
# The initial empty string places the output on the line below the variable label.
c("", sub("<", "<", format.pval(p, digits=3, eps=0.001)))
}https://stackoverflow.com/questions/67711459
复制相似问题