mytest <- function(x, value=0) { xx <- as.character(substitute(x)) if(!is.numeric(x)) stop(sprintf('%s is not numeric', xx)) if(shapiro.test(x)$p.value>.10){ print(t.test(x, mu=value)) }else{ print(wilcox.test(x, mu=value)) }}

We can combine that with another function to produce a density plot.

myplot <- function(x,color="blue"){ xx <- as.character(substitute(x)) if(!is.numeric(x)) stop(sprintf('%s is not numeric', xx)) title <- paste("Density Plot","\n","Dataset = ",deparse(substitute(x))) mydens <- density(x) plot(mydens,main=title,las=1) polygon(mydens,col=color) }

Now, let's see how our functions work. If we generate some random values from a Gaussian distribution, we would expect it to "normally" pass a normality test and a t-test to be performed. However, if we had data that was generated from another distribution that is not 'normal', than typically we would expect to see the results from the Wilcox test.

set.seed(123) n <- 1000 normal <- rnorm(n,0,1) chisq <- rchisq(n,df=5) mytest(normal) myplot(normal) #Test for difference from 5 for chi-square data mytest(chisq,value=5) myplot(chisq ,color="orange")

Results from 'mytest(normal)':

One Sample t-test

data: x

t = 0.5143, df = 999, p-value = 0.6072

alternative hypothesis: true mean is not equal to 0

95 percent confidence interval:

-0.04541145 0.07766719

sample estimates:

mean of x

0.01612787

Results from 'mytest(chisq,value=5)':

Wilcoxon signed rank test with continuity correction

data: x

V = 214385, p-value = 8.644e-05

alternative hypothesis: true location is not equal to 5

**Conclusion**

The benefit of working ahead can be seen. Once you have these functions written you can add them to your personal R package that you host on github. Then you will be able to use them whenever you have an internet connection and the whole R community has the chance to benefit. Also, it is easy to combine these two functions into one.

` `

#Combine the functions PlotAndTest <- function(x){ mytest(x) myplot(x) }