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

Hello, For anyone familiar with R coding. I could use some help. I need to run t

ID: 3170419 • Letter: H

Question

Hello, For anyone familiar with R coding. I could use some help. I need to run the regression when I remove X2 from the matrix to create Xnew = cbind(X[,1],X[,3],X[,4]).

The following code is what I used originally for all the variables:

> library(mvtnorm)
> trueBeta = c(5,8,4,9)
> n = 1000;
> numSamp = 1000;
> epsilon = matrix(rnorm(n*numSamp),n)
> xCovar = matrix(c(1,1.5,1.5,3),2,2)
> X = cbind(rep(1,n),rmvnorm(n, cbind(1,2),xCovar), runif(n, min = 0, max = 10))
> k = ncol(X)
> y = matrix(0,n,numSamp)
> beta_ols = matrix(0,k,numSamp)
>
> for (it in 1:numSamp) {
+ y[,it] = X%*%trueBeta + epsilon[,it]
+ beta_ols[,it] = solve(t(X)%*%X)%*%t(X)%*%y[,it]
+
+ }
> beta_ols

How do I run it again excluding the variable X2?

Explanation / Answer

library(boot)
x=frets
y=x[,1]
y
x1=x[,3]
x1
fit=lm(y~x1) # fit simple linear regression model
names(fit)
plot(y~x1,xlab="lcavol",ylab="lpsa")
abline(fit)

m=model.matrix(~x1)
m
xtxi=solve(t(m)%*%m)
betaest=xtxi%*%t(m)%*%y
betaest
b0=betaest[1]
b1=betaest[2]
b0
b1

summary(fit)
coef=fit$coefficient
bo=coef[1]
b1=coef[2]
bo

########for multiple linear regression model
library(boot)
x=frets
y=x[,1]
x1=x[,2]
x2=x[,3]
x3=x[,4]
fit=lm(y~x1+x2+x3) # fit simple linear regression model
names(fit)
plot(y~x1,xlab="lcavol",ylab="lpsa")

m=model.matrix(~x1+x2+x3)
m
xtxi=solve(t(m)%*%m)
betaest=xtxi%*%t(m)%*%y
betaest
b0=betaest[1]
b1=betaest[2]
b0
b1
b2=betaest[3]
b3=betaest[4]
b2
b3
summary(fit)
coef=fit$coefficient
bo=coef[1]
b1=coef[2]
b2=coef[3]
b3=coef[4]
bo
b1
b2
b3
b1

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