micfalb-r at yahoo.de
2011-Mar-23 17:17 UTC
[R] Function to crop p-values from multiple Anovas
Starting with data from a microarray experiment and I would like to analyse the influence of two factors (age, treatment) on gene expression. Looking through the r-help archives and the web I tried the following: I put my data in a dataframe similar to this one:> example.df <- as.data.frame(matrix(data=runif(32,100,1000), nrow=4, ncol=4)) > example.df <- cbind(c("young","young","young","young","old","old","old","old"),c("drug", "control", "drug","control","drug","control","drug","control"), example.df) > colnames(example.df) <- c(c("age","treatment"),paste("gene",1:4,sep="")) > rownames(example.df) <- paste("sample", 1:8, sep="") > example.dfage treatment gene1 gene2 gene3 gene4 sample1 young drug 392 878 908 740 sample2 young control 167 263 711 392 sample3 young drug 155 252 242 547 sample4 young control 333 348 295 300 sample5 old drug 392 878 908 740 sample6 old control 167 263 711 392 sample7 old drug 155 252 242 547 sample8 old control 333 348 295 300 Now I would like to define a function that will crop the p-values from an Anova (so that I can use the function with a 'for loop' later on to go through all the genes):> p.fun <- function(arg) {two_way_anova <- aov(arg ~ age * treatment, data = example.df) two_way_sum <- summary(two_way_anova) p_values <- two_way_sum[[1]]$"Pr(>F)"[1:3] return(p_values) } Unfortunately my setup seems to be flawed as I'm not capable to call my function:> p.fun(gene1)Error in eval(expr, envir, enclos) : object 'gene1' not found> p.fun("gene1")Error in model.frame.default(formula = arg ~ age * treatment, data = example.df, : variable lengths differ (found for 'age') Any help on this would be truly appreciated. Many thanks, Mic Falb University of Heidelberg
micfalb-r wrote:> > > > p.fun <- function(arg) { > two_way_anova <- aov(arg ~ age * treatment, data = example.df) > two_way_sum <- summary(two_way_anova) > p_values <- two_way_sum[[1]]$"Pr(>F)"[1:3] > return(p_values) > } > > Unfortunately my setup seems to be flawed as I'm not capable to call my > function: > > p.fun(gene1) > Error in eval(expr, envir, enclos) : object 'gene1' not found > > p.fun("gene1") > Error in model.frame.default(formula = arg ~ age * treatment, data > example.df, : > variable lengths differ (found for 'age') > and provide commented, minimal, self-contained, reproducible code. >Try this for your function p.fun <- function(arg) { lhs <- get(arg, envir=as.environment(example.df)) two_way_anova <- aov(lhs ~ age * treatment, data = example.df) two_way_sum <- summary(two_way_anova) p_values <- two_way_sum[[1]]$"Pr(>F)"[1:3] return(p_values) } Use with p.fun("gene1") There are surely more elegant methods. E.g. no quotes in the argument of the p.fun call. /Berend -- View this message in context: http://r.789695.n4.nabble.com/Function-to-crop-p-values-from-multiple-Anovas-tp3400271p3400434.html Sent from the R help mailing list archive at Nabble.com.
Possibly Parallel Threads
- R Shiny Help - Trouble passing user input columns to emmeans after ANOVA analysis
- Which function to use for multiple comparison?
- lmer specification for random effects: contradictory reults
- linear regression for grouped data
- How could I use a function saved in one file ?