Welcome to the Genome Toolbox! I am glad you navigated to the blog and hope you find the contents useful and insightful for your genomic needs. If you find any of the entries particularly helpful, be sure to click the +1 button on the bottom of the post and share with your colleagues. Your input is encouraged, so if you have comments or are aware of more efficient tools not included in a post, I would love to hear from you. Enjoy your time browsing through the Toolbox.
Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Wednesday, January 29, 2014

Bar Plot with 95% Conficence Interval Error Bars in R

R is a great plotting program for making publication quality bar plots of your data.  I often need to make quick bar plots to help collaborators quickly visualize data, so I thought I would put together a very generalized script that can be modified and built on as a template to make future bar plots in R.

In this script you manually enter your data (as you would see it in a 2x2 table) and then calculate the estimated frequency and 95% CI around that frequency using the binom.confint function in the binom package.  Next, a parametric and non-parametric p-value is calculated with the binom.test and fisher.test commands, respectively.  These statistics are then plotted using the R's barplot function.  The example code is below along with what the plot will look like.  P-values and N's are automatically filled and the y limits are calculated to ensure the graph includes all the plotted details.



Thursday, January 16, 2014

Remove Outliers from R Variable

R boxplot is an easy function to visualize a variable and get a sense of the distribution of values as well as potential outlier data points that may exist.  By saving the output to a variable (ex: bxplt <- boxplot(data$expression)), you can see a list of outlier points (ex: bxplt$out) that you may wish to exclude from an analysis.  The method R uses to identify extreme values is to calculate 1.5 times the interquartile range (ie: third quartile minus first quartile) and create limits by subtracting this value from the first quartile and adding it to the third quartile.  Any point that is less than the smaller limit or greater than the larger limit is considered an outlier by this method.  Here is a simple function I created to remove outliers from an R variable, the script essentially removes outliers identified by the boxplot function by replacing outlier values with NA and returning this modified variable for analysis.  So, for example, if you wanted to find the mean of data$expression with outliers removed all you would need to do is first run the below function and then use the command mean(ro(data$expression)).  Overall, a pretty simple way to remove out outliers if you do indeed choose to do so.

Wednesday, January 15, 2014

Syntax for a User-Defined R Function

R functions are incredibly handy ways to have R carry out repetitive tasks for you without having to copy and paste lines in your code over and over again.  Since I don't daily write new R functions, I sometimes forget what the syntax is to create these custom functions.  Here's an example function I made to tell if a number is even or odd:


To run a custom R function simply use the function name with all the needed input variable in parenthesis (ex: even_num(413)).

Friday, November 8, 2013

Python Function to Calculate P-value from Z score

Every time I needed to quickly convert a z score to a p-value, I would have to search online for an online calculator or refresh my mind how to do in in R/Excel.  Getting tired of periodically having to do this, I built a simple Python function to do this for me.  Here's the code if you find it helpful: