Announcement

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

  • Accessing temporary file produced by -crtrees- in mata

    Hi,

    I am trying to access a temporary file (matatrees) produced by the -crtrees- command in stata.

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	30.7 KB
ID:	1528701

    My goal is to open this temporary file in mata to then convert it into a stata matrix and finally a dta file. I am new to mata programming and couldn't seem to find any help in the reference manual.

    Sorry if this is a basic question.

    Indra

  • #2
    The crtrees command is a user-written Stata package available from SSC implementing Classification and Regression Tree methodology.

    Now I will admit to not having used CART methodology, although I've wanted to. So as a learning experience I installed the package and ran an example, and then created the demonstration code shown below. I note that each of the three matrices created by crtrees has the same number of rows, so I placed the matrices side-by-side into a single Stata dataset with the columns of the tree matrix turned into variables named according to the matrices from which they came. Better names could be chosen if I better understood what the matrices represent.
    Code:
    cls
    sysuse auto, clear 
    // crtrees will not replace an existing file
    capture erase mytrees.mmat
    // the savetrees option only works with rforests()
    // this example taken from the help crtrees output 
    //   with the number of bootstraps much smaller for demonstration
    crtrees price trunk weight length foreign gear_ratio, ///
       rforests generate(p_hat) bootstraps(10) savetrees(mytrees.mmat)
    
    mata:
    file = fopen("mytrees.mmat", "r")
    trees = fgetmatrix(file)
    criteria = fgetmatrix(file)
    coefficients = fgetmatrix(file)
    fclose(file)
    mata describe
    end
    
    clear
    getmata (trees_*) = trees
    getmata (criteria_*) = criteria
    getmata (coefficients) = coefficients
    describe
    Code:
    . mata:
    ------------------------------------------------- mata (type end to exit) ----------------------
    : file = fopen("mytrees.mmat", "r")
    
    : trees = fgetmatrix(file)
    
    : criteria = fgetmatrix(file)
    
    : coefficients = fgetmatrix(file)
    
    : fclose(file)
    
    : mata describe
    
          # bytes   type                        name and extent
    -------------------------------------------------------------------------------
            2,528   real colvector              coefficients[316]
           25,280   real matrix                 criteria[316,10]
                8   real scalar                 file
           10,112   real matrix                 trees[316,4]
    -------------------------------------------------------------------------------
    
    : end
    ------------------------------------------------------------------------------------------------
    
    . 
    . clear
    
    . getmata (trees_*) = trees
    
    . getmata (criteria_*) = criteria
    
    . getmata (coefficients) = coefficients
    
    . describe
    
    Contains data
      obs:           316                          
     vars:            15                          
    ------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ------------------------------------------------------------------------------------------------
    trees_1         byte    %10.0g                
    trees_2         byte    %10.0g                
    trees_3         byte    %10.0g                
    trees_4         float   %10.0g                
    criteria_1      byte    %10.0g                
    criteria_2      byte    %10.0g                
    criteria_3      int     %10.0g                
    criteria_4      int     %10.0g                
    criteria_5      int     %10.0g                
    criteria_6      int     %10.0g                
    criteria_7      byte    %10.0g                
    criteria_8      byte    %10.0g                
    criteria_9      float   %10.0g                
    criteria_10     float   %10.0g                
    coefficients    float   %10.0g                
    ------------------------------------------------------------------------------------------------

    Comment


    • #3
      This is so helpful, thank you so much William!

      Comment

      Working...
      X