Here is a maximum log likelihood estimation of linear model.
set.seed(2013)
# random generate simulation data with GPD distribution with 3 parameters
alpha<- 1; beta<- 2;
n<- 100;
err<- rnorm(n, sd=0.5)
x <- rnorm(n)
# model y defined as
y<- alpha+beta*x + err
data<- data.frame(x, y)
# define -2*loglikelihood function
LL<-function(params){
alpha<- params[1]; beta<- params[2]; sigma<- params[3]
linkfun<- alpha+beta*data$x
error<- data$y-linkfun
;
f<- (1/(sigma*sqrt(2*pi))) * exp(-0.5*(error/sigma)**2)
ll<- sum(log(f))
return(-2*ll)
}
#optimized with initial condistion
optim(c(0,0,5),LL)
|