Manual Maximum-Likelihood Estimation of an AR-Model in R

后端 未结 1 1650
星月不相逢
星月不相逢 2021-02-06 19:50

I am trying to estimate a simple AR(1) model in R of the form y[t] = alpha + beta * y[t-1] + u[t] with u[t] being normally distributed with mean zero and standard deviat

相关标签:
1条回答
  • 2021-02-06 20:05

    This works for me -- basically what you've done but leaving out the first element of the response, since we can't predict it with an AR model anyway.

    Simulate:

    library(stats)
    set.seed(101)
    data <- arima.sim(n=1000,list(ar=0.1),mean=10)
    

    Negative log-likelihood:

    logl <- function(sigma,alpha,beta) {
       -sum(dnorm(data[-1],alpha+beta*data[1:length(data)-1],sigma,log=TRUE))
    }
    

    Fit:

    library(stats4)
    mle(logl,start=list(sigma=1,alpha=5,beta=0.05),method="L-BFGS-B")
    ## Call:
    ## mle(minuslogl = logl, start = list(sigma = 1, alpha = 5, beta = 0.05), 
    ##     method = "L-BFGS-B")
    ## 
    ## Coefficients:
    ##  0.96150573 10.02658632  0.09437847 
    

    Alternatively:

    df <- data.frame(y=data[-1],ylag1=head(data,-1))
    library(bbmle)
    mle2(y~dnorm(alpha+beta*ylag1,sigma),
         start=list(sigma=1,alpha=5,beta=0.05),
         data=df,method="L-BFGS-B")
    
    0 讨论(0)
提交回复
热议问题