• Please upload your assignment to Brightspace before the due date Wednesday noon (12:00 pm), 31 Jan
2018.
• Submit your assignment in PDF format and name it as A1_YourName_B00##.pdf
• R should be used exclusively and the R code must be provided in your assignment.
• Show the results clearly; the code must be executable and comment your code.
• Please remember that all of the work must be your own, and answers must be given in your own words.
• Let me know if you run into any trouble.
Q1
[1 point] 1. Create a vector “vec” with 20 random numbers from 1 to 100 (sample() function) and print it.
(Use the last 3 digits of your student number to set a random seed.)
[1 point] 2. Return the last 5 elements in this vector
[1 point] 3. Use this vector to create a 5×4 matrix, and print out the matrix.
[2 points] 4. Get all the values divisible by 3 in this matrix (Use R!).
[2 points] 5. Return all the rows where the value of the second column is greater than the first column.
Q2
[2 points] 1. Download the file Credit.csv from http://www-bcf.usc.edu/~gareth/ISL/data.html, import the
data to R.
[2 points] 2. How many rows and columns in this data set (not including row numbers)?
[1 point] 3. How many male students in this data set?
1
[2 points] 4. Plot a scatter graph using Age (x) against Income (y) and color the points blue if gender is male
and red for female.
[1 point] 5. Add a horizontal line at the position of the average income (y = mean of income).
[1 point] 6. Randomly choose 10 rows from the data frame. Credit and make it a new data frame. df2. (hint:
sample() function)
[2 point] 7. Sort the new data frame. df2 by column Income in descending order. Print out the results.
[1 points] 8. Save the data df2 to your desktop and use “|” to separate the columns. (You only need provide
the code.)
Q3
[3 points] 1. Calculatesummationtext3z=1summationtext4y=1summationtextyx=1 exy+z
[3 points] 2. Use R to find all the prime numbers between 20 and 50. (Prime numbers: can be divided evenly
only by 1 or itself.)
[4 points] 3. Create a function that returns the elements on odd positions in a vector.
Input: a vector
Output: a vector of the elements only on odd positions
Use the vector in your first question as an example and show the results.
[5 points] 4. Create a function to calculate the confidence intervals. This function should be able to handle
small data sets (n = 30 using normal distribution).
Input: a data set; alpha (confidence level)
Output: confidence interval (lower boundary and upper boundary)
Given the following sample data of battery life: 491, 485, 503, 492, 482, 487, 490, 485, 495, 502 calculate the
95% confidence interval of the mean battery life.
[1 point] 2. Randomly generate 1000 samples from a normal distribution (choose any mean and sd you like),
and assign them to a variable. (Use the last 3 digits of your student number to set a random seed.)
[1 point] 3. Use these 1000 samples to draw a histogram.
[2 points] 4. Add a curve of this norm distribution (using the mean and sd you set) on that histogram. (Be
sure that y axis is consistent.)
[2 points] 5. Add vertical lines at 0.025 and 0.975 quantile
[2 points] 6. Add texts beside those two vertical lines to show the quantile values (“x =”).