forked from bioinformatics-core-shared-training/basicr
-
Notifications
You must be signed in to change notification settings - Fork 40
/
solution-exercise6.Rmd
56 lines (43 loc) · 1.23 KB
/
solution-exercise6.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---
title: "Exercise 6"
author: "Your Name"
date: '`r format(Sys.time(), "%d %b %Y")`'
output: pdf_document
---
First reading the data and doing an initial scatter plot
```{r}
weather <- read.csv("ozone.csv")
plot(weather$Temp, weather$Ozone,xlab="Temperature",ylab="Ozone level",pch=16)
```
Fitting the model using the R formula syntax
```{r}
mod1 <- lm(weather$Ozone~weather$Temp)
summary(mod1)
```
Showing the best-line fit on the plot
```{r}
plot(weather$Temp, weather$Ozone, pch=16)
abline(mod1, col="red", lty=2)
```
```{r}
plot(weather$Temp, weather$Ozone, pch=16)
abline(mod1, col="red", lty=2)
coeffs = coef(mod1)
text(60,150, paste("y = ", round(coeffs[2],2), "x",round(coeffs[1],2),sep=""))
```
Calculating the correlation using the `cor` function and putting the r-squared value on the graph
```{r}
plot(weather$Temp, weather$Ozone, pch=16)
abline(mod1, col="red", lty=2)
cor = cor(weather$Temp,weather$Ozone,use="complete.obs")
cor
text(95,150, paste("r^2 = ", round(cor^2,2)))
```
A little magic to make the formatting a bit nicer
```{r}
plot(weather$Temp, weather$Ozone, pch=16)
abline(mod1, col="red", lty=2)
cor = cor(weather$Temp,weather$Ozone,use="c")
cor
text(95,150, substitute(paste(r^2, "=" ,x),list(x=round(cor^2,2))))
```