Question

In: Math

use R # Problem 4 (5 pts each): # Set x as a vector of 500...

use R

# Problem 4 (5 pts each):
# Set x as a vector of 500 random numbers from Unif(100,300).
# This vector will be kept fixed for the rest of this problem.
#
# (a) Define a function b1(x, beta0, beta1, sigm) that uses the lm() function to
# return the regression line slope b1 for y as a linear function of x, where
#
# y = beta0 + beta1 x + err
#
# and the error term 'err' has a normal N(0,sigm^2) distribution
# (note that standard deviation is equal 'sigm').
#
# Hint: See how the slope b1 is extracted in the initial example of Session 11.


# (b) Replicate the function b1 twenty thousand times for
# beta0 = 15, beta1 = 2, and sigm =10, and store into a vector 'Slopes'.


# (c) Plot the empirical density of Slopes.


# (d) Calculate sample mean and sample variance of Slopes.


# (e) Add to the plot the pdf of a Normal distribution with parameters from part (d).

Solutions

Expert Solution

The R code is given below for the problem


#----------------------R code----------------------
rm(list=ls(all=T))

n=500;beta0=15;beta1=2;sigm=10

set.seed(10)
x=runif(n,100,300) ## Generating vector x form Uniform(100,300)

b1=function(x,beta0,beta1,sigm){ ##defining function b1

err=rnorm(n,0,sigm) ###generating values on err from Norm(0,sigm^2)
y=beta0+beta1*x+err ###generating values on y
b=as.vector(lm(y~x)$coeff) ###fitting the linear regression line y=beta0+beta1+err
b ##vector of slopes
}
b1(x,beta0,beta1,sigm)

Slopes=replicate(20,b1(x,beta0,beta1,sigm)) ###Replicating the function b1 20,000 times and storing to Slopes

m=apply(Slopes,1,mean) ##sample mean of the slopes
### m[1] is mean of beta0 samples and m[2] is mean of beta1 samples

s=apply(Slopes,1,var) ##sample variance of the slopes
### s[1] is sample variance of beta0 and s[2] is sample variance of beta1

plot(density(Slopes[1,]),ylim=c(0,0.32),main="Empirical Density") ###empirical density plot of beta0
curve(dnorm(x,m[1],sqrt(s[1])),col="red",add=T) ##adding plot of Norm(m[1],s[1])

plot(density(Slopes[2,]),ylim=c(0,60),main="Empirical Density") ###empirical density plot of beta1
curve(dnorm(x,m[2],sqrt(s[2])),col="green",add=T) ##adding plot of Norm(m[2],s[2])


Related Solutions

# Problem 5: Use R to compute: # (a)(6 pts) Set three random vectors of 1000...
# Problem 5: Use R to compute: # (a)(6 pts) Set three random vectors of 1000 random numbers as follows: # x1: from Unif(0,5), # x2: from Binom(70,0.2), and # x3: from Exp(1). # Define y = x1 + 4*x2 + 8*x3 + 10 + Err, where Err ~ N(0,sd=10). # (b)(6 pts) Create a dataframe df containing these four variables # and create scatterplots of all pairs of these four variables. # (c) Create a multiple regression model for...
This problem is going to use the data set in R called "ChickWeight" that has 4...
This problem is going to use the data set in R called "ChickWeight" that has 4 variables, as described below. ChickWeight: A data frame with 578 observations on 4 variables. 1) weight: a numeric vector giving the body weight of the chick (gm). 2) Time: a numeric vector giving the number of days since birth when the measurement was made. 3) Chick: an ordered factor with levels 18 < ... < 48 giving a unique identifier for the chick. The...
This problem is going to use the data set in R called "ChickWeight" that has 4...
This problem is going to use the data set in R called "ChickWeight" that has 4 variables, as described below. ChickWeight: A data frame with 578 observations on 4 variables. 1) weight: a numeric vector giving the body weight of the chick (gm). 2) Time: a numeric vector giving the number of days since birth when the measurement was made. 3) Chick: an ordered factor with levels 18 < ... < 48 giving a unique identifier for the chick. The...
This problem is going to use the data set in R called "ChickWeight" that has 4...
This problem is going to use the data set in R called "ChickWeight" that has 4 variables, as described below. ChickWeight: A data frame with 578 observations on 4 variables. 1) weight: a numeric vector giving the body weight of the chick (gm). 2) Time: a numeric vector giving the number of days since birth when the measurement was made. 3) Chick: an ordered factor with levels 18 < ... < 48 giving a unique identifier for the chick. The...
Problem set 2: use the following table to answer questions 4-7 (40 pts total): # of...
Problem set 2: use the following table to answer questions 4-7 (40 pts total): # of children Frequencies 0 472 1 218 2 175 3 320 4 187 5 128 6 47 7 33 8 20 N 1,600 Please find the mean for numbers of children in this distribution (20 pts). # of children Frequencies How many # of children in each category: # of Children * Freq. in each column 0 472 0*472=0 1 218 2 175 3 320...
1. Write the set { x | x ∈ R, x2 = 4 or x 2...
1. Write the set { x | x ∈ R, x2 = 4 or x 2 = 9} in list form. 2. {x: x is a real number between 1 and 2} is an a) finite set b) empty set c) infinite set 3. Write set {1, 5, 15, 25,…} in set-builder form. 4. What is the cardinality of each of these sets? a) {{a}} b) {a, {a}} c) {a, {a}, {a, {a}}} d) {∅} e) {∅, {∅}, {∅, {∅}}}...
Use R studio to do this problem. This problem uses the wblake data set in the...
Use R studio to do this problem. This problem uses the wblake data set in the alr4 package. This data set includes samples of small mouth bass collected in West Bearskin Lake, Minnesota, in 1991. Interest is in predicting length with age. Finish this problem without using Im() (a) Compute the regression of length on age, and report the estimates, their standard errors, the value of the coefficient of determination, and the estimate of variance. Write a sentence or two...
*(4) (a) Prove that if p=(x,y) is in the set where y<x and if r=distance from...
*(4) (a) Prove that if p=(x,y) is in the set where y<x and if r=distance from p to the line y=x then the ball about p of radius r does not intersect with the line y=x. (b) Prove that the set where y<c is an open set. Justify your answer
Problem 4: Suppose M is a random matrix, and x is a deterministic (fixed) column vector....
Problem 4: Suppose M is a random matrix, and x is a deterministic (fixed) column vector. Show that E[x' M x] = x' E[M] x, where x' denotes the transpose of x.
Problem 5. Recall that Mn,n(R) is the vector space of all n by n real matrices....
Problem 5. Recall that Mn,n(R) is the vector space of all n by n real matrices. (a) Show that W = {A | tr(A) = 0} is a subspace of Mn,n(R).? (b) Determine the dimension of W and find a basis for it.? (c) Show that the trace map tr : Mn,n(R) → R is a linear transformation.?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT