Home > OS >  One-sample T-test Over Multiple Columns with Multiple mu Values in R
One-sample T-test Over Multiple Columns with Multiple mu Values in R

Time:12-03

I have several datasets, each for a particular time point, and each containing several measures. For each of them, I want to conduct a one-sample t-test on each measure, so across all the columns. Each measure has a different mu value that I want to compare my results with. I have tried creating a function to do this so I only have to give it the name of the dataset as an argument. I have created a list of mu values. However, the function won't accept this and I get an error. Here is an example dataset:

t1 <- rnorm(20, 10, 1)
t2 <- rnorm(20, 10, 1)
t3 <- rnorm(20, 10, 1)
test_data <- data.frame(t1, t2, t3)

And the lists of mu values and variables:

muvals <- c(24, 51.8, 21.89)
varlist <- c(t1, t2, t3)

This is my attempt at the function:

onett <- function(tpoint) {
  t.test(tpoint$varlist, mu = muvals)
}

And the error message I get is: Error in t.test.default(tpoint$varlist, mu = muvals) : 'mu' must be a single number

Is there a way to get this function to work, or otherwise iterate through each column and the list of mu values? Please help.

CodePudding user response:

There may be shorter ways but here is a proposition to test all your samples with all your mu values: you store p-values into a data-frame.

You will find below a function where you can specify both your sample and your mu value ; then you could create a data-frame to store p-values.

# 1- Simulating samples into a data-frame
set.seed(1)
for(k in 1:3){ 
  assign(paste("t", k, sep=""), rnorm(20, 10, 1)) 
}
test_data <- data.frame(t1, t2, t3)

# 2- Choosing mu values to test
muvals <- c(24, 51.8, 21.89)

# 3- Creating function which depends on both your sample and your mu value
onett <- function(tPoint, muValue) {
  t.test(tPoint, mu=muValue)$p.value
}

# 4- Creating a data-frame for p-values storage with your mu values as row-names and your sample name as column-name
dfPvalues <- data.frame(matrix(NA, length(muvals), ncol(test_data)), row.names=muvals)
colnames(dfPvalues) <- colnames(test_data)

# 5- Filling the p-value data-frame through a loop
for(i in 1:nrow(dfPvalues)){
  for(j in 1:ncol(dfPvalues)){
    dfPvalues[i, j] <- onett(tPoint=test_data[,j], muValue=muvals[i])
  }
}

CodePudding user response:

You'd have to iterate over every combination of each column and mu value. To simply print out the results of all t-tests the purrr::cross2 function would give you a list of all column/mu combinations and purrr::map would loop over the tests:

library(purrr)

t1 <- rnorm(20, 10, 1)
t2 <- rnorm(20, 10, 1)
t3 <- rnorm(20, 10, 1)
test_data <- data.frame(t1, t2, t3)

onett <- function(data) {
  muvals <- c(24, 51.8, 21.89)
  map(cross2(data, muvals), ~ t.test(.x[[1]], mu = .x[[2]]))
}

onett(test_data)
#> [[1]]
#> 
#>  One Sample t-test
#> 
#> data:  .x[[1]]
#> t = -79.575, df = 19, p-value < 2.2e-16
#> alternative hypothesis: true mean is not equal to 24
#> 95 percent confidence interval:
#>   9.627445 10.364141
#> sample estimates:
#> mean of x 
#>  9.995793 
#> 
#> 
#> [[2]]
#> 
#>  One Sample t-test
#> 
#> data:  .x[[1]]
#> t = -52.418, df = 19, p-value < 2.2e-16
#> alternative hypothesis: true mean is not equal to 24
#> 95 percent confidence interval:
#>   9.857787 10.943797
#> sample estimates:
#> mean of x 
#>  10.40079 
#> 
#> 
#> [[3]]
#> 
#>  One Sample t-test
#> 
#> data:  .x[[1]]
#> t = -66.473, df = 19, p-value < 2.2e-16
#> alternative hypothesis: true mean is not equal to 24
#> 95 percent confidence interval:
#>   9.618567 10.496566
#> sample estimates:
#> mean of x 
#>  10.05757 
#> ... etc.

You might want to think about other ways of outputting the results though if you want to be sure you can line up the feed of test results with the data/params being used in each case.

Created on 2021-12-02 by the reprex package (v2.0.1)

  • Related