Announcement

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

  • mata

    Hi pals,
    I have a big dataset with 1000rows, 9colums, part of which is the following dataset, I would like to find out G based on the relationship between G and wcdf: G=10*wcdf^9-9*wcdf^10. The code I wrote is as follows, but it does not work. any help would be appreciated.
    G wcdf
    .428
    .929
    .661
    .211
    .863
    .401
    .436
    .757
    .078
    clear mata
    gen G=.
    mata:
    v=J(1,1,.)
    st_view(v,.,"G wcdf")
    fuction myfuc(G,a){
    return(a-10*(G^9)+ (9*(G^10)))
    }

    for (i=1; i<=1000;i++){
    r=mm_root(G=.,&myfun(),0,1)
    }
    end

  • #2
    Originally posted by Li Zhang View Post
    I would like to find out G based on the relationship between G and wcdf: G=10*wcdf^9-9*wcdf^10.
    The following does what you're asking.

    .ÿ
    .ÿversionÿ16.1

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿinputÿdoubleÿwcdf

    ÿÿÿÿÿÿÿÿÿÿÿwcdf
    ÿÿ1.ÿ.428
    ÿÿ2.ÿ.929
    ÿÿ3.ÿ.661
    ÿÿ4.ÿ.211
    ÿÿ5.ÿ.863
    ÿÿ6.ÿ.401
    ÿÿ7.ÿ.436
    ÿÿ8.ÿ.757
    ÿÿ9.ÿ.078
    ÿ10.ÿend

    .ÿ
    .ÿlocalÿline_sizeÿ`c(linesize)'

    .ÿsetÿlinesizeÿ80

    .ÿ
    .ÿmata:
    -------------------------------------------------ÿmataÿ(typeÿendÿtoÿexit)ÿ------
    :ÿmataÿsetÿmatastrictÿon

    :ÿ
    :ÿvoidÿfunctionÿcreateG()ÿ{
    >ÿ
    >ÿÿÿÿÿÿÿÿÿ//ÿAddÿGÿtoÿtheÿStataÿdataset
    >ÿÿÿÿÿÿÿÿÿ(void)ÿst_addvar("double",ÿ"G")
    >ÿ
    >ÿÿÿÿÿÿÿÿÿ//ÿ"Iÿhaveÿaÿbigÿdatasetÿwithÿ1000rows,ÿ9colums",ÿsoÿuseÿaÿview
    >ÿÿÿÿÿÿÿÿÿrealÿmatrixÿView
    >ÿÿÿÿÿÿÿÿÿpragmaÿunsetÿView
    >ÿÿÿÿÿÿÿÿÿst_view(View,ÿ.,ÿ("G",ÿ"wcdf"))
    >ÿ
    >ÿÿÿÿÿÿÿÿÿ//ÿDoÿtheÿarithmetic:ÿ"G=10*wcdf^9-9*wcdf^10"
    >ÿÿÿÿÿÿÿÿÿView[.,ÿ1]ÿ=ÿ10ÿ:*ÿView[.,ÿ2]:^9ÿ:-ÿ9ÿ:*ÿView[.,ÿ2]:^10
    >ÿÿÿÿÿÿÿÿÿ
    >ÿÿÿÿÿÿÿÿÿstata("listÿGÿwcdf,ÿnoobsÿseparator(0)")
    >ÿ}

    :ÿ
    :ÿend
    --------------------------------------------------------------------------------

    .ÿ
    .ÿmata:ÿcreateG()

    ÿÿ+------------------+
    ÿÿ|ÿÿÿÿÿÿÿÿÿGÿÿÿwcdfÿ|
    ÿÿ|------------------|
    ÿÿ|ÿ.00296297ÿÿÿ.428ÿ|
    ÿÿ|ÿ.84473479ÿÿÿ.929ÿ|
    ÿÿ|ÿ.09758327ÿÿÿ.661ÿ|
    ÿÿ|ÿ6.716e-06ÿÿÿ.211ÿ|
    ÿÿ|ÿ.59290605ÿÿÿ.863ÿ|
    ÿÿ|ÿ.00171344ÿÿÿ.401ÿ|
    ÿÿ|ÿ.00345936ÿÿÿ.436ÿ|
    ÿÿ|ÿ.26016267ÿÿÿ.757ÿ|
    ÿÿ|ÿ9.937e-10ÿÿÿ.078ÿ|
    ÿÿ+------------------+

    .ÿ
    .ÿsetÿlinesizeÿ`line_size'

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .


    Code:
    version 16.1
    
    clear *
    
    input double wcdf
    .428
    .929
    .661
    .211
    .863
    .401
    .436
    .757
    .078
    end
    
    local line_size `c(linesize)'
    set linesize 80
    
    mata:
    mata set matastrict on
    
    void function createG() {
    
        // Add G to the Stata dataset
        (void) st_addvar("double", "G")
    
        // "I have a big dataset with 1000rows, 9colums", so use a view
        real matrix View
        pragma unset View
        st_view(View, ., ("G", "wcdf"))
    
        // Do the arithmetic: "G=10*wcdf^9-9*wcdf^10"
        View[., 1] = 10 :* View[., 2]:^9 :- 9 :* View[., 2]:^10
        
        stata("list G wcdf, noobs separator(0)")
    }
    
    end
    
    mata: createG()
    
    set linesize `line_size'
    
    exit

    Comment


    • #3
      Hi Joseph, thanks for your time. For the first step, I need to post all 1000 numbers one by one? Is there any other way? This is just a simulation that i am working on, in my real data, there will be more than 2000 rows. Thanks

      Comment


      • #4
        Also, when I code input double wcdf, it reminds me that wcdf already defined. Any advice? Thanks

        Comment


        • #5
          Hi Joseph, it's my bad. You solved my problem. You have been a great help. thanks so much!

          Comment


          • #6
            Hi Joseph,

            I just realized that you did G=10*wcdf^9-9*wcdf^10, rather than wcdf=10*G^9-9*G^10. I switched G and wcdf in your code, but it gave me empty. Any suggest?Thanks

            Comment

            Working...
            X