Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial/Practical: Difference between revisions
Line 79: | Line 79: | ||
</syntax> | </syntax> | ||
=== | ===Questions=== | ||
* Modify this script so that you can overlay many independent paths as you want (Tip = use the function capability from 'R'). | * Modify this script so that you can overlay many independent paths as you want (Tip = use the function capability from 'R'). | ||
* Build a function to only return the last X coordinate after nbSteps. | * Build a function to only return the last X coordinate after nbSteps. |
Revision as of 03:35, 9 October 2009
<html> <body> <!-- Start of StatCounter Code --> <script type="text/javascript"> var sc_project=3315864; var sc_invisible=0; var sc_partition=36; var sc_security="8bb2efcd"; </script>
<script type="text/javascript" src="http://www.statcounter.com/counter/counter_xhtml.js"></script><noscript><div class="statcounter"><a class="statcounter" href="http://www.statcounter.com/"><img class="statcounter" src="http://c37.statcounter.com/3315864/0/8bb2efcd/0/" alt="blog stats" /></a></div></noscript> <!-- End of StatCounter Code -->
</body> </html>
Introduction to 'R'
Part 1: Random Walks
Below is a basic 'R' script to simulate a random walk along the X axis. Random walks are important to model as they relate for example to the phenomenon of diffusion.
- Copy and save the script into a file.
- Run this script within 'R'
<syntax type='R'>
- Random Walk 1D / 1 Trace
-
- number of steps
nbSteps <- 1000
- sampling and path construction
- First method: Looping
- draw from uniform distribtion between [0,1]
x <- runif(n = nbSteps)
- initialise path variable
xSteps <- rep(0, nbSteps) #as opposed to x1Steps <-0
- build independent steps series
for(i in 1:nbSteps){ if(x[i]>0.5){ xSteps[i] <- 1} else{ xSteps[i] <- -1 } }
- build path from independent steps
xPath <- cumsum(xSteps)
- Second method: vectorisation (faster)
- x <- runif(n = nbSteps)
- xSteps <- ifelse(x>0.5, 1, -1)
- xPath <- cumsum(xSteps)
- plot path
plot(xPath, 1:nbSteps, type='l', col='red', lwd =2,
main = 'Random Walk 1D', xlab = "X coordinate", ylab = "Steps")
</syntax>
Questions
- Modify this script so that you can overlay many independent paths as you want (Tip = use the function capability from 'R').
- Build a function to only return the last X coordinate after nbSteps.
- Use the previous function to build the distribution of X coordinate from 1000 independent simulations, after nbSteps.
- Extend the first script to be able to simualte 2D random walks.
Part 2: Enzymatic reaction analysis
<syntax type='R'>
- R Example, inspired by
- Enzymatic analysis
concentration <- c(0.3330, 0.1670, 0.0833, 0.0416, 0.0208, 0.0104, 0.0052) velocity <- c(3.636, 3.636, 3.236, 2.660, 2.114, 1.466, 0.866)
- create data frame by combining data
df <- data.frame(concentration, velocity)
- print 'df'
df
- Correct one value from 'velocity'
velocity[4] <- 2.666
- Check that data frame 'df' has also been updated
df$velocity[4] # or df[4,2]
- Visualise data
plot(df$concentration, df$velocity)
- Linear regression
- create new variable to linearise plot
df$ytrans <- df$conc/df$vel
- plot new relationship
plot(df$conc, df$ytrans)
- compute linear regression
lmfit <- lm(ytrans~conc, data=df)
names(lmfit)
summary(lmfit)
plot(lmfit)
class(lmfit)
coef(lmfit)
plot(df$conc, df$ytrans) abline(coef(lmfit))
Vm <- 1/coef(lmfit)[2] K <- Vm*coef(lmfit)[1]
</syntax>
- Basic intro to 'R' language
- types: vector, string, factor, data_frame
- functions for basic stats
- Exercises:
- ...
- Import / Export CSV files (other file formats ?)
- Exercises:
- ...
- Exercises:
- Fitting (least square method)
- Exercises:
- ...
- Exercises:
- Plotting (scatter plots, histograms, time series)
- Exercises:
- ...
- Exercises:
- ODE simulations (based on basic gene expression models)
- Exercises:
- ...
- Exercises: