I\'m supposed to do the following exercise In R Code , but I can only do Part 1.
ID: 3301020 • Letter: I
Question
I'm supposed to do the following exercise In R Code, but I can only do Part 1. I don't know how to combine the variables I created for parts 2, 3 and 4. Can you help with the R Code?
1. Denerate 500 observations xi from the uniform distribution with range [0, 10]. Randomly generate 500 errors, ui from the normal [0, 36] distribution (i.e., a normal random variable with mean zero and variance 36).
2. Now generate yi = 1 + 2xi + ui = 0 + 1xi + ui . This means 0 = 1 and 1 = 2, or the population intercept is one and the population slope is two. What are your estimates of the intercept and slope? Are they equal to the population values in the above equation? Explain.
3. Obtain the OLS residuls of ui , the ui hat, and verify if their sum equals to zero.
4. Compute the errors ui = yi - yi hat , Do their sum equal to zero?
Explanation / Answer
The R snippet is as follows
# the variable
xi <- runif(500, min=0 , max=10)
# the error term
ui <- rnorm(500,mean=0 , sd = 6)
# generate yi
yi <- 1 + 2*xi +ui
## fit the regression as given above
fit <- lm(yi ~ xi + ui )
summary(fit)
fit$residuals
sum(fit$residuals) # very close to zero , almost zero
## almost zero , sum of y-yhat
sum(fit$fitted.values - yi)
If you run them in R studio , you would see the exact results as asked in the question.