################################################################# #Question 2 ################################################################# sightings = c(5,0,1,1,0,3,2,3,4,1) ######Part (b) mean(sightings) ################################################################## #Define the Poisson function ################################################################## poisson.fun = function(theta,y){ n = length(y) LL = -theta*n + log(theta)*sum(y) - sum(factorial(y)) return(-LL) } ######Part (c) opt = optim(2.5, poisson.fun, y=sightings, method=c("BFGS")) ################################################################# #Question 3 ################################################################# mroz87 = read.table( "http://www.montana.edu/ebelasco/ecns562/homework/mroz87.dat", header = T) names( mroz87) # Take a subset of the data with wage > 0 posWage = subset( mroz87, wage > 0 ) posWage$logWage = log( posWage$wage ) summary( posWage$logWage ) ###################################################################### # Model logWage as a linear function of educ, exper and exper^2 ###################################################################### # Create a variable for exper^2 and add it to the posWage data frame posWage$exper2 = posWage$exper^2 lm.posWage = lm( logWage ~ educ + exper + exper2, data = posWage) summary( lm.posWage ) ############################################################################### # Use motheduc, fatheduc as instruments, assuming exper and exper^2 are exogenous variables ############################################################################### # First check that educ is correlated with motheduc and fatheduc instr.posWage.1stage = lm( educ ~ exper + exper2 + fatheduc + motheduc, data = posWage ) summary( instr.posWage.1stage ) # Now fit the multiple instruments model educ.hat = instr.posWage.1stage$fitted.values #Second stage regressions instr.posWage = lm( logWage ~ educ.hat + exper + exper2, data = posWage) summary( instr.posWage ) #2SLS using tsls() function instr.posWage2 = tsls( logWage ~ educ + exper + exper2, instruments =~ exper + exper2 + fatheduc + motheduc, data = posWage ) summary( instr.posWage2 ) #Compare parameter results from three methods cbind(lm.posWage$coeff, instr.posWage$coeff, instr.posWage2$coeff)