[R] t-test: many changing groups.
Adams, Jean
jvadams at usgs.gov
Sun Mar 8 23:32:04 CET 2015
Hard to disentangle your code in the non-HTML world of r-help. Please use
plain text in the future. Below is one approach you could try.
Jean
# response variables
resp_vars <- runif(20)
names(resp_vars) <- paste0("sample_", seq(20))
# independent variables
ind_vars <- matrix(sample(c("A", "B"), 2000, TRUE), ncol=20,
dimnames=list(paste0("obs_", seq(100)), names(resp_vars)))
# save the t-test results to a list
t_fit <- apply(ind_vars, 1, function(obs) t.test(resp_vars ~ obs))
# grab the estimates from each t-test and put it in a data frame
library(broom)
t_est <- do.call(rbind, lapply(t_fit, tidy))
names(t_est) <- c("diff", "meanA", "meanB", "t", "p.value", "df",
"conf.low", "conf.high")
head(t_est)
On Sat, Mar 7, 2015 at 10:05 AM, White Sky <bluebarbarossa at hotmail.com>
wrote:
> I'd like to perform a t-test between groups 'A' and 'B'. The difficulty is
> that although there is only one response variable, there are many
> observations, and the grouping (A or B) differs with each observation. My
> code for generating the input data is shown below.
> I'd like to know how to approach doing the test, ideally so that the
> t-test results for each observation are presented in a table. I'm not sure
> where to start as other searches have been futile... something along the
> lines of t.test(ind_vars ~ resp_vars) , maybe using rapply, and separating
> groups by A and B each time...
> # Matrix for response variableN_samples <- 20resp_vars <-
> matrix(runif(n=N_samples, min=0, max=1))sample_names <- paste0("sample_",
> 1:N_samples )rownames(x=resp_vars) <- sample_namescolnames(x=resp_vars) <-
> "resp"resp_vars [1:5,]
> # Matrix for independent variablesN_observations <- 100ind_vars <-
> matrix(NA, N_observations, N_samples)ind_vars <- apply(ind_vars, c(1,2),
> function(x) sample(c("A", "B"),1))ind_var_names <- paste0("obs_",
> 1:N_observations)rownames(x=ind_vars) <- ind_var_namescolnames(x=ind_vars)
> <- sample_namesind_vars[1:3,1:5]
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list