Announcement

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

  • Code to generate estimated multivariate time series model with the coefficients

    Good day all,

    Please, I need the Stata command to write out my model with the estimated coefficients. For instance, I have done the following regression with 10 explanatory variables in Stata:

    newey LIS prevLIS imports equities infotechperm sqrcostofadmin investmenteducation sqrgdppercapita VASmanu corruptionindexr VASagric, lag(0)

    I followed this code to generate the equation:
    sysuse auto
    regress mpg weight
    matrix m = e(b)
    local a = m[1,2]
    local b = m[1,1]
    file open dofile using "dofile.do", read write
    file write dofile "gen y = `a' + `b'*weight"
    file close dofile

    The file "dofile.do", which you can apply to the external data, contains this line:
    Code:

    gen y = 39.44028353071797 + -.0060086868119993*weight

    Here is what I came up with:

    matrix m = e(b)
    local a = m[1,11]
    local b = m[1,10]
    file open dofile using "dofile.do", read write
    file write dofile"gen LIS = ‘a’ + _’b’*prevLIS +_’b’*imports + _’b’*equities + _’b’*infotechperm + _’b’*sqrcostofadmin + _’b’*investmenteducation + _’b’*sqrgdppercapita + _’b’* VASmanu + _’b’*corruptionindexr + _’b’*VASagric, lag(0)"
    file close dofile

    gen LIS = 5.53409598195157 + _'b'*prevLIS +_'b'*imports + _'b'*equities + _'b'*infotechperm + _'b'*sqrcostofadmin + _'b'*investmenteducation + _'b'*sqrgdppercapita + _'b'* VASmanu + _'b'*corruptionindexr + _'b'*VASagric, lag(0)0)

    The output in my dofile only shows the constant as shown above.

    What should I do to generate the equation with all the coefficients?

    Thank you.

    Florence Ijagbone



  • #2
    I won't try to rewrite your code in total. I will just give a few hints.

    If you're referrring to local macros, the quotation marks are different. You need

    Code:
    `b'
    not the same quotation mark on both left and right. You got that right for

    Code:
    `a'
    However, the coefficient for each predictor is different. It makes no sense to refer to the same local macro for each. With your approach you need to map

    m[1,1] to m[1,10]

    to separate local macros, say

    b1 to b10

    Comment


    • #3
      Thanks Nick.

      I have applied the code below, following the hints:

      matrix m = e(b)
      local a = m[1,11]
      local b1 to b10 = m[1,1] to m[1,10]
      file open dofile using "dofile.do", read write
      file write dofile"gen LIS = ‘a’ + _‘b1’*prevLIS +_‘b2’*imports + _‘b3’*equities + _‘b4’*infotechperm + _‘b5’*sqrcostofadmin + _‘b6’*investmenteducation + _‘b7’*sqrgdppercapita + _‘b8’* VASmanu + _‘b9’*corruptionindexr + _‘b10’*VASagric, lag(0)"
      file close dofile

      This is the output:

      gen LIS = 5.53409598195157 + _to b10 = m[1,1] to m[1,10]*prevLIS +_*imports + _*equities + _*infotechperm + _*sqrcostofadmin + _*investmenteducation + _*sqrgdppercapita + _* VASmanu + _*corruptionindexr + _*VASagric, lag(0)nteducation + _-1.881959991026205*sqrgdppercapita + _-1.881959991026205* VASmanu + _-1.881959991026205*corruptionindexr + _-1.881959991026205*VASagric, lag(0)

      Please, can I get the right code or command to generate the equation in Stata? I have no programming knowledge. I have only tried to follow an existing command I got from statalist.

      Thank you.

      Comment


      • #4
        Originally posted by Florence Ijagbone View Post
        I followed this code to generate the equation:
        <code redacted for brevity>
        The file "dofile.do", which you can apply to the external data. . .

        What should I do to generate the equation with all the coefficients?
        Your objective appears to be fitting a regression model to one dataset and then using its coefficients to make predictions with another.

        If so, then that isn't the way to go about it.

        Instead, you can use Stata's estimates save to much more easily accomplish what you want.

        I illustrate the technique below.
        Code:
        version 19
        
        clear *
        
        quietly sysuse auto
        preserve
        
        *
        * Begin here
        *
        // Fitting the regression model to domestic automobiles
        quietly keep if !foreign
        regress gear_ratio c.(trunk weight length)
        
        estimates save Model // <= Here
        
        // Now apply the coefficients to generate predictions for the foreign automobiles
        restore
        quietly keep if foreign
        
        estimates use Model.ster // <= Here
        
        predict double xb, xb
        
        exit
        I've never had occasion to use newey and so don't know how well the method works with that regression command, but I'd recommend giving it a whirl.

        Comment


        • #5
          Originally posted by Joseph Coveney View Post
          I've never had occasion to use newey and so don't know how well the method works with that regression command, but I'd recommend giving it a whirl.
          newey estimates linear regression using OLS, with Newey and West standard errors. The coefficient estimates and predictions are therefore identical to those from regress.

          Comment


          • #6
            Originally posted by Andrew Musau View Post
            The coefficient estimates and predictions are therefore identical to those from regress.
            Thank you, Andrew; yeah, I figured that, but wasn't certain that the objects left behind would be amenable to estimates save in the same way and so hedged my bet.

            It seems that I ought to appreciate more Stata's striving for consistency (syntax, returned objects) in its estimation and postestimation commands.

            Comment

            Working...
            X