Note that there are some explanatory texts on larger screens.

plurals
  1. POEstimating Parameters in R from a Weighted Sum of Lagged Variables
    primarykey
    data
    text
    <p>I am afraid I am stuck with an estimation problem.</p> <p>I have two variables, X and Y. Y is explained by the weighted sum of n lagged values of X. My aim is to estimate the two parameters c(alpha0,alpha1) in:</p> <p>Yt = Sum from j=1 to n of ( ( alpha0 + alpha1 * j ) * Xt-j )</p> <p><img src="https://i.stack.imgur.com/o1ypY.gif" alt="enter image description here"></p> <p>Where Xt-j denotes the jth lag of X. </p> <p>I came up with this approach because I thought it would be a good idea to estimate the slope of the weights rather than estimating one parameter for each lag of X added (I intent to set n very large).</p> <p>To the model noise ut is added, which is assumed to be <b>normally distributed</b> with mean zero and standard deviation sigma. </p> <p>Assuming I would like to set <b>n=510</b>, then I need the original series and 510 lagged series. In order to avoid any NAs in the series I transform the original data to "data_chopped", containing only the observations after the first 510 observations have been dropped, and the matrix "data_lagged" in which each column represents a lagged series:</p> <pre><code>library(stats) data&lt;-arima.sim(n=10000,list(ar=0.15,ma=0.1),mean=0.5) data_chopped&lt;-data[511:length(data)] data_lagged&lt;-matrix(nrow=length(data_chopped),ncol=510) for (i in 1:510){ data_lagged[,i]&lt;-head(data,-i)[(511-i):length(head(data,-i))] } #Check result: cbind(data_chopped,data_lagged[,1:3]) #data_lagged[,1] is the first lag of the original data, data_lagged[,2] is the second lag, and so on. No NAs whatsoever to deal with </code></pre> <p>To demonstrate the "working order" of my log-likelihood function and the generated series I first would like to fit an AR(3) model:</p> <pre><code>logl&lt;-function(sigma,alpha,beta,gamma){ -sum(log((1/(sqrt(2*pi)*sigma)) * exp(-(( data_chopped -alpha*data_lagged[,1] -beta*data_lagged[,2] -gamma*data_lagged[,3] )^2)/(2*sigma^2)))) } library(stats4) mle(logl,start=list(sigma=1,alpha=0,beta=0,gamma=0),method="L-BFGS-B") </code></pre> <p>When I now try to estimate my model in the same way it just does not work. I never got a loop within a log-likelihood function to work, this is why I just wrote out the above model. So,</p> <p>Yt = Sum from j=1 to n of ( ( alpha0 + alpha1 * j ) * Xt-j ) </p> <p>= (alpha+beta*1)*Xt-1 + (alpha+beta*2)*Xt-2 + (alpha+beta*3)*Xt-3 + ... + (alpha+beta*510)*Xt-510</p> <pre><code>logl&lt;-function(sigma,alpha,beta){ -sum(log((1/(sqrt(2*pi)*sigma)) * exp(-(( data_chopped -(alpha + beta*1)*data_lagged[,1] -(alpha + beta*2)*data_lagged[,2] -(alpha + beta*3)*data_lagged[,3] -(alpha + beta*4)*data_lagged[,4] -(alpha + beta*5)*data_lagged[,5] ... -(alpha + beta*510)*data_lagged[,510] )^2)/(2*sigma^2)))) } library(stats4) mle(logl,start=list(sigma=1,alpha=0.5,beta=0),method="L-BFGS-B") Error in optim(start, f, method = method, hessian = TRUE, ...) : L-BFGS-B needs finite values of 'fn' </code></pre> <p>I do not get the error if I try just a very few lines:</p> <pre><code>logl&lt;-function(sigma,alpha,beta){ -sum(log((1/(sqrt(2*pi)*sigma)) * exp(-(( data_chopped -(alpha + beta*1)*data_lagged[,1] -(alpha + beta*2)*data_lagged[,2] -(alpha + beta*3)*data_lagged[,3] -(alpha + beta*4)*data_lagged[,4] -(alpha + beta*5)*data_lagged[,5] )^2)/(2*sigma^2)))) } library(stats4) mle(logl,start=list(sigma=1,alpha=0.5,beta=0),method="L-BFGS-B") Call: mle(minuslogl = logl, start = list(sigma = 1, alpha = 0.5, beta = 0), method = "L-BFGS-B") Coefficients: sigma alpha beta 1.07797708 0.26178848 -0.04378526 </code></pre> <p>Can someone please help me out on this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload