-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Third lecture proposed exercises
- Loading branch information
Showing
13 changed files
with
1,646 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,360 @@ | ||
library( RNetCDF ) | ||
library( ncdf ) | ||
pwd | ||
help? | ||
directory | ||
setwd("/Users/samuel/") | ||
3+3 | ||
3+3 | ||
# this is just an example | ||
pi | ||
? help | ||
? mean | ||
whiteside | ||
library(MASS) | ||
whiteside | ||
top(whiteside) | ||
head(whiteside) | ||
names( whiteside) | ||
summary( whiteside) | ||
summary( whiteside[,1]) | ||
1970:1970 | ||
1970:1979 | ||
array = c( 1970:1979) | ||
array | ||
# create a vector | ||
x = 1:100 | ||
y = sin(x) | ||
plot( x ~ y) | ||
plot( y ~ x) | ||
plot( y ~ x, line) | ||
? plot | ||
plot( y ~ x, "l") | ||
plot( y ~ x, type="l") | ||
x = 1:1000 | ||
y = sin(x) | ||
plot( x ~ y , type="l") | ||
plot( y ~ x , type="l") | ||
plot( y ~ x , type="p") | ||
plot( y ~ x , type="") | ||
plot( y ~ x , type="l") | ||
plot( y ~ x , type="p") | ||
help(array) | ||
source("http://bioconductor.org/biocLite.R") | ||
?BiocUpgrade | ||
biocLite("BiocUpgrade") | ||
y | ||
y | ||
install.packages("biomaRt") | ||
biocLite() | ||
biocLite("biomaRt") | ||
choosebank() | ||
library("seqinr") | ||
choosebank() | ||
choosebank("genbank") | ||
query("BRCA1", "SP=Homo sapiens AND K=BRCA1") | ||
library("seqinr") | ||
choosebank("genbank") | ||
query("BRCA1", "SP=Homo sapiens AND K=BRCA1") | ||
attributes(BRCA1) | ||
BRCA1$req | ||
query("BRCA1", "SP=Homo sapiens AND AC=U61268") | ||
myseq <- getSequence(BRCA1$req[[1]]) | ||
myseq | ||
dotPlot( myseq, myseq) | ||
length( myseq) | ||
BRCA.variant1 <- getSequence(BRCA1$req[[1]]) | ||
length( BRCA.variant1) | ||
query(listname = "BRCA1", query="SP=homo sapiens AND K=BRCA1") | ||
BRCA.variant2 <- getSequence(BRCA1$req[[1]]) | ||
setwd("~/Google Drive/MSc MSE/Visualization and data modelling/GitHubCode/Lecture 2") | ||
# Exercise 1: download the example2.txt file | ||
# disclaimer: the data should be in the current directory to run this file | ||
# Exercise 2: Read this data into R | ||
data = read.table( "example2.txt", header=TRUE) | ||
# Exercise 3: Print out the data for cases 10 to 18 | ||
data[10:18, ] | ||
data | ||
data[, 2] | ||
data[c(23,2,5), 2] | ||
summary( data) | ||
tapply( data, mean) | ||
tapply( data$1, mean) | ||
tapply( data$yrs, mean) | ||
tapply( data[,1], mean) | ||
lapply( data, mean) | ||
lapply( data, c(mean, sum) | ||
) | ||
lapply( data, mean) | ||
sapply( data, mean) | ||
sapply( data, min) | ||
sapply( data, max) | ||
sapply( data, c(max,min,mean) | ||
) | ||
sapply( data, sd) | ||
customizedFunction <- function(x) { | ||
c(min = min(x), mean = mean(x), max = max(x), sd = sd(x)) | ||
} | ||
sapply( data, customizedFunction) | ||
c(min = min(x), mean = mean(x), max = max(x), Deviation = sd(x)) | ||
} | ||
sapply( data, customizedFunction) | ||
customizedFunction <- function(x) { | ||
c(min = min(x), mean = mean(x), max = max(x), Deviation = sd(x)) | ||
} | ||
sapply( data, customizedFunction) | ||
source('~/.active-rstudio-document', echo=TRUE) | ||
random.numbers <- sample( runif( 100, min=0, max=2), size=100, replace=F) | ||
hist ( random.numbers ) | ||
# Exercise 2: Use dt to evaluate the density function of the t distribution with 13 degrees | ||
# of freedom at 20 values in the range -1 to 1. | ||
density <- dt( x=seq(-1,1,len=20), df=13) | ||
plot( density ) | ||
# Exercise 3: Find P[X <= x] = 0.01 for a t distribution with 9 degrees of freedom. | ||
# pt calculates the cumulative distribution function, P( X <= x) | ||
# the function X is the t distribution with 9 degrees of freedom. | ||
# x = 0.01 | ||
pt( 0.01, df=9) | ||
# Exercise 4: IQ scores are known to have a normal distribution with mean 100 and | ||
# standard deviation 15. What IQ would you have if you were in the 80th | ||
min( IQ[ IQ > (0.9 * max(IQ))]) | ||
IQ <- rnorm(1000, mean=100, sd=15) | ||
quantile( IQ, 0.8) # We apply the quantile function to compute the percentiles | ||
# we can check graphically the result: | ||
plot( IQ ) | ||
abline( a=quantile(IQ, 0.8), b=0, col="red", lwd=2, lty="longdash") | ||
# Exercise 5: What IQ would you have if you were in the top 10 percent? | ||
min( IQ[ IQ > (0.9 * max(IQ))]) | ||
IQ | ||
max(IQ) | ||
density <- dt( x=seq(-1,1,len=20), df=13) | ||
plot( density ) | ||
pnorm( 0.9) | ||
pnorm( 0.9:1.0) | ||
pnorm( 1.0) | ||
x = seq( -1, 1, length=100) | ||
x | ||
hx = dnorm( x ) | ||
plot( hx, x) | ||
plot( x, hx) | ||
px = pnorm( x ) | ||
plot( x, px) | ||
hx = dnorm( x ); plot( x, hx) | ||
px = pnorm( x ); plot( x, px) | ||
x <- seq(-4,4,length=100)*sd + mean | ||
hx <- dnorm(x,mean,sd) | ||
IQ <- dnorm(1000, mean=100, sd=15) | ||
quantile( IQ, 0.8) # We apply the quantile function to compute the percentiles | ||
IQ <- rnorm(1000, mean=100, sd=15) | ||
quantile( IQ, 0.8) # We apply the quantile function to compute the percentiles | ||
mean <- 100 | ||
sd <- 15 | ||
IQ <- seq( -1, 1, length=100)*sd + mean | ||
dIQ <- dnorm(IQ, mean = mean, sd = sd) | ||
plot( IQ, dIQ) | ||
mean <- 100 | ||
sd <- 15 | ||
IQ <- seq( -1, 1, length=100)*sd + mean | ||
dIQ <- dnorm(IQ, mean = mean, sd = sd) | ||
plot( IQ, dIQ) | ||
IQ <- seq( -4, 4, length=100)*sd + mean | ||
dIQ <- dnorm(IQ, mean = mean, sd = sd) | ||
plot( IQ, dIQ) | ||
# percentile? | ||
mean <- 100 | ||
sd <- 15 | ||
IQ <- seq( -4, 4, length=100)*sd + mean | ||
dIQ <- dnorm(IQ, mean = mean, sd = sd) | ||
plot( IQ, dIQ) | ||
quantile( dIQ, 0.8) | ||
quantile( IQ, 0.8) | ||
quantile( IQ, 0.8, lty="longdash") | ||
plot( IQ, dIQ, lty="longdash") | ||
plot( IQ, dIQ) | ||
quantile( IQ, 0.8) | ||
# we can check graphically the result: | ||
plot( IQ ) | ||
abline( a=quantile(IQ, 0.8), b=0, col="red", lwd=2, lty="longdash") | ||
plot( IQ ) | ||
abline( a=quantile(IQ, 0.8), b=0, col="red", lwd=2, lty="longdash") | ||
abline( a=0, b=80, col="red", lwd=2, lty="longdash") | ||
plot( IQ ) | ||
#abline( a=quantile(IQ, 0.8), b=0, col="red", lwd=2, lty="longdash") | ||
points( x=80, y=(quantile(IQ, 0.8))) | ||
points( x=80, y=(quantile(IQ, 0.8)), pch=23) | ||
plot( IQ ) | ||
#abline( a=quantile(IQ, 0.8), b=0, col="red", lwd=2, lty="longdash") | ||
points( x=80, y=(quantile(IQ, 0.8)), pch=23) | ||
plot( IQ, dIQ) | ||
quantile( IQ, 0.8) | ||
# we can check graphically the result: | ||
plot( IQ ) | ||
#abline( a=quantile(IQ, 0.8), b=0, col="red", lwd=2, lty="longdash") | ||
points( x=80, y=(quantile(IQ, 0.8)), pch=23) | ||
plot( IQ, type='l' ) | ||
#abline( a=quantile(IQ, 0.8), b=0, col="red", lwd=2, lty="longdash") | ||
points( x=80, y=(quantile(IQ, 0.8)), pch=23) | ||
plot( IQ, type='l', lty="longdash") | ||
#abline( a=quantile(IQ, 0.8), b=0, col="red", lwd=2, lty="longdash") | ||
points( x=80, y=(quantile(IQ, 0.8)), pch=23) | ||
plot( IQ, type='l', lty="longdash") | ||
points( x=80, y=result, pch=23) | ||
text( x=80, y=result, "El resultado") | ||
result <- quantile( IQ, 0.8) | ||
# we can check graphically the result: | ||
plot( IQ, type='l', lty="longdash") | ||
points( x=80, y=result, pch=23) | ||
text( x=80, y=result, "El resultado") | ||
text( x=80, y=result, "El resultado", offset=0.5) | ||
plot( IQ, type='l', lty="longdash") | ||
points( x=80, y=result, pch=23) | ||
text( x=80, y=result, "El resultado", offset=0.5) | ||
plot( IQ, type='l', lty="longdash") | ||
points( x=80, y=result, pch=23) | ||
string = 'hola' | ||
text( x=80, y=result, string, offset=0.5) | ||
string = 'Hola ' + 20 | ||
string = 'Hola ' + as.string(20) | ||
string = 'Hola %d', 10 | ||
string = 'Hola ' + ' adios' | ||
string = c('Hola ',' adios') | ||
text( x=80, y=result, string, offset=0.5) | ||
plot( IQ, type='l', lty="longdash") | ||
points( x=80, y=result, pch=23) | ||
string = c('Hola ',' adios') | ||
text( x=80, y=result, string, offset=0.5) | ||
string = c('Hola ',' adios') | ||
string | ||
string = as.character( c('Hola ',' adios')) | ||
string | ||
string = paste('Hola ',' adios') | ||
string | ||
string = paste('Hola ','adios') | ||
string | ||
string = paste('Hola ',20) | ||
string | ||
string = paste( 80, result) | ||
string | ||
string = paste( '(',80,',', result,')') | ||
string | ||
string = paste( '(', 80 ,',', result,')') | ||
string | ||
text( x=80, y=result, string, offset=0.5) | ||
text( x=80, y=result, string, offset=0.5, cex=0.6) | ||
plot( IQ, type='l', lty="longdash") | ||
points( x=80, y=result, pch=23) | ||
string = paste( '(', 80 ,',', result,')') | ||
string | ||
text( x=80, y=result, string, offset=0.5, cex=0.6) | ||
text( x=85, y=result, string, offset=0.5, cex=0.6) | ||
plot( IQ, type='l', lty="longdash") | ||
points( x=80, y=result, pch=23) | ||
string = paste( '(', 80 ,',', result,')') | ||
string | ||
text( x=85, y=result, string, offset=0.5, cex=0.6) | ||
plot( IQ, type='l', lty="longdash") | ||
points( x=80, y=result, pch=23) | ||
string = paste( '(', 80 ,',', result,')') | ||
string | ||
text( x=88, y=result, string, offset=0.5, cex=0.6) | ||
plot( IQ, type='l', lty="longdash") | ||
points( x=80, y=result, pch=23) | ||
string = paste( '(', 80 ,',', result,')') | ||
string | ||
text( x=88, y=result, string, offset=0.5, cex=0.6) | ||
min(IQ) | ||
max(IQ) | ||
0.9*max(IQ) | ||
0.9*(max(IQ) - min(IQ)) | ||
mean <- 100 | ||
sd <- 15 | ||
IQ <- seq( -4, 4, length=100)*sd + mean | ||
dIQ <- dnorm(IQ, mean = mean, sd = sd) | ||
plot( IQ, dIQ) | ||
0.9*(max(IQ) - min(IQ)) + min(IQ) | ||
p <- 0.9*(max(IQ) - min(IQ)) + min(IQ) | ||
min( IQ[ IQ > p]) | ||
p <- 0.9*(max(IQ) - min(IQ)) + min(IQ) | ||
sprintf("Answer %.2f", p) | ||
answer <- pt( 0.01, df=9) | ||
sprintf("Answer %.2f", answer) | ||
pnorm(142, mean=mean, sd=sd) | ||
1 - pnorm(142, mean=mean, sd=sd) | ||
sprintf("Answer %.2f", answer) | ||
answer <- (1 - pnorm(142, mean=mean, sd=sd)) | ||
sprintf("Answer %.2f", answer) | ||
sprintf("Answer %.6f", answer) | ||
sprintf("Answer %.6f %", answer) | ||
sprintf("Answer %.6f '%'", answer) | ||
set.seed(144) | ||
values = runif( 100, min=0, max=2) | ||
random.numbers <- sample( values, size=20, replace=F) | ||
set.seed(144) | ||
values = runif( 100, min=0, max=2) | ||
set1 <- sample( values, size=20, replace=F) | ||
set2 <- sample( values, size=20, replace=F) | ||
set1 | ||
set2 | ||
set.seed(0) | ||
set1 <- rnorm(n=10, mean = 100, sd=15) | ||
set2 <- rnorm(n=10, mean = 100, sd=15) | ||
set1 | ||
set2 | ||
set.seed(0) | ||
set1 <- rnorm(n=20, mean = 100, sd=15) | ||
set2 <- rnorm(n=20, mean = 100, sd=15) | ||
set1 | ||
set2 | ||
set.seed(48639158) | ||
set1 <- rnorm(n=20, mean = 100, sd=15) | ||
set2 <- rnorm(n=20, mean = 100, sd=15) | ||
set1 | ||
set2 | ||
set.seed(0) | ||
set1 <- rnorm(n=20, mean = 100, sd=15) | ||
set2 <- rnorm(n=20, mean = 100, sd=15) | ||
sprintf("First pair of samples") | ||
set1 | ||
set2 | ||
set.seed(48639158) | ||
set1 <- rnorm(n=20, mean = 100, sd=15) | ||
set2 <- rnorm(n=20, mean = 100, sd=15) | ||
sprintf("Second pair of samples") | ||
set1 | ||
set2 | ||
mean <- 100 | ||
sd <- 15 | ||
IQ <- seq( -4, 4, length=100)*sd + mean | ||
dIQ <- dnorm(IQ, mean = mean, sd = sd) | ||
plot( IQ, dIQ) | ||
result <- quantile( IQ, 0.8) | ||
sprintf("Answer %.2f", result) | ||
IQ <- seq( -4, 4, length=100) | ||
dIQ <- dnorm(IQ, mean = mean, sd = sd) | ||
plot( IQ, dIQ) | ||
result <- quantile( IQ, 0.8) | ||
mean <- 100 | ||
sd <- 15 | ||
IQ <- seq( -4, 4, length=100)*sd + mean | ||
dIQ <- dnorm(IQ, mean = mean, sd = sd) | ||
plot( IQ, dIQ) | ||
result <- quantile( IQ, 0.8) | ||
mean <- 100 | ||
sd <- 15 | ||
IQ <- seq( -4, 4, length=100)*sd + mean | ||
dIQ <- dnorm(IQ, mean = mean, sd = sd) | ||
plot( IQ, dIQ) | ||
result <- quantile( IQ, 0.8) | ||
sprintf("Answer %.2f", result) | ||
foo <- function(x, ...){} | ||
foo <- function(x, ...){} | ||
foo <- function(x, ...){ | ||
} | ||
foo <- function(x, ...) | ||
{ | ||
ans <- rnorm(x, ...) | ||
} | ||
x = 10 | ||
plot( x, foo(x)) | ||
plot(foo(x)) | ||
plot(foo(x, 100, 15)) |
Oops, something went wrong.