-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnotes.Rmd
183 lines (129 loc) · 4.33 KB
/
notes.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
---
title: "Notes"
output:
html_document:
include:
before_body: header.html
---
# Thursday
For building a package with roxygen documentation (the part that makes the help files for you), you need to tell RStudio to build that documentation when you click 'Build and Reload'.
Go to Tools > Project Options > Build Tools Then click the box that says the build documentation with roxygen. A pop-up window should appear. Click the box at the bottom that says to remake documentation when you click 'Build and Reload'.
----
Seasonal data. If you have seasonal data that you would like to analyze, please put in this format:
```
Year Month metric.tons
2018 1 1
2018 2 2
2018 3 3
...
2019 1 4
2019 2 6
2019 3 NA
```
Read in with this code:
```
test <- read.csv("Data/test.csv", stringsAsFactors = FALSE)
save(test, file="test.RData")
```
# Wednesday
To save data in R to a .csv file use the following:
```
write.csv(landings, file="test.csv", row.names=FALSE, quote=FALSE)
```
This works for data.frames, like landings, and also matrices:
```
mat <- matrix(rnorm(10),5,2)
colnames(mat) <- c("number 1", "number 2")
write.csv(landings, file="test.csv", row.names=FALSE, quote=FALSE)
```
# Tuesday
The bookdown-demo repository has been edited so that it does not try to make a PDF. If you want that, there are instructions in the README.md file.
---
Read in simple csv file named ```test.csv``` that looks like this
```
Year Species metric.tons
1 2018 Fish1 1
2 2019 Fish1 2
3 2018 Fish2 3
4 2019 Fish2 4
5 2018 Fish3 6
6 2019 Fish4 NA
```
with this code:
```
test <- read.csv("Data/test.csv", stringsAsFactors = FALSE)
save(test, file="test.RData")
```
---
Read in a file where the data are in columns. If your data (test.csv) look like this with each species (or site) across the columns:
```
Year,Anchovy,Sardine,Chub mackerel,Horse mackerel,Mackerel,Jack Mackerel
1964,5449.2,12984.4,1720.7,4022.4,NA,NA
1965,4263.5,10611.1,1278.5,4158.3,NA,NA
1966,5146.4,11437.8,802.6,3012.1,NA,NA
```
Use this code:
```
library(reshape2)
test <- read.csv("Data/test.csv", stringsAsFactors = FALSE)
melt(test, id="Year", value.name="metric.tons", variable.name="Species")
save(test, file="test.RData")
```
If your data also have, say, a month (or qtr) column, use this code:
```
library(reshape2)
test <- read.csv("Data/test.csv", stringsAsFactors = FALSE)
melt(test, id=c("Year","Month"), value.name="metric.tons", variable.name="Species")
save(test, file="test.RData")
```
----
If you have a response variable and multiple explanatory variables:
```
Year, Anchovy, SST, Mackerel
1964, 5449.2, 24.4, 1720.7
1965, 4263.5, 30.1, 1278.5
1966, 5146.4, 23.8, 802.6
```
Use this code:
```
test <- read.csv("Data/test.csv", stringsAsFactors = FALSE)
save(test, file="test.RData")
```
Use this `lm()` model (or gam() etc):
```
fit <- lm(Anchovy ~ SST + Mackerel, data=test)
```
---
What is a list? It is a object with multiple other types of objects 'tied' together.
```{r}
a <- list(ar=c(.8,.3), name="model", years=1:10)
a
```
You can now reference the different objects in `a`.
```{r}
a$ar
a$name
```
----
Why is the `arima.sim` throwing an error sometimes? You must specify a stationary model and if you just randomly chose the $\beta$'s (AR parameters) and $\theta$'s (MA parameters) then you might be specifying a non-stationary model.
# Monday
Please install the rmarkdown package. I forgot to include that.
```
install.packages("rmarkdown")
```
----
If you do not have LaTeX installed, you can install the tinytex package. Run these commands. This will allow you to make PDF files from R Markdown files. Some of you installed tinytex, but you need to run the 2nd command also to install LiveTeX.
```
install.packages('tinytex')
tinytex::install_tinytex()
```
----
If your get an error saying package or repository unavailable when you are trying to install packages, try the following:
* Make sure you are online.
* Run `chooseCRANmirror()` from the command line. Pick any mirror.
* Try installing package again. Repeat with a different mirror if it doesn't work.
-----
<hr>
<div style="text-align: center">
<i class="fas fa-cubes"></i> --------------------------------- <i class="fas fa-cubes"></i>
</div>