-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice_solns.R
57 lines (48 loc) · 1.81 KB
/
practice_solns.R
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
## Part 1
## 1
aj.assess <- filter(olaf.assessments, grepl('amberjack', stocklong, ignore.case = TRUE))
aj.assess
amberjack <- filter(timeseries, stockid == aj.assess$stockid[2])
## 2
aj.recruit <- filter(amberjack, tsid == 'R-E00')
summarize(aj.recruit, tsyear[which.max(tsvalue)])
arrange(aj.recruit, desc(tsvalue))
filter(aj.recruit, rank(-tsvalue) == 1)
## 3
ggplot(data = filter(amberjack, tsid == 'SSB-MT', tsyear >= 1965, tsyear <= 2005),
aes(x = tsyear, y = tsvalue)) +
geom_line() + geom_point(size = 2) +
xlab('Year') + ylab('SSB (mt)')
plot(tsvalue ~ tsyear, type = 'l', xlab = 'Year', ylab = 'SSB (mt)', las = 1,
data = filter(amberjack, tsid == 'SSB-MT', tsyear >= 1965, tsyear <= 2005))
## Part 2
## 1
pac.herring <- filter(stock, scientificname=='Clupea pallasii') %>%
inner_join(assessment) %>%
inner_join(timeseries) %>%
select(stocklong, tsid, tsyear, tsvalue) %>%
group_by(stocklong, tsid)
## 2
summarize(pac.herring, mn = mean(tsvalue, na.rm=TRUE),
stdev = sd(tsvalue, na.rm=TRUE)) %>%
View()
## 3
filter(pac.herring, tsid == 'SSB-MT') %>%
ggplot() + aes(x=tsyear, y=tsvalue) + geom_line() +
facet_wrap(~ gsub('Pacific herring', '', stocklong)) +
xlab('Year') + ylab('SSB (mt)')
par(mfrow = c(3,3), mar = c(2,2,2,2))
filter(pac.herring, tsid == 'SSB-MT') %>%
do(temp = plot(.$tsyear, .$tsvalue, type = 'l',
xlab = '', ylab = '',
main = gsub('Pacific herring', '',
.$stocklong[1])))
## Bonus
ungroup(pac.herring) %>%
mutate(tsid = gsub('-', '_', tsid)) %>%
spread(key = tsid, value = tsvalue) %>%
filter(!is.na(SSB_MT)) %>%
ggplot() + aes(x = tsyear, y = SSB_MT) +
geom_point(aes(col=ER_ratio)) +
geom_line() + xlab('Year') + ylab('SSB (mt)') +
facet_wrap(~ gsub('Pacific herring', '', stocklong))