# Estimating Regression Equation
# Income-Education relationship
#
yed<- read.table("http://web.pdx.edu/~crkl/ec575/data/YED20.TXT",header=T,nrows=20)
# file name may be case sensitive!
# yed<- read.table("C:/course17/ec575/data/yed20.txt",header=T,nrows=20)
summary(yed)

library(maxLik)  # need to install maxLik library
# vector-valued normal likelihood: mu=b[1]+b[2]*Education, sigma=b[3]
llf<-function(b) dnorm(yed$Income,b[1]+b[2]*yed$Education,b[3],log=T)
M1<-maxLik(llf,start=c(0,2,10),method="BHHH")
# Note: BHHH method requires vector-valued log-likelihood, if analytical gradient is not provided
summary(M1)

# Nonlinear least squares
M2<-nls(Income~(b1+b2*Education),data=yed,start=list(b1=0,b2=2))
summary(M2)
# compare with OLS
summary(lm(Income~Education,data=yed))
