Note that there are some explanatory texts on larger screens.

plurals
  1. POThreshold Quantile Regression code CRASHES in R
    text
    copied!<p>i am working on a two stage threshold quantile regression model in R, and my aim is to estimate the threshold of the reduced-form equation (call it rhohat), and the threshold of the structural equation (call it qhat), in two stages. On the first stage, i estimate rhohat by quantile regression and obtain the fitted values. I use these fitted values to estimate qhat on the second stage. The code is as follows (thanks to Prof. Bruce Hansen, whose code i modified): </p> <pre><code>#************************************************************# #Quantile Regression. qr.regress &lt;- function(y,x){ beta &lt;- c(rq(y~x,tau)$coefficients[1],rq(y~x,tau)$coefficients[2]) beta } #Threshold Estimation with one independent variable + constant. joint_thresh &lt;- function(y,x,q){ n=nrow(y) k=ncol(x) e=y-x%*%rq(y~x,tau)$coefficients[2]-rq(y~x,tau)$coefficients[1] s0 &lt;- det(t(e)%*%e) n1 &lt;- round(.05*n)+k n2 &lt;- round(.95*n)-k qs &lt;- sort(q) qs &lt;- qs[n1:n2] qs &lt;- as.matrix(unique(qs)) qn &lt;- nrow(qs) sn &lt;- matrix(0,qn,1) for (r in 1:qn){ d &lt;- (q&lt;=qs[r]) xx &lt;- (x)*(d%*%matrix(1,1,k)) xx &lt;- xx-x%*%rq(xx~x,tau)$coefficients[2]-rq(xx~x,tau)$coefficients[1] ex &lt;- e-xx%*%rq(e~xx,tau)$coefficients[2]-rq(e~xx,tau)$coefficients[1] exw &lt;- ex*(tau-(ex&lt;0)) sn[r] &lt;- sum(exw) } r &lt;- which.min(sn) smin &lt;- sn[r] qhat &lt;- qs[r] d &lt;- (q&lt;=qhat) x1 &lt;- x*(d%*%matrix(1,1,k)) x2 &lt;- x*((1-d)%*%matrix(1,1,k)) beta1 &lt;- rq(y~x1,tau)$coefficients[2] beta2 &lt;- rq(y~x2,tau)$coefficients[2] yhat &lt;- x1%*%beta1+x2%*%beta2 list(yhat=yhat,qhat=qhat) } #Threshold Estimation with two independent variables + constant. joint_thresh_2 &lt;- function(y,x,q){ n &lt;- nrow(y) k &lt;- ncol(x) e=y-x[,1]%*%t(rq(y~x-1,tau)$coefficients[3])-x[,2]%*%t(rq(y~x-1,tau)$coefficients[2])-x[,3]%*%t(rq(y~x-1,tau)$coefficients[3]) s0 &lt;- det(t(e)%*%e) n1 &lt;- round(.05*n)+k n2 &lt;- round(.95*n)-k qs &lt;- sort(q) qs &lt;- qs[n1:n2] qs &lt;- as.matrix(unique(qs)) qn &lt;- nrow(qs) sn &lt;- matrix(0,qn,1) for (r in 1:qn){ d &lt;- (q&lt;=qs[r]) xx &lt;- (x)*(d%*%matrix(1,1,k)) xx &lt;- xx[,1]-x%*%rq(x[,1]~xx-1,tau)$coefficients ex &lt;- e-xx%*%qr.regress(e,xx)[2]-xx%*%qr.regress(e,xx)[1] exw &lt;- ex*(tau-(ex&lt;0)) sn[r] &lt;- sum(exw) } r &lt;- which.min(sn) smin &lt;- sn[r] qhat &lt;- qs[r] d &lt;- (q&lt;=qhat) x1 &lt;- x*(d%*%matrix(1,1,k)) x2 &lt;- x*((1-d)%*%matrix(1,1,k)) beta1 &lt;- rq(y~x1-1,tau)$coefficients[1] beta2 &lt;- rq(y~x2-1,tau)$coefficients[3] c1 &lt;- rq(y~x1-1,tau)$coefficients[2] c2 &lt;- rq(y~x2-1,tau)$coefficients[2] yhat &lt;- x1[,1]%*%t(beta1)+x2[,3]%*%t(beta2)+c1+c2 list(yhat=yhat,qhat=qhat) } #Threshold Reduced-form eqn. tau=0.50 stqr_thresh_loop &lt;- function(n,reps){ qhat=as.vector(reps) rhohat=as.vector(reps) kx &lt;- 1 sig &lt;- matrix(c(1,0.5,0.5,1),2,2) x&lt;- matrix(rnorm(n*kx),n,kx) q &lt;- matrix(rnorm(n),n,1) z2 &lt;- cbind(matrix(1,n,1),q) for(i in 1:reps){ e &lt;- matrix(rnorm(n*2,quantile(rnorm(n),tau),1),n,2)%*%chol(sig) z1=0.5+0.5*x*(q&lt;=0)+1*x*(q&gt;0)+e[,2] y=0.5+1*z1*(q&lt;=1)+1.5*z1*(q&gt;1)+1*z2[,2]+e[,1] out1 &lt;- joint_thresh(y=z1,x=x,q=q) z1hat&lt;- out1$yhat rhohat[i] &lt;- out1$qhat zhat &lt;- cbind(z1hat,z2) out2 &lt;- joint_thresh_2(y=y,x=zhat,q=q) qhat[i] &lt;- out2$qhat } #Close for loop. list(rhohat=rhohat,qhat=qhat) } #************************************************************# </code></pre> <p>You can easily run it yourselves. The problem is that when i write,</p> <p>stqr_thresh_loop(n=200,reps=500)</p> <p>the code crashes and never gives me any result. What am i doing wrong? Thank you very much!!</p>
 

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