Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

DATA 101

Assignment 3

Please submit your assignment as an R script le named with your last name, student number,  assignment number and with the suffix R. For example,  if Joe Smith,  student number 87654321 hands in Assignment 4, he would name the file Smith87654321A2 .R.

Within your answer le, include answers with your R code preceded by the # sign.  For example, to answer the 5th question on an assignment which is  Perform the calculation 2 + 2”, you would type

#  Question  5

2  +  2  #coding

#  4  (your  answer here  )

Due Date: November 14, 2022

In each question below, type the required lines of R code, together with the answer to the question.

1. If you have not already done so, install the DAAG package, either using the menu system in RStudio or by typing install .packages(DAAG) at the command prompt in

R. The data frame Manitoba .lakes concerns the elevation and area of the largest lakes in Manitoba.

(a) Obtain a scatter plot of area versus elevation for these lakes.  Comment on any noteworthy observations. (2 points)

(b) Use the xlim argument and adjust the character size when using the text function to add the row names to obtain a graph that appears as below: (5 points)

 

Winnipeg 

 

 

 

 

 

 

Mani(W)t(i)o(n)b(n)ia(p)egosis 

 

Gods   

Cross   Playgreen 

SouthernIndian

Cedar

160                 180                 200                 220                 240                 260

elevation

You will also need to use the adj argument in the call to the text function.          (c) Using the lm() function, fit the least-squares line relating area to elevation and

overlay this line on your plot using the abline() function. (2 points)

(d) Make a new data frame called ML that contains two columns.  One column called elevationdivided100 will be the value of elevation column in Manitoba .lakes divided by 100. The other column called areadivided1000 will be the value of area column in Manitoba .lakes divided by 1000.  Redo the previous questions using variables areadivided1000 and elevationdivided100 in the new data frame.  (6 points)

2. Use the help function to learn how to use the function unique. Give an example of its proper use. (3 points)

3. Plot height versus age for the pine tree growth data in Loblolly, and overlay the best-fit line obtained from the lm() function. (5 points)

4. Do you think that the straight line is the best way to represent these data?  Explain briefly. (2 points)

5. The Loblolly pine data actually contains age and height measurements for a number of different trees, grown from different seed sources.

❼ write code that attempts to t straight lines to the height and age data for each different tree (represented by the factor Seed). Make a data.frame called coef .df to show all slopes and intercepts are obtained for each line and row names correspond to each different tree. (10 points)

❼ Construct another scatter plot of height versus age for all of the trees, and use a for loop to overlay all n of the lines corresponding to the different slopes and intercepts obtained with the above code. (2 points)

6. Refer to the previous exercise.  Obtain side-by-side histograms( in a 1 by 2 layout) of the slopes and intercepts of the lines that were obtained with the code in the previous question. (4 points)

7. Refer to the previous two exercises.   Obtain a scatter plot of the slopes versus the intercepts, overlaying the best-fit line.

If you had to describe a “typical” line which relates height to age for these kinds of trees, what would you say is a typical slope and what would you say is a typical intercept? (4 points)

8. You can use the following two formulas to to generate n uniform numbers between 0 and 1.

北n  = (7北n − 1 ) mod 17

u  = 北n /17

and

yn  = (13yn − 1 ) mod 29

uy  = yn /29

Now let zn  = (u + uy ) mod 1. Then zn  is also between 0 and 1.

❼ Write code to generate 16 X 28 = 728 uniform numbers between 0 and 1 of the form zn  assuming that 北0  = 3 and y0  = 9. (10 points)

❼ Using the function unique to see how many values are generated before they are repeated in the vector z. (1 point)

❼ Simulate 448 uniform numbers between 0 and 1 using the built-in function runif. Using the function unique to see how many values are not repeated in one simula- tion run. (1 point)

❼ Compare and describe the result from last two previous parts and explain why there is a difference. (3 points)