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.

a <- list(ar=c(.8,.3), name="model", years=1:10)
a
## $ar
## [1] 0.8 0.3
## 
## $name
## [1] "model"
## 
## $years
##  [1]  1  2  3  4  5  6  7  8  9 10

You can now reference the different objects in a.

a$ar
## [1] 0.8 0.3
a$name
## [1] "model"

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:



———————————