2.1, 2.2, 2.4, 2.7 and 2.8. Chapter 2 exercises at pages 49-53 of the textbook.
ANSWER
x<-c(seq(0,1,0.1))
x
## [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
y<-c(seq(1,10.3,1))
y
## [1] 1 2 3 4 5 6 7 8 9 10
funct<-function(leng){
empty_list <- vector(mode='list', length=leng)
for (z in 1:leng){
empty_list[[z]]<-seq(1:z)
}
vt<-unlist(empty_list)
print(vt)
}
funct(5)
## [1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
If X behaves as a standard normal, then X2 behaves as chi-squared with 1 df. The cumulative chi-squared distribution can be found in R using the function pchisq(y,d) where y is the ordinate and d is the degrees of freedom. Use this function to find the probability of a 1 df chi-squared variate taking values between 0.5 and 1.2.
Answer
pchisq(1.2,1) - pchisq(0.5,1)
## [1] 0.2061784
How useful is the histogram to detect normally distributed data?
Use runif(50) to generate a set of 50 random values, distributed uniformly between zero and one. Draw the histogram of these using hist(). Do these look normally distributed?
x <- runif(50)
hist(x)
Answer
the distribution does not look normal
Generate 50 random values, each the sum of 3 uniforms and plot the histogram. Do these look normally distributed? Suppose we looked at the histogram of the sum of 20 uniforms. How many uniform random values do we need to add together to produce a “normal-looking” histogram?
genHist <- function(k){
b <- c()
for (i in 1:k)
{
##random values
b <- append(b,runif(50))
}
return(b)
}
graphNums<- c(3,20)
for(i in 1:length(graphNums)){
z <- genHist(graphNums[i])
graphNames <- c("3 uniforms", "20 uniforms")
hist(z, main = graphNames[i], col="blue")
}
Answer The most normal histograms are closer to 100
genHist <- function(k){
b <- c()
for (i in 1:k)
{
##normaly distributed values
b <- append(b,rnorm(50))
}
return(b)
}
graphNums<- c(3,20)
for(i in 1:length(graphNums)){
z <- genHist(graphNums[i])
graphNames <- c("3 uniforms", "20 uniforms")
hist(z, main = graphNames[i], col="blue")
}
Answer . the larger the sum the more closer to normal the histogram looks
As a comparison, look at a histogram of 50 normal random values generated using rnorm(). How normal does this histogram appear?
hist(rnorm(50))
Answer
This is a normal histogram
$ f(x) = $
#ratf <- curve(1/(-5*x+2))
ratf <- curve(1/(sin(5*pi*x)+2))
inverse1<-curve(-1/(sin(5*pi*x)+2))
ratf
## $x
## [1] 0.00 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10 0.11 0.12 0.13 0.14
## [16] 0.15 0.16 0.17 0.18 0.19 0.20 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29
## [31] 0.30 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.40 0.41 0.42 0.43 0.44
## [46] 0.45 0.46 0.47 0.48 0.49 0.50 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59
## [61] 0.60 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.70 0.71 0.72 0.73 0.74
## [76] 0.75 0.76 0.77 0.78 0.79 0.80 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89
## [91] 0.90 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.00
##
## $y
## [1] 0.5000000 0.4637284 0.4330847 0.4074995 0.3864308 0.3693981 0.3559964
## [8] 0.3459003 0.3388617 0.3347069 0.3333333 0.3347069 0.3388617 0.3459003
## [15] 0.3559964 0.3693981 0.3864308 0.4074995 0.4330847 0.4637284 0.5000000
## [22] 0.5424272 0.5913720 0.6468266 0.7081076 0.7734591 0.8396425 0.9017186
## [29] 0.9533402 0.9878381 1.0000000 0.9878381 0.9533402 0.9017186 0.8396425
## [36] 0.7734591 0.7081076 0.6468266 0.5913720 0.5424272 0.5000000 0.4637284
## [43] 0.4330847 0.4074995 0.3864308 0.3693981 0.3559964 0.3459003 0.3388617
## [50] 0.3347069 0.3333333 0.3347069 0.3388617 0.3459003 0.3559964 0.3693981
## [57] 0.3864308 0.4074995 0.4330847 0.4637284 0.5000000 0.5424272 0.5913720
## [64] 0.6468266 0.7081076 0.7734591 0.8396425 0.9017186 0.9533402 0.9878381
## [71] 1.0000000 0.9878381 0.9533402 0.9017186 0.8396425 0.7734591 0.7081076
## [78] 0.6468266 0.5913720 0.5424272 0.5000000 0.4637284 0.4330847 0.4074995
## [85] 0.3864308 0.3693981 0.3559964 0.3459003 0.3388617 0.3347069 0.3333333
## [92] 0.3347069 0.3388617 0.3459003 0.3559964 0.3693981 0.3864308 0.4074995
## [99] 0.4330847 0.4637284 0.5000000
inverse1
## $x
## [1] 0.00 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10 0.11 0.12 0.13 0.14
## [16] 0.15 0.16 0.17 0.18 0.19 0.20 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29
## [31] 0.30 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.40 0.41 0.42 0.43 0.44
## [46] 0.45 0.46 0.47 0.48 0.49 0.50 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59
## [61] 0.60 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.70 0.71 0.72 0.73 0.74
## [76] 0.75 0.76 0.77 0.78 0.79 0.80 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89
## [91] 0.90 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.00
##
## $y
## [1] -0.5000000 -0.4637284 -0.4330847 -0.4074995 -0.3864308 -0.3693981
## [7] -0.3559964 -0.3459003 -0.3388617 -0.3347069 -0.3333333 -0.3347069
## [13] -0.3388617 -0.3459003 -0.3559964 -0.3693981 -0.3864308 -0.4074995
## [19] -0.4330847 -0.4637284 -0.5000000 -0.5424272 -0.5913720 -0.6468266
## [25] -0.7081076 -0.7734591 -0.8396425 -0.9017186 -0.9533402 -0.9878381
## [31] -1.0000000 -0.9878381 -0.9533402 -0.9017186 -0.8396425 -0.7734591
## [37] -0.7081076 -0.6468266 -0.5913720 -0.5424272 -0.5000000 -0.4637284
## [43] -0.4330847 -0.4074995 -0.3864308 -0.3693981 -0.3559964 -0.3459003
## [49] -0.3388617 -0.3347069 -0.3333333 -0.3347069 -0.3388617 -0.3459003
## [55] -0.3559964 -0.3693981 -0.3864308 -0.4074995 -0.4330847 -0.4637284
## [61] -0.5000000 -0.5424272 -0.5913720 -0.6468266 -0.7081076 -0.7734591
## [67] -0.8396425 -0.9017186 -0.9533402 -0.9878381 -1.0000000 -0.9878381
## [73] -0.9533402 -0.9017186 -0.8396425 -0.7734591 -0.7081076 -0.6468266
## [79] -0.5913720 -0.5424272 -0.5000000 -0.4637284 -0.4330847 -0.4074995
## [85] -0.3864308 -0.3693981 -0.3559964 -0.3459003 -0.3388617 -0.3347069
## [91] -0.3333333 -0.3347069 -0.3388617 -0.3459003 -0.3559964 -0.3693981
## [97] -0.3864308 -0.4074995 -0.4330847 -0.4637284 -0.5000000
Find the area under this curve using numerical quadrature with integrate().
ratf1 <- function(x,...){
return (1/(2+sin(5*pi*x)))
}
integrate(ratf1,lower = 0, upper = 1)
## 0.5388603 with absolute error < 6.5e-07
Identify maximums and minimums of this function in R using nlm(). Show how the results depend on the starting values.
nlmmax<-function(f,p,...){
res<- nlm(function(x,...) -f(x,...),p,...)
resMax<- -res$minimum
res<- nlm(ratf1,p,...)
res$maximum<- resMax
return(res)
}
nlmmax(ratf1,0)
## $minimum
## [1] 0.3333333
##
## $estimate
## [1] 0.0999995
##
## $gradient
## [1] 0
##
## $code
## [1] 1
##
## $iterations
## [1] 5
##
## $maximum
## [1] 1