Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial/Practical: Difference between revisions
No edit summary |
No edit summary |
||
Line 17: | Line 17: | ||
</div> | </div> | ||
<br style="clear:both"> | <br style="clear:both"> | ||
==Part 1: Random Walks== | |||
Below is a basic 'R' script to simulate a random walk along the X axis. | |||
Random walk is important to model as it is related to the phenomenon of diffusion. | |||
* Copy and save this script into a file. | |||
* Run this script within 'R' | |||
<syntax type='R'> | |||
############################ | |||
# | |||
# Random Walk 1D / 1 Trace | |||
# | |||
############################ | |||
# number of samples | |||
N <- 1000 | |||
# draw 3xN samples from normal distribution | |||
samples_1 <- rnorm(N) | |||
# Calculate 1D Paths | |||
path_1 <- cumsum(samples_1) | |||
#Plot samples, time series, and histogram | |||
steps <- seq(1:N) | |||
plot(steps, path_1, | |||
type='l', | |||
lwd = 2, | |||
col='red', | |||
main = "1D Random Walk", | |||
xlab = "Steps", | |||
ylab = "X axis", | |||
xlim = c(0, N), | |||
ylim = c(-50,50) | |||
) | |||
</syntax> | |||
===Question=== | |||
* Modify this script so that you can overlay as many independent paths as you want (Tip = use the function capability of 'R'). | |||
Here is a modified script to model a 2D Random Walk. | |||
<syntax type='R'> | |||
############################ | |||
# | |||
# Random Walk 2D | |||
# | |||
############################ | |||
# number of samples | |||
N <- 1000 | |||
# draw N samples from normal distribution | |||
x_samples <- rnorm(N, mean = 0, sd = 1) | |||
y_samples <- rnorm(N, mean = 0, sd = 0.5) | |||
# Calculate 1D Path in each direction | |||
X_path <- cumsum(x_samples) | |||
Y_path <- cumsum(y_samples) | |||
#Plot time series, and histogram | |||
# enable 2 plots to be displayed within the same graphics window | |||
par(mfrow = c(1,2)) | |||
# Scatter plot for X/Y distribution | |||
plot(x_samples, y_samples, type = 'p', | |||
lwd = 1, | |||
col = 'green', | |||
main = 'X/Y distribution', | |||
xlab = "X Samples", | |||
ylab = "Y Samples", | |||
xlim = c(-5, 5), | |||
ylim = c(-5,5)) | |||
# Time Serie for the particle path | |||
plot(X_path, Y_path, | |||
type='l', | |||
lwd = 2, | |||
col='red', | |||
main = "2D Random Walk", | |||
xlab = "X coordinate", | |||
ylab = "Y coordinate", | |||
xlim = c(-50, 50), | |||
ylim = c(-50,50) | |||
) | |||
</syntax> | |||
===Question=== | |||
Revision as of 09:23, 6 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 walk is important to model as it is related to the phenomenon of diffusion.
- Copy and save this script into a file.
- Run this script within 'R'
<syntax type='R'>
############################ # # Random Walk 1D / 1 Trace # ############################
# number of samples N <- 1000
# draw 3xN samples from normal distribution samples_1 <- rnorm(N)
# Calculate 1D Paths path_1 <- cumsum(samples_1)
#Plot samples, time series, and histogram steps <- seq(1:N)
plot(steps, path_1,
type='l', lwd = 2, col='red', main = "1D Random Walk", xlab = "Steps", ylab = "X axis", xlim = c(0, N), ylim = c(-50,50) ) </syntax>
Question
- Modify this script so that you can overlay as many independent paths as you want (Tip = use the function capability of 'R').
Here is a modified script to model a 2D Random Walk.
<syntax type='R'>
- Random Walk 2D
-
- number of samples
N <- 1000
- draw N samples from normal distribution
x_samples <- rnorm(N, mean = 0, sd = 1) y_samples <- rnorm(N, mean = 0, sd = 0.5)
- Calculate 1D Path in each direction
X_path <- cumsum(x_samples) Y_path <- cumsum(y_samples)
- Plot time series, and histogram
- enable 2 plots to be displayed within the same graphics window
par(mfrow = c(1,2))
- Scatter plot for X/Y distribution
plot(x_samples, y_samples, type = 'p', lwd = 1, col = 'green', main = 'X/Y distribution', xlab = "X Samples", ylab = "Y Samples", xlim = c(-5, 5), ylim = c(-5,5))
- Time Serie for the particle path
plot(X_path, Y_path, type='l', lwd = 2, col='red', main = "2D Random Walk", xlab = "X coordinate", ylab = "Y coordinate", xlim = c(-50, 50), ylim = c(-50,50) ) </syntax>
Question
- 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: