代写 R STATS 380, 2019 Assignment 2 Due: 8pm Fri 20 Sep

STATS 380, 2019 Assignment 2 Due: 8pm Fri 20 Sep
Submit your assignment online on Canvas before due date. Your submission should include two files: a written report in MS Word or PDF format that contains your answers to the assignment questions, and an R file that includes all your R code. Both files should have the same base name of the form “assign1-surname -firstname -upi ”, followed with appropriate name extensions (doc/docx/pdf/r). For example, James Bond would submit his files with the base name “assign1-bond-james-jbon007”.
In general, you should provide examples and intermediate results to illustrate how your code works when it is not obvious to understand. Comment your code appropriately. Unless asked for, avoid using explicit loops (or anything equivalent) if ever possible.
1. [15 marks] Write a function named Dist that inputs a distance matrix, where the element in row i, column j gives the distance between city i and city j and outputs the minimum one-hop distance between cities and the pair of cities that achieves that minimum.
Taking the following matrix as an example,
your answer should be
> Dist(x)
Minimum_distance 423
0 5 7 x=5 0 4
740
City_i
City_j
1

2. [10 marks] Figure 1 demonstrates the scatter plot of both land distance and flush distance using the data Beach-Birds.csv on Canvas. Write R code to produce this figure. Note that to add to an existing plot use argument new = TRUE in par().
20 200
15 150
10 100
5 50
00 0 100 200 300 400
Individual
Figure 1:
2
Flush distance
Land distance

3. [25 marks] Produce the following figure using the data Beach-Birds.csv on Canvas. For producing Figure 2 you need to write a function named strat.hist() with the following definition
and then the following R code will produce this figure.
Each histogram shows not only the total frequency on each interval, but also the fre- quencies for all species by using different colours.
> strat.hist= function(y, f, g, breaks = 15, xlab=NULL, main=””){ }
# y: numeric variable (flush.dist)
# f: factor variable (species)
# g: gender
# breaks: number of cells for the histogram
> data= read.csv(Beach-Birds.csv)
> with(data, strat.hist(flush.dist, Species, Sex,
main = “Frequency of Flush distance for different species”))
Female
Frequency of Flush distance for different species
20
15
10
5
Male
Gull Oystercatcher Plover
Stint
0
25 20 15 10 5 0 0 5 10 15 20 25
Frequency
Figure 2:
Hint: You may start with writing a function named coord.hist with function defini- tion:
> coord.hist= function(y, f, g, breaks){ }
which returns the coordinates and breackpoints of each rectangle shown in sub-figures. For writing this function after finding the breakpoints in hist(), you may need to count the number of observations (for both genders) of each species in each interval. Then using the cumsum() function you can identify the coordinates.
3