library(lmtest)
## Loadingrequired package: zoo
##
## Attaching package: 'zoo'
## The followingobjects are masked from 'package:base':
##
## as.Date,as.Date.numeric
library(MASS)
library(car)
## Loadingrequired package: carData
library(caret)
## Warning:package 'caret' was built under R version 3.6.2
## Loadingrequired package: lattice
## Loadingrequired package: ggplot2
x = rep(1:100)
a = 10
b = 2
sigma2_2 = x*10
eps = rnorm(x,mean=0,sd=sqrt(sigma2_2))
y2 = a+b*x+ eps
model2 = lm(y2~ x)
sigma2_3 = x^2*10
eps = rnorm(x,mean=0,sd=sqrt(sigma2_3))
y3 = a+b*x+ eps
model3 = lm(y3~ x)
bptest(model2)
##
## studentizedBreusch-Pagan test
##
## data: model2
## BP = 17.713, df = 1, p-value = 2.569e-05
bptest(model3)
##
## studentizedBreusch-Pagan test
##
## data: model3
## BP = 22.914, df = 1, p-value = 1.694e-06
model2_new <- lm(y2~x, weights = 1/sqrt(x*10))
summary(model2_new)
##
## Call:
## lm(formula = y2 ~ x, weights = 1/sqrt(x * 10))
##
## Weighted Residuals:
## Min 1Q Median 3Q Max
## -8.8801 -3.0583 0.1019 2.7379 12.0636
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.1094 2.7914 4.696 8.63e-06 ***
## x 1.9094 0.0598 31.929 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.287 on 98 degrees offreedom
## Multiple R-squared: 0.9123, Adjusted R-squared: 0.9114
## F-statistic: 1019 on 1 and 98 DF, p-value:< 2.2e-16
bptest(model2_new)
##
## studentizedBreusch-Pagan test
##
## data: model2_new
## BP = 17.713, df = 1, p-value = 2.569e-05
model3_new <- lm(y3~x, weights = 1/x)
summary(model3_new)
##
## Call:
## lm(formula = y3 ~ x, weights = 1/x)
##
## Weighted Residuals:
## Min 1Q Median 3Q Max
## -57.00 -14.29 1.27 13.17 54.10
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 11.8506 13.2823 0.892 0.374
## x 2.2725 0.4257 5.338 6.07e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 23.79 on 98 degrees offreedom
## Multiple R-squared: 0.2253, Adjusted R-squared: 0.2174
## F-statistic: 28.5 on 1 and 98 DF, p-value:6.068e-07
bptest(model3_new)
##
## studentizedBreusch-Pagan test
##
## data: model3_new
## BP = 22.914, df = 1, p-value = 1.694e-06
y2BCMod<-BoxCoxTrans(y2)
print(y2BCMod)
## Box-CoxTransformation
##
## 100 data points used to estimate Lambda
##
## Input data summary:
## Min. 1stQu. Median Mean 3rd Qu. Max.
## 12.24 64.51 105.43 109.66 156.76 269.81
##
## Largest/Smallest: 22
## Sample Skewness: 0.25
##
## Estimated Lambda: 0.7
y2_new=predict(y2BCMod, y2)
Mod2_bc <- lm(y2_new ~ x)
bptest(Mod2_bc)
##
## studentizedBreusch-Pagan test
##
## data: Mod2_bc
## BP = 8.393, df = 1, p-value = 0.003767
y3BCMod<-BoxCoxTrans(y3+450)
print(y3BCMod)
## Box-CoxTransformation
##
## 100 data points used to estimate Lambda
##
## Input data summary:
## Min. 1stQu. Median Mean 3rd Qu. Max.
## 120.3 452.1 538.1 576.6 710.6 1169.6
##
## Largest/Smallest: 9.72
## Sample Skewness: 0.626
##
## Estimated Lambda: 0.5
y3_new=predict(y3BCMod, y3)
Mod3_bc <- lm(y3_new ~ x)
bptest(Mod3_bc)
##
## studentizedBreusch-Pagan test
##
## data: Mod3_bc
## BP = 5.5629, df = 1, p-value = 0.01834