?>

data(gpa3, package=’wooldridge’)

# load packages (which need to be installed!)
library(lmtest); library(car)

head(gpa3)
str(gpa3)

# Estimate model (only for spring data)
reg <- lm(cumgpa~sat+hsperc+tothrs+female+black+white,
data=gpa3, subset=(spring==1))
# Usual SE:
coeftest(reg)

plot(reg,1)

# Refined White heteroscedasticity-robust SE:
coeftest(reg, vcov=hccm)
coeftest(reg, vcov=hccm(reg,type=’hc0′))

######################## not big problem

#

data(hprice1, package=’wooldridge’)
head(hprice1)

# Estimate model
reg1 <- lm(price~lotsize+sqrft+bdrms, data=hprice1)
summary(reg1)
reg1

# Automatic BP test: Bresch-Pagan
library(lmtest)
bptest(reg1)

# Manual regression of squared residuals
summary(lm( resid(reg1)^2 ~ lotsize+sqrft+bdrms, data=hprice1))

 

data(hprice1, package=’wooldridge’)

# Estimate model
reg2 <- lm(log(price)~log(lotsize)+log(sqrft)+bdrms, data=hprice1)
reg2
summary(reg2)

# BP test
library(lmtest)
bptest(reg2)
summary(lm( resid(reg2)^2 ~ log(lotsize)+log(sqrft)+bdrms, data=hprice1))

 

# White test
bptest(reg2, ~ fitted(reg2) + I(fitted(reg2)^2) )
summary(lm( resid(reg2)^2 ~ fitted(reg2) + I(fitted(reg2)^2), data=hprice1))

 

#####################################################
library(foreign)
d401k<-read.dta(“http://fmwww.bc.edu/ec-p/data/wooldridge/401ksubs.dta”)
str(d401k)
head(d401k,20)

# OLS (only for singles: fsize==1)
ols<-lm(nettfa ~ inc + I((age-25)^2) + male + e401k,
data=d401k, subset=(fsize==1))

# WLS
wls <-lm(nettfa ~ inc + I((age-25)^2) + male + e401k, weight=1/inc,
data=d401k, subset=(fsize==1))

summary(ols)
summary(wls)
bptest(ols)
bptest(wls)

 

###########################################################

data(smoke, package=’wooldridge’)

# OLS
olsreg<-lm(cigs~log(income)+log(cigpric)+educ+age+I(age^2)+restaurn,
data=smoke)
olsreg

# BP test
library(lmtest)
bptest(olsreg)

# FGLS: estimation of the variance function
logu2 <- log(resid(olsreg)^2)
varreg<-lm(logu2~log(income)+log(cigpric)+educ+age+I(age^2)+restaurn, data=smoke)
summary(varreg)

# FGLS: WLS
w <- 1/exp(fitted(varreg))
wlsreg<-lm(cigs~log(income)+log(cigpric)+educ+age+I(age^2)+restaurn,
weight=w ,data=smoke)

summary(wlsreg)
bptest(wlsreg)

 

 

 

221124 計量經濟學_2
?>