data(bwght, package='wooldridge')# Basic model:lm( bwght ~ cigs+faminc, data=bwght)# Weight in pounds, manual way:bwght$bwghtlbs <- bwght$bwght/16lm( bwghtlbs ~ cigs+faminc, data=bwght)# Weight in pounds, direct way:lm( I(bwght/16) ~ cigs+faminc, data=bwght)# Packs of cigarettes:lm( bwght ~I(cigs/20) +faminc, data=bwght)
Effects-Automatic.R
# Repeating the regression from Example 6.2:data(hprice2, package='wooldridge')res <-lm( log(price) ~log(nox)+log(dist)+rooms+I(rooms^2)+stratio,data=hprice2)# Automatic effects plot using the package "effects"library(effects)plot( effect("rooms",res) )
Effects-Manual.R
# Repeating the regression from Example 6.2:data(hprice2, package='wooldridge')res <-lm( log(price) ~log(nox)+log(dist)+rooms+I(rooms^2)+stratio,data=hprice2)# Predictions: Values of the regressors:# rooms = 4-8, all others at the sample mean:X <-data.frame(rooms=seq(4,8),nox=5.5498,dist=3.7958,stratio=18.4593)# Calculate predictions and confidence interval:pred <-predict(res, X, interval ="confidence")# Table of regressor values, predictions and CI:cbind(X,pred)# Plot matplot(X$rooms, pred, type="l", lty=c(1,2,2))
Example-6-1.R
data(hprice2, package='wooldridge')# Estimate model with standardized variables:lm(scale(price) ~0+scale(nox)+scale(crime)+scale(rooms)+scale(dist)+scale(stratio), data=hprice2)
Example-6-2-Anova.R
library(car)data(hprice2, package='wooldridge')res <-lm(log(price)~log(nox)+log(dist)+poly(rooms,2,raw=TRUE)+ stratio,data=hprice2)# Manual F test for rooms:linearHypothesis(res, matchCoefs(res,"rooms"))# ANOVA (type 2) table:Anova(res)
Example-6-2.R
data(hprice2, package='wooldridge')res <-lm(log(price)~log(nox)+log(dist)+rooms+I(rooms^2)+ stratio,data=hprice2)summary(res)# Using poly(...):res <-lm(log(price)~log(nox)+log(dist)+poly(rooms,2,raw=TRUE)+ stratio,data=hprice2)summary(res)
Example-6-3.R
data(attend, package='wooldridge')# Estimate model with interaction effect:(myres<-lm(stndfnl~atndrte*priGPA+ACT+I(priGPA^2)+I(ACT^2), data=attend))# Estimate for partial effect at priGPA=2.59:b <-coef(myres)b["atndrte"] +2.59*b["atndrte:priGPA"] # Test partial effect for priGPA=2.59:library(car)linearHypothesis(myres,c("atndrte+2.59*atndrte:priGPA"))
Example-6-5.R
data(gpa2, package='wooldridge')# Regress and report coefficientsreg <-lm(colgpa~sat+hsperc+hsize+I(hsize^2),data=gpa2)reg# Generate data set containing the regressor values for predictionscvalues <-data.frame(sat=1200, hsperc=30, hsize=5)# Point estimate of predictionpredict(reg, cvalues)# Point estimate and 95% confidence intervalpredict(reg, cvalues, interval ="confidence")# Define three sets of regressor variablescvalues <-data.frame(sat=c(1200,900,1400), hsperc=c(30,20,5), hsize=c(5,3,1))cvalues# Point estimates and 99% confidence intervals for thesepredict(reg, cvalues, interval ="confidence", level=0.99)
Example-6-6.R
data(gpa2, package='wooldridge')# Regress (as before)reg <-lm(colgpa~sat+hsperc+hsize+I(hsize^2),data=gpa2)# Define three sets of regressor variables (as before)cvalues <-data.frame(sat=c(1200,900,1400), hsperc=c(30,20,5), hsize=c(5,3,1))# Point estimates and 95% prediction intervals for thesepredict(reg, cvalues, interval ="prediction")
Formula-Logarithm.R
data(hprice2, package='wooldridge')# Estimate model with logs:lm(log(price)~log(nox)+rooms, data=hprice2)
Confidence-Bands.py
import wooldridge as wooimport numpy as npimport statsmodels.formula.api as smfimport pandas as pdimport matplotlib.pyplot as pltgpa2 = woo.dataWoo('gpa2')# regress and report coefficients:reg = smf.ols(formula='colgpa ~ sat', data=gpa2)results = reg.fit()print(f'beta_hat: \n{results.params}\n')# regressor (SAT) values for prediction from 400 to 1600 in steps of 100:SAT = pd.DataFrame({'sat': np.arange(400, 1600+1, 100)})# predictions and 95% confidence intervals:colgpa_pred = results.get_prediction(SAT)colgpa_pred_CI = colgpa_pred.summary_frame(alpha=0.05)[['mean', 'mean_ci_lower', 'mean_ci_upper']]print(f'colgpa_pred_CI: \n{colgpa_pred_CI}\n')# plot:plt.plot(SAT, colgpa_pred_CI['mean'], color='black', linestyle='-', label='')plt.plot(SAT, colgpa_pred_CI['mean_ci_upper'], color='green', linestyle='--', label='upper CI')plt.plot(SAT, colgpa_pred_CI['mean_ci_lower'], color='red', linestyle='--', label='lower CI')plt.ylabel('colgpa')plt.xlabel('sat')plt.legend()plt.savefig('PyGraphs/Confidence-Bands.pdf')# quadratic model as an alternative:reg2 = smf.ols(formula='colgpa ~ sat + I(sat**2)', data=gpa2)results2 = reg2.fit()colgpa_pred2 = results2.get_prediction(SAT)colgpa_pred2_CI = colgpa_pred2.summary_frame(alpha=0.05)[['mean', 'mean_ci_lower', 'mean_ci_upper']]# plot:plt.plot(SAT, colgpa_pred2_CI['mean'], color='black', linestyle='-', label='')plt.plot(SAT, colgpa_pred2_CI['mean_ci_upper'], color='green', linestyle='--', label='upper CI')plt.plot(SAT, colgpa_pred2_CI['mean_ci_lower'], color='red', linestyle='--', label='lower CI')plt.ylabel('colgpa')plt.xlabel('sat')plt.legend()plt.savefig('PyGraphs/Confidence-Bands2.pdf')