No announcement yet.
  • Filter
  • Time
  • Show
Clear All
new posts

  • "variable unit1 not found" in the code of dynamic factor model

    Dear Sir,

    I am trying to run the dynamic factor model mentioned in Feferici and Mazzitelli(2014) by using the codes in this paper with the application on my data. However, as I ran the ado file, the error message "variable unit1 not found" were shown in the result window. I attached my data file, ado file and the error message in this mail for you. Would you please help me to fix the error that I met? Thanks for your help.
    Chih Cheng Chen
    Attached Files

  • #2
    Welcome to Statalist.

    In the code on the left, we see the command
        twoway (line unit`i' t), ...
    In the code on the right, we see the command
        twoway (line unit`i'1 t), ...
    Note the digit 1 at the end of the unit variable name in the code on the right. Failure to include that is perhaps the cause of your error message.

    Now, with that possible answer to your question, let me offer some advice for your future posts to Statalist.

    You have accidentally posted your topic in Statalist's Mata Forum, which is used for discussions of Stata's Mata language. Your question would have seen a much larger audience if you had correctly posted it in Statalist's General Forum.

    Also, if you have not already done so, you should review the Statalist FAQ linked to at the top of this page, as well as from the Advice on Posting link on the page you used to create your post, for posting guidelines and suggestions. Posting pictures of anything other than a graph is not generally considered helpful; code and results should be copied as text from the Stata Do-file editor or results window and pasted using CODE delimiter blocks.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Finally, in the English language, "Sir" applies to males. In this forum, "Member" would be more appropriate and inclusive.


    • #3
      Dear William Lisowski,

      Thanks for your reply. Your advise for posting a question in this forum are also appreciated. As I move the "1" away from the code. The error message is still there. Thus, I attached the whole codes for the
      Stata Do-file editor of my problem are as following:

      *import the dataset
      use C:\Users\chihcheng\Desktop\FAVAR\DFM.dta, clear
      *To normalize the variables
      foreach x of varlist x11 x12 x13 x14 x15 x16 x17{ 
          *easily generate the standardized all variables
          egen z`x' = std(`x') 
          *Make the standardized variables list as a Matrix
          mkmat z`x' 
      * define the name of the Matrix of standardized variables
      matrix A = zx11, zx12, zx13, zx14, zx15, zx16, zx17
      *calculate the variance matrix of ST
      matrix ST=J(7,7,0) 
      forvalues i=1(20)320 { 
          matrix C=A[`i'..(`i'+20-1),1...] 
          svmat C 
          matrix accum cov = C1-C7, deviations noconstant 
          matrix mcov=cov/(r(N)-1) 
          matrix ST=ST+mcov 
          drop C1-C7
      *list the values of Matrix ST
      matrix list ST 
      *calculate its eigenvalues and the relative eigenvectors
      matrix symeigen eigenvectors eigenvalues = ST 
      *list the values of eigenvalues
      matrix list eigenvalues
      *In this case there are 5 eigenvalues greater than one. 
      *The explained variability by each of them is given
      matrix D= diag(eigenvalues) 
      *this is a diagonal matrix
      matrix explained_variability = eigenvalues/trace(D) 
      matrix list explained_variability 
      *So the first eigenvector associated to the first eigenvalues embodies more than the 70% of total variability of matrix ST
      *The cumulative proportion of explained variability is given by:
      matrix cumulative_variability = explained_variability 
      forvalues i=2/7 { 
       matrix cumulative_variability[1,`i'] = cumulative_variability[1,`i'] + cumulative_variability[1,`i'-1] 
      matrix list cumulative_variability  
      *The eigenvectors of the matrix ST are the following: 
      matrix list eigenvectors
      *The vector of overall averages
      matrix overall_average= J(1,1,0) 
      foreach i of varlist zx11 zx12 zx13 zx14 zx15 zx16 zx17{ 
          matrix Q=diag( `i') 
          matrix overall_average = overall_average, trace(Q)/rowsof(Q) 
      matrix overall_average = overall_average[1...,2...] 
      matrix list overall_average 
      *Of course the overall average of the variables is equal to zero because of the initial standardization. 
      *The vector of the average of each unit in the sample zi is given by: 
      matrix zi=J(1,7,0) 
      forvalues i = 1/20 { 
          matrix z=J(1,7,0) 
          forvalues k = `i'(20)320 { 
              matrix z =z+A[`k',1...] 
          matrix z=z/14 
          matrix zi= zi\z 
      matrix zi = zi[2...,1...] 
      matrix list zi 
      *The final computing of the scores is given by
      mat ci=J(1,7,0) 
      forvalues i=1/20 { 
          matrix cih=(zi[`i',1...]-overall_average)*eigenvectors 
          matrix ci = ci\cih 
      matrix ci = ci[2...,1...] 
      matrix list ci 
      *The derived scores allow us to represent the static structure of the units: 
      svmat ci 
      twoway (scatter ci2 ci1, mlabel(id)) 
      *The diferential dynamic of the units is expressed by the “trajectories” traced by each unit on the 
      *common factorial space: they are given by the scores at each time t:
      matrix zt = J(1,7,0) 
      forvalues t=1(20)320{ 
          matrix z2=J(1,7,0) 
          local t2=`t'+19 
          forvalues k=`t'/`t2' { 
              matrix z2= z2+A[`k',1...] 
          matrix z2= z2/20 
          matrix zt=zt\z2 
      matrix zt=zt[2...,1...] 
      matrix list zt 
      *The computation of the trajectories of each unit is given by: 
      forvalues i = 1/20 { 
          matrix unit`i' = J(1,7,0) 
          forvalues k = `i'(20)320 { 
              matrix cht = A[`k',1...] 
              matrix unit`i' = unit`i' \ cht 
          matrix unit`i' = unit`i'[2...,1...] 
          matrix unit`i'= (unit`i'-zt)*eigenvectors 
          matrix list unit`i' 
      *Because the power of explanation of the total variability of the first component is satisfactory, the 
      *scores of each unit for each time t on the first axis provide the wanted overall innovation index.
      *a graphical representation of the trajectories a trend variable is needed: 
      matrix t=J(16,1,1) 
      forvalues i=2/16 { 
          matrix t[`i',1]= t[`i',1]+ t[`i'-1,1] 
      svmat t
      *Now is possible to compile a graph for the trajectory of each county: 
      forvalues i = 1/20 { 
          forvalues k = `i'(20)320 { 
              matrix cht = A[`k',1...] 
              matrix unit`i' = unit`i'
          twoway (line `i' t), xtitle(, size(zero) color(ltbluishgray)) legend(off) nodraw 
          graph save `i', replace 
      In addition, I also attached my dataset for this exercise. Thus, would you please kindly help me to check where the problem is. Thanks for your great help.


      Attached Files


      • #4
        You have not understood my post #2.

        In the picture in post #1, the code in the Do-file Editor window (on the right) and in the attached is
        twoway (line unit`i'1 t), ...
        This code is correct.

        In the picture in post #1, the code in the Results window (on the left) that you ran and gave the error message is
        twoway (line unit`i' t), ...
        This code is incorrect because it lacks the digit 1 in what is supposed to be
        In post #3, the code you show is
        twoway (line `i' t), ...
        This code is further incorrect because it still lacks the digit 1 and it now includes an incorrect space. You do not tell us what the error message was for this code, but I expect it was a different error message than the one you showed in post #1.

        Change the code you are running to match that in from post #1.