首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >table1 package R中的ANOVA P-value列

table1 package R中的ANOVA P-value列
EN

Stack Overflow用户
提问于 2021-05-27 03:22:57
回答 1查看 300关注 0票数 0

我正在尝试对一个数据集执行方差分析测试,以便使用table1包比较表中不同组的平均值。在页面底部的this example中,作者执行t测试,将2个均值(男性与女性)与我在代码中粘贴的函数进行比较。

我想做同样的事情,但有多种方法,如下面的示例数据集所示。我想要年龄组的所有列,以及方差分析p值列。

我没有找到一个解决方案,所以如果有人能帮助我,我将非常感激!

代码语言:javascript
复制
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("<", "&lt;", 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)
EN

回答 1

Stack Overflow用户

发布于 2021-05-27 21:43:56

编辑:我解决了这个问题,其实很简单。这里是函数的新版本,以防有人正在寻找相同的功能:

代码语言:javascript
复制
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("<", "&lt;", format.pval(p, digits=3, eps=0.001)))
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67711459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档