0% found this document useful (0 votes)
14 views

Assignment of Regression.

This document outlines a Monte Carlo simulation to estimate regression parameters and their uncertainties. It generates random data based on assumed true intercept and slope values, fits a regression model, and repeats this process many times. It then calculates average and variance estimates for the intercept, slope, and error variance across iterations. Confidence intervals are constructed and the proportion of iterations consistent with the true values are reported.

Uploaded by

iqra mumtaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Assignment of Regression.

This document outlines a Monte Carlo simulation to estimate regression parameters and their uncertainties. It generates random data based on assumed true intercept and slope values, fits a regression model, and repeats this process many times. It then calculates average and variance estimates for the intercept, slope, and error variance across iterations. Confidence intervals are constructed and the proportion of iterations consistent with the true values are reported.

Uploaded by

iqra mumtaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

REGRESSION ASSIGNMENT

> B0<- 50 # intercept

> B1<- 3 # Slope

> n<-25 # sample size

> M<-500 # No of iterations/experiments

> X_i = seq(1:25) # independent variable

> intrcpt <- rep(0,M)

> slop <- rep(0,M)

> Mse<-rep(0,M)

> y_hat<- matrix(0,n,M)

> # =============== Begin Monte Carlo simulation============#

> for(i in 1:M){

+ U_i = rnorm(n, mean = 0, sd = 2) # error

+ Y_i = B0 + B1 * X_i + U_i

+ model <- lm(Y_i ~ X_i)

+ intrcpt[i] <- model$coefficients[1]

+ slop[i] <- model$coefficients[2]

+ y_hat<-predict(model)

+ Mse[i]<-sum((Y_i-y_hat)^2)/(n-2)

+}

> # ========= create a data frame for slope and intercept estimates =======#

> df <- data.frame(Beta_0 =intrcpt , Beta_1 = slop)

> #=========== Avarage valueof Bo,B1 ======================#

> Avg.Bo<-mean(df$Beta_0)

> Avg.Bo
 [1] 50.04963
> Avg.B1<-mean(df$Beta_1)

> Avg.B1

 [1] 2.996082
> #===== Standard Eror and sampling variance Bo,B1 ========#

> SE.Bo<-sd(df$Beta_0)

> SE.Bo

 [1] 0.8081315
> SE.B1<-sd(df$Beta_1)

> SE.B1

 [1] 0.05340573
> var.Bo<-var(df$Beta_0)

> var.Bo

 [1] 0.6530765
> var.B1<-var(df$Beta_1)

> var.B1

 [1] 0.002852172
> cat("Beta0 mean=", Avg.Bo, "\n","Beta0 Var=", var.Bo, "\n","Beta0 SE=", SE.Bo, "\n")

 Beta0 mean= 2.996082


 Beta0 Var= 0.6530765
 Beta0 SE= 0.8081315
> cat("Beta1 mean=", Avg.B1, "\n","Beta1 Var=", var.B1, "\n","Beta1 SE=", SE.B1, "\n")

 Beta1 mean= 2.996082


 Beta1 Var= 0.002852172
 Beta1 SE= 0.05340573
 #====================== C.I for B0 ======================#

> Ucl<- df$Beta_0+ 1.96*SE.Bo

> Lcl<- df$Beta_0 - 1.96*SE.Bo

> #============== how many sample contain if true Bo=50 ========#

> countBeta_0<-sum(Lcl<50&Ucl>50)

> countBeta_0

 [1] 476
> #============= C.I B1 ================#

> LCL<-df$Beta_1-1.96*SE.B1

> UCL<- df$Beta_1 + 1.96*SE.B1

> #========== How many sample containif true B0=50 ==============#

> countBeta_1<- sum(LCL<3&UCL>3)

> countBeta_1

 [1] 478
> #========== C.I for true sigma =================#

> CR1<-qchisq(0.025,n-2)

> CR1

 [1] 11.68855
> CR2<-qchisq(0.975,n-2)

> CR2

 [1] 38.07563
> lcl.sigma<-(n-2)*Mse/(CR2)

> Ucl.sigma<-(n-2)*Mse/(CR1)

> #============== How many vlues lie in sigma=4 ==========#

> countsigma.hat<-sum(lcl.sigma<4&Ucl.sigma>4)
> countsigma.hat

 [1] 479

You might also like