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

Quiz: Final


Question 1

Suppose is a normally distributed random variable with mean = 1 and variance = 9. Show and run the R code to find the probability . X P(0 < X < 3)


Question 2

Consider the dataset mtcars (used in our lectures and practices) and read its documentation in RStudio to fully understand the meaning of each column (variable). Then, show and run the R code to find the second smallest displacement for 3-forward gear cars having a weight strictly less than 5000lb.


Question 3

A simple random sample of 5 Phillips Hall students weighed 132, 145, 162, 166, and 175 pounds. A simple random sample of 4 Tripp Hall students weighed 137, 147, 158, and 170.

Suppose that each hall's population of weights is normally distributed. Show and run the R code to find the p-value for a test of whether the population mean weights of Phillips and Tripp students are the same (You need to determine which test is appropriate here).

AND write down the alternative hypothesis explicitly

AND use the significance level of 0.05 to draw a conclusion of the test.


Question 4

Sort the rows of mtcars by decreasing horsepower (hp), breaking ties by increasing displacement (disp). Show and run the R code to find the mileage (mpg) of the first six-cylinder (cyl) car in the sorted data.frame.


Question 5

Set the seed for random number generation to 1.

Show and run the R code to estimate the variance of the maximum of random samples of size n=50 from the uniform distribution on the interval [10, 20]. To do this, simulate N=5000 random samples by using replicate() (no for or repeat loop, Otherwise some points will be docked), find the maximum of each sample, and then find the sample variance of these maxima.


Question 6

Divide mtcars into three groups of data frames according to the number of cylinders in the car's engine. Make a sub-scatterplots of gas mileage on the y-axis vs. weight on the x-axis for each group (so, three subplots in total). The points should be colored , with 4-cylinder cars colored red, 6-cylinder cars colored blue, and 8-cylinder cars colored green. Then, put three subplots into one single plot by the following layout: 4-cylinder cars on the upper-left corner, 6-cylinder cars on the upper-right corner with the same size, 8-cylinder cars on the bottom with the double size. There is no blank area in the plot.

To draw the plot above, someone wrote R code with some comments, bugs, and bad style as follows:

rm(list = ls()) # initialization
m=mtcars # load mtcars into m
m4cyl=m[m$cyl=4];m6cyl=m[m$cyl=6];m8cyl=m[m$cyl=8] # divide m into 3 groups of data f
rames: 4cyl(m4cyl), 6cyl(m6cyl), 8cyl(m8cyl)
n=matrix(data=(1,2,3,3),nrow=2,ncol=2) # set up the layout matrix
Layout(n) # apply the layout matrix
layout.show(layout(n)) # for test only. please comment out this line after the finali
zation of your code.
plot(x=m4cyl$wt,y=m4cyl$mpg,main="Mileage vs. Weight (4 cyl)",xlab="weight (pounds)",
ylab="mileage (mpg)",col=red # draw the 1st subplot on the upper-left corner. keep th
e main title and labels intact.
plot(x=m6cyl$wt,y=m6cyl$mpg,main="Mileage vs. Weight (6 cyl)",xlab="weight (pounds)",
ylab="mileage (mpg)",col=blue) # draw the 2nd subplot on the upper-right corner. keep
the main title and labels intact.
plot(x=m8cyl$wt,y=m8cyl$mpg,main="Mileage vs. Weight (8 cyl)",xlab="weight (pounds)",
ylab="mileage (mpg)",col=green # draw the 3rd subplot on the bottom. keep the main ti
tle and labels intact.
# is some code missing after plotting for the layout reset?
# if yes, you MUST put one line of code for it in the following blank line.
# if no, skip it

Please copy and paste this R code into RStudio, then read and follow the comments at the end of each line to understand the purpose of each line (just like what I highly recommended for reading my own lecture notes).

Then, don't rewrite R code from the scratch. Just try your best to debug line by line and make the code draw the desired plot. Also, follow the comments to finish other simple tasks if required.

Finally, copy and paste your corrected code after debugging as your answer in the following blank.

You can make your code with an improved style (like my lecture code) and briefly describe your debugging procedure under your code if you can (optional).


Question 7

Answer all the following questions and show how to find your answers by inputing some simple commands on the console and searching in the R documentation:

By default, does lines() create a new graph or add to an existing graph?

By default, does curve() create a new graph or add to an existing graph?

How to switch curve() from creating a new graph to adding to an existing graph, or vice versa?

Again, you will lose some points if you don't explain why.


Question 8

Suggest at least two meaningful ways briefly to improve STAT 303.