Skip to content

Commit

Permalink
lecture slides
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaczhang committed Jan 16, 2024
1 parent c296a27 commit 0932d52
Show file tree
Hide file tree
Showing 13 changed files with 705 additions and 600 deletions.
Binary file modified dev/lablecture_day3_pca.key
Binary file not shown.
1,224 changes: 640 additions & 584 deletions docs/lab/lab_day3_pca.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 12 additions & 5 deletions docs/lab/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,15 @@ <h2 class="anchored" data-anchor-id="lab-notes-and-r-scripts">Lab notes and R sc
<td style="text-align: center;">Day 3</td>
<td style="text-align: center;"><a href="../lab/lab_day3_pca.html">Day 3: Principal Component Analysis</a></td>
<td style="text-align: center;"><a href="https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab2_pca.R">Code</a></td>
<td style="text-align: center;"></td>
<td style="text-align: center;"><a href="presentation/Lab_pca.pdf">Slides</a></td>
</tr>
<tr class="odd">
<td style="text-align: center;"></td>
<td style="text-align: center;"></td>
<td style="text-align: center;"></td>
<td style="text-align: center;"></td>
</tr>
<tr class="even">
<td style="text-align: center;">Day 4</td>
<td style="text-align: center;"><a href="../lab/lab_day4_clustering.html">Day 4: Clustering</a></td>
<td style="text-align: center;"><a href="https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab3_clustering.R">Code</a></td>
Expand Down Expand Up @@ -571,10 +577,11 @@ <h2 class="anchored" data-anchor-id="lab-notes-and-r-scripts">Lab notes and R sc
<span id="cb1-35"><a href="#cb1-35" aria-hidden="true" tabindex="-1"></a>| Day 1 | <span class="co">[</span><span class="ot">Day 1: Introduction to R</span><span class="co">](lab_day1_intro.qmd)</span> | <span class="co">[</span><span class="ot">Code</span><span class="co">](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab0_intro_r.R)</span> |<span class="co">[</span><span class="ot">Slides</span><span class="co">](presentation/Lab_IntroR.pdf)</span> |</span>
<span id="cb1-36"><a href="#cb1-36" aria-hidden="true" tabindex="-1"></a>| | <span class="co">[</span><span class="ot">(supplement): Navigate RStudio and workspace</span><span class="co">](lab_day1s_rstudio.qmd)</span> | | |</span>
<span id="cb1-37"><a href="#cb1-37" aria-hidden="true" tabindex="-1"></a>| Day 2 | <span class="co">[</span><span class="ot">Day 2: Multiple testing</span><span class="co">](lab_day2_testing.qmd)</span> | <span class="co">[</span><span class="ot">Code</span><span class="co">](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab1.R)</span>, <span class="co">[</span><span class="ot">Code (solution)</span><span class="co">](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab1_exercise_solution.R)</span> | |</span>
<span id="cb1-38"><a href="#cb1-38" aria-hidden="true" tabindex="-1"></a>| Day 3 | <span class="co">[</span><span class="ot">Day 3: Principal Component Analysis</span><span class="co">](lab_day3_pca.qmd)</span> |<span class="co">[</span><span class="ot">Code</span><span class="co">](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab2_pca.R)</span> | |</span>
<span id="cb1-39"><a href="#cb1-39" aria-hidden="true" tabindex="-1"></a>| Day 4 | <span class="co">[</span><span class="ot">Day 4: Clustering</span><span class="co">](lab_day4_clustering.qmd)</span> |<span class="co">[</span><span class="ot">Code</span><span class="co">](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab3_clustering.R)</span> | |</span>
<span id="cb1-40"><a href="#cb1-40" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-41"><a href="#cb1-41" aria-hidden="true" tabindex="-1"></a></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<span id="cb1-38"><a href="#cb1-38" aria-hidden="true" tabindex="-1"></a>| Day 3 | <span class="co">[</span><span class="ot">Day 3: Principal Component Analysis</span><span class="co">](lab_day3_pca.qmd)</span> |<span class="co">[</span><span class="ot">Code</span><span class="co">](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab2_pca.R)</span> | <span class="co">[</span><span class="ot">Slides</span><span class="co">](presentation/Lab_pca.pdf)</span></span>
<span id="cb1-39"><a href="#cb1-39" aria-hidden="true" tabindex="-1"></a> |</span>
<span id="cb1-40"><a href="#cb1-40" aria-hidden="true" tabindex="-1"></a>| Day 4 | <span class="co">[</span><span class="ot">Day 4: Clustering</span><span class="co">](lab_day4_clustering.qmd)</span> |<span class="co">[</span><span class="ot">Code</span><span class="co">](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab3_clustering.R)</span> | |</span>
<span id="cb1-41"><a href="#cb1-41" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-42"><a href="#cb1-42" aria-hidden="true" tabindex="-1"></a></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div></div></div></div></div>
</div> <!-- /content -->

Expand Down
Binary file added docs/lab/presentation/Lab_pca.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions docs/search.json

Large diffs are not rendered by default.

32 changes: 25 additions & 7 deletions lab/code/MED3007_Lab1_exercise_solution.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
###-------------------------###
### SOLUTIONS TO EXERCISES ###
###-------------------------###



###--------------###
### 2. EXERCISE ###
###--------------###
Expand All @@ -19,13 +25,17 @@ png('./Rplots/ex2_histogram_vitaminD.png', height = 500, width = 500)
hist(Testfil_Rcourse$vitD_v1, prob = TRUE, xlab = 'vitamin D', main = 'histogram of vitamin D')
dev.off() # for closing the plot file

png('./Rplots/ex2_boxplot_vitaminD.png', height = 500, width = 500)
png('./ex2_boxplot_vitaminD.png', height = 500, width = 500)
boxplot(vitD_v1 ~ gender, Testfil_Rcourse)
dev.off() # for closing the plot file


vit1 <- Testfil_Rcourse[which(data$gender==0),'vitD_v1']
vit2 <- Testfil_Rcourse[which(data$gender==1),'vitD_v1']
vit1 <- Testfil_Rcourse[which(Testfil_Rcourse$gender==0),'vitD_v1']
vit2 <- Testfil_Rcourse[which(Testfil_Rcourse$gender==1),'vitD_v1']
# alternative syntax:
vit1 <- Testfil_Rcourse$vitD_v1[which(Testfil_Rcourse$gender==0)]
vit2 <- Testfil_Rcourse$vitD_v1[which(Testfil_Rcourse$gender==1)]



qqnorm(vit1, main = 'vitamin D - females')
Expand All @@ -37,7 +47,7 @@ qqline(vit2)
shapiro.test(vit1)
shapiro.test(vit2) # data is normally distb.

t.test(vitD_v1 ~ gender, data) # there is a difference in vitD between groups (gender)
t.test(vitD_v1 ~ gender, Testfil_Rcourse) # there is a difference in vitD between groups (gender)



Expand All @@ -55,7 +65,8 @@ t.test(vitD_v1 ~ gender, data) # there is a difference in vitD between groups (g

## 1. Load in the data using read.csv(). You will need to select header=F.

exp.data <- read.csv("./data/Ch10Ex11.csv",header=FALSE)
exp.data <- read.csv("./data/Ch10Ex11.csv",header=F)
dim(exp.data)
# I want each row to represent a sample, and each column a gene
exp.data <- t(exp.data)
dim(exp.data)
Expand Down Expand Up @@ -83,6 +94,9 @@ abline(h=0)

# let us first apply the t-test and get an idea of the significance in the data
# How many significant p-values (without any correction)
pval.ttest <- apply(exp.data, 2,
function(x){t.test(x[which(groups==1)], x[which(groups==2)])$p.value})

sum(pval.ttest < alpha)

# how many expected false positives?
Expand All @@ -101,11 +115,15 @@ sum(pval.fdr < alpha)

# Get the significant genes (here the genes are numbered from 1 to 1000)
sign.genes <- which(pval.fdr < alpha)
# or
sign.genes <- which(pval.fwer < alpha)

# Plot them
plot(genes.gr.means[1,], xlab = 'genes', main = 'significant genes in blue',
ylim = range(genes.gr.means), ylab = 'mean expr')
points(genes.gr.means[2,], col=2)
points(sign.genes, genes.gr.means[1,sign.genes], col=4, pch=4)
points(sign.genes,genes.gr.means[2,sign.genes], col=4, pch=4)
points(sign.genes, genes.gr.means[1,sign.genes], col=4, pch=4, lwd=2)
points(sign.genes,genes.gr.means[2,sign.genes], col=4, pch=4, lwd=2)
abline(h=0)


25 changes: 24 additions & 1 deletion lab/lab_day3_pca.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Download datasets [here](https://github.com/ocbe-uio/course_med3007/tree/main/la

R script: [Code](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab2_pca.R)

Lab Lecture
Lab Lecture: [Slides](presentation/Lab_pca.pdf)



Expand Down Expand Up @@ -187,6 +187,29 @@ scores_food <- pc_food$x
round(scores_food, digits = 2)
```

(Optional) You can double check that the score is indeed the product of the original data (after scaling) with loading. Take Albania for example (first row of the data). Its score for PC1 is 2.94. Let us try to reproduce this number from formula.

```{r}
#| label: pca-food-albania
#| warning: false
#| echo: true
food[1,]
# scale the data
food_s <- scale(food)
round(food_s[1,], digits = 2) # after centering and scaling
# loading for PC1 is the first column
round(loading_food[,1], digits = 2)
# let us muliple element by element, then sum together
prod_by_element <- as.numeric(food_s[1,]) * loading_food[, 1]
round(prod_by_element, digits = 2)
# sum all elements together
sum(prod_by_element)
```




Another important result from the `prcomp()` command is the explained variance (PVE, proportion of variance explained). All PCs explain 100% of the variance; while the first PC explains the largest proportion (46%), second PC explains the second largest proportion (18.2%) and so on.

You can find the variance (squared standard deviation, `sdev`) using the following commands. You can compare with the results from `summary()`, see if they correspond.
Expand Down
3 changes: 2 additions & 1 deletion lab/overview.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ day 4 (clustering)
| Day 1 | [Day 1: Introduction to R](lab_day1_intro.qmd) | [Code](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab0_intro_r.R) |[Slides](presentation/Lab_IntroR.pdf) |
| | [(supplement): Navigate RStudio and workspace](lab_day1s_rstudio.qmd) | | |
| Day 2 | [Day 2: Multiple testing](lab_day2_testing.qmd) | [Code](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab1.R), [Code (solution)](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab1_exercise_solution.R) | |
| Day 3 | [Day 3: Principal Component Analysis](lab_day3_pca.qmd) |[Code](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab2_pca.R) | |
| Day 3 | [Day 3: Principal Component Analysis](lab_day3_pca.qmd) |[Code](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab2_pca.R) | [Slides](presentation/Lab_pca.pdf)
|
| Day 4 | [Day 4: Clustering](lab_day4_clustering.qmd) |[Code](https://github.com/ocbe-uio/course_med3007/blob/main/lab/code/MED3007_Lab3_clustering.R) | |


Expand Down
Binary file added lab/presentation/Lab_pca.pdf
Binary file not shown.

0 comments on commit 0932d52

Please sign in to comment.