简体   繁体   中英

ANOVA P-value column in table1 package R

I'm trying to perform an ANOVA test on a data set to compare the means of different groups in a table using table1 package. In this example , at the bottom of the page, the writer performs a t-test to compare 2 means (male vs female) with a function that I've pasted with my code.

I would like to do the same but for multiple means, as shown with my example dataset below. I would like to have all the columns of age groups, and an ANOVA p-value column.

I didn't find a solution so if anyone can help I would be very thankful!


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)

Edit: I solved the problem it was actually quiet easy. Here the new version of the function in case someone is looking for the same feature:

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)))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM