In: Statistics and Probability
1. Basic use of R/R Studio. Solve the following problem in R and print out the commands and outputs.
(a) Create a vector of the positive odd integers less than 100; Remove the values greater than 60 and less than 80; Find the variance of the remaining set of values
(b) What’s the difference in output between the commands 2*1:5 and (2*1):5? Why is there a difference?
(c) If you wanted to enter the odd numbers from 1 to 19 in the variable x, what command would you use?
(d) If you create a variable using the following command y=c(-1,2,-3,4,-5), what command would put the positive values of y into the variable z?
(e) What R command would give you the 95th percentile for a chi-squared distribution with 10 degrees of freedom?
(f) Generate a vector of 1000 standard normal random variables using the command x=rnorm(1000), use R to give a five number summary of your simulated data; what is the mean and variance of your x variable? Make and print a histogram for this data.
rm(list=ls())
(a)
#Create a vector of the positive odd integers less than
100
a=seq(from = 1, to = 100, by = 2)
a
[1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
47 49
[26] 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91
93 95 97 99
#Remove the values greater than 60 and less than 80
b=a[-31:-40]
b
[1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
47 49
[26] 51 53 55 57 59 81 83 85 87 89 91 93 95 97 99
#variance of the remaining set of values
var(b)
[1] 931.2821
#########################################################################
(b)
#Difference in output between the commands 2*1:5 and
(2*1):5
2*1:5 # here first work 1:5 then multiply by 2
[1] 2 4 6 8 10
(2*1):5 # here first work 2*1 then work 2:5
[1] 2 3 4 5
##########################################################################
(c)
#If you wanted to enter the odd numbers from 1 to 19 in the
variable x
x=seq(1,19,2)
x
[1] 1 3 5 7 9 11 13 15 17 19
############################################################################
(d)
# If you create a variable using the following command
y=c(-1,2,-3,4,-5),
# what command would put the positive values of y into the variable
z?
z=y[y>0]
z
[1] 2 4
#####################################################################################
(e)
# R command would give you the 95th percentile for a chi-squared
distribution with 10 degrees of freedom.
qchisq(0.95,10)
[1] 18.30704
######################################################################################
(f)
x=rnorm(1000)
# summary of given variable
summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-3.13872 -0.64016 -0.03391 -0.00435 0.66116 3.22207
# mean of x
mean(x)
[1] -0.004350421
# variance of x
var(x)
[1] 1.0273
# Histogram of this data