Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. A simple simulation for Bernoulli random variables, and logistic regression p

ID: 3201428 • Letter: 3

Question

3. A simple simulation for Bernoulli random variables, and logistic regression part 1. Suppose we have a single covariate z. Generate n 1000 independent Bernou (T) random variables (Y i 1,...,n), according to the model logit (Au) Bo Bar, i 1,-- in where E(Y), with Bo 1, Bi 0.5, and design points zi regularly spaced on the interval (-10,10) Hints for coding in R: (a) You can use command seq to generate regularly spaced design points. (b) You can do all steps using vectors you don't need any loops here. (c) After assigning values for n, Bo, Bi, and (za: i 1, 1000 calculate the corresponding true T, values for i 1....,n. (Hints: what is the relationship between pu and t, for Bernoulli and what link is be- ing used in the model above? Hence write down the equation for each nr, in terms of the corresponding zi and your parameter values. Use this equation to calculate the vector of success probabilities.) (d) Plot logit (T) (vertical axis) versus (horizontal axis) for these n 1000 points. (e) Plot (vertical axis) versus z. (horizontal axis) for these n 1000 points. (f) Now use R command "rbinom", with the appropriate parameter vector and constants, to simulate Y Bernoulli(T). What are the only possible values for Y? Save your results as a vector Y. Now fit a logistic regression model to your simulated data vector, using the command glumi gin y x, family binomial) where y is your vector of simulated Y values, and x is the vector of your design points. Paste the code you wrote into the document that you will hand in for this homework. Paste the results of "summary(glml)" into the document you will hand in for this homework. Note that the summary indicates that the value of the dispersion parameter for the Binomial is taken to equal 1 (see in notes for Binomial). What are the values of your parameter estimates Ao and Bi? Note: in week 2 of lectures we have not yet covered the deviance residuals, the null de- viance, the residual deviance and the associated hypothesis tests but we will study these in upcoming lectures. After we have studied these, you should be able to interpret the results of this summary. In addition to the material pasted into the document you hand in, keep the code in a file, since you may need this in another homework later.

Explanation / Answer

Dear Student,

I have completed the R coding for the question mentioned. I am pasting the R code below. If you find difficulty to follow any step, i have included the comments after every line. It may help you to understand the code better.

#### R CODE BEGINS

rm(list = ls(all = T))

library(LaplacesDemon) # Package to generate bernoulli random variabes

Beta0=1; Beta1=0.5 # parameter values given in question

pie=rep(); lgt=rep(); X=rep(); Y=rep() # defining some vectors for use

for(i in 1:1000) # loop for generating Pi and Logit.
{
y=rbern(1000,0.5);
pie[i]=mean(y);
lgt[i]=logit(pie[i]);
X[i]=(lgt[i]-Beta0)/(Beta1);
  
}

plot(pie,lgt,main="Plot Between Pi and Logit Pi") # Plot Between Pi and Logit

plot(pie,X,main="Plot Between Pi and X") # Plot Between Pi and X


for(i in 1:1000) # loop for defining vector Y
{
Y[i]=rbinom(1,1,pie[i])
}

summary(Y)

Model=glm(Y ~ X, family=binomial) # GLM for Y and X

summary(Model) # Summary of GLM

#### R CODE ENDS

Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote