# Simulation of Measurement Error in Predictor Model for Simple # Linear Regression # Set random number generator seed to duplicate data with # changes only in the parameters # Beta weight for predicting y from the true score part # of x.1 (x.t) beta.x.t<-.6 # Error in y after accounting for x.t y.error<-(1-beta.x.t^2)^.5 # Sample size sample.size<-50 # Create residuals in predicting y scores after accounting for # the true score part of predictor x.1 (x.t) y.e<-rnorm(sample.size, 0, y.error) # Create true score component of x.1 predictor scores x.t x.true<-.6 x.true.var<-x.true^2 x.t<-rnorm(sample.size,0,x.true.var^.5) # Error in x x.e<-rnorm(sample.size,0,(1-x.true.var)^.5) # Create observed y scores with true score component of x1 (x.t); the beta # weight between y and x.t; and the error in predicting y after accounting # for x.t y<- beta.x.t * x.t + (y.e) y # Create observed x scores (x.1) with the sum of true score part of x.1 (x.t) # and the measurement error part of x.1 (x.e) x.1<-x.t+x.e # Regression Analysis of y and x.1 # Here we use "scale" function to convert raw scores to z scores y.x.lm<-lm(scale(y)~scale(x.1)) plot(y.x.lm) plot(x.1,y) abline(y.x.lm) summary(y.x.lm) ########################################################## # # Bootstrap Analysis of Regression Model with measurement errors # in the predictor x.1 and the outcome variable y library(Hmisc) library(Design) library(boot) library(simpleboot) # Apply bootstrap validation to the Measurement Error Regression model # (similar to cross validation) y.x.ls<-ols(scale(y)~scale(x.1), x=TRUE, y=TRUE) validate(y.x.ls, B=1000) y.x.ls anova(y.x.ls) # A simple bootstrap analysis of the correlation with errors in measurement cor.boot<-pairs.boot(scale(y),scale(x.1),FUN=cor,R=1000) boot.ci(cor.boot) mean(cor.boot$t) mean(cor.boot$t)^2