mata

N = rows(y1)

//y

y = y1\y2\y3

//x

x=blockdiag(x1, blockdiag(x2,x3))

//z

z=blockdiag(z1, blockdiag(z2,z3))

sel11 = J(N,1,1)

sel12 = J(N,1,0)

sel13 = J(N,1,0)

sel1=(sel11\sel12\sel13)

sel2=(sel12\sel11\sel13)

sel3=(sel12\sel13\sel11)

id = st_data(. , ("_ID"))

info = panelsetup(id,1)

nc = rows(info)

b0 = J(1, cols(x), 0)

D = J(cols(z), cols(z), 0 )

function oiv_gmm( M, todo, b, q, S, H) {

real scalar nc

y = moptimize_util_depvar(M,1)

xb = moptimize_util_xb(M,b,1)

z1 = moptimize_util_userinfo(M, 1)

z2 = moptimize_util_userinfo(M, 2)

z3 = moptimize_util_userinfo(M, 3)

z = moptimize_util_userinfo(M, 4)

sel1 = moptimize_util_userinfo(M, 5)

sel2 = moptimize_util_userinfo(M, 6)

sel3 = moptimize_util_userinfo(M, 7)

D = moptimize_util_userinfo(M, 8)

id = moptimize_util_userinfo(M, 9)

e = y - xb

e1 = NULL

st_select(e1,e,sel1)

e2 = NULL

st_select(e2,e,sel2)

e3 = NULL

st_select(e3,e,sel3)

info = panelsetup(id,1)

nc = rows(info)

for(i=1; i<=503; i++) {

z1i = panelsubmatrix(z1,i,info)

z2i = panelsubmatrix(z2,i,info)

z3i = panelsubmatrix(z3,i,info)

e1i = panelsubmatrix(e1,i,info)

e2i = panelsubmatrix(e2,i,info)

e3i = panelsubmatrix(e3,i,info)

zi = blockdiag(z1i, blockdiag(z2i,z3i))

ei = e1i\e2i\e3i

D = D + zi'*ei*ei'*zi

}

Wopt = qrinv(D)

m = z'*e

q = m'*Wopt**m

}

/// initialise moptimize

M = moptimize_init()

moptimize_init_evaluator(M, &oiv_gmm())

moptimize_init_evaluatortype(M, "d0")

moptimize_init_depvar(M, 1, y)

moptimize_init_eq_indepvars(M, 1, x)

moptimize_init_userinfo(M, 1, (z1))

moptimize_init_userinfo(M, 2, (z2))

moptimize_init_userinfo(M, 3, (z3))

moptimize_init_userinfo(M, 4, (z))

moptimize_init_userinfo(M, 5, (sel1))

moptimize_init_userinfo(M, 6, (sel2))

moptimize_init_userinfo(M, 7, (sel3))

moptimize_init_userinfo(M, 8, (D))

moptimize_init_userinfo(M, 9, (id))

moptimize_init_eq_cons(M, 1, "off")

moptimize_init_eq_coefs(M, 1, (b0))

moptimize_init_which(M, "min")

moptimize_init_technique(M, "nr")

moptimize(M)

moptimize_result_display(M)

b_gmm = moptimize_result_coefs(M)

b_gmm

*** get GMM estimes

b_gmm = moptimize_result_coefs(M)'

end

The following error message shows up: nonpointer found where pointer required. So far what I have deduced is that it maybe a problem in the step where I'm calculating the cluster robust GMM weight using panelsubmatrix. What would possibly be the correct way to approach this ?]]>

Effect of various farm practices on crop yield (Kg/Acre) |
||||||||||

Lentil | Lentil | green gram | green gram | peas | peas | dasasd | dasasd | sasa | sasa | |

Mean without | Change associated with | Mean without | Change associated with | Mean without | Change associated with | Mean without | Change associated with | Mean without | Change associated with | |

Intercropping | 342 | 19 (2.035)*** | ||||||||

Subsidy | ||||||||||

Seed treatment | ||||||||||

Fertilizers | ||||||||||

dsdssa | ||||||||||

das | ||||||||||

dasd | ||||||||||

dfsa | ||||||||||

dfas | ||||||||||

sd |

Hi, I want to export a table like above in excel using my regression results. Basically, my regression is something like this: reg yield intercropping if crop == blackgram.

I want to export regression coefficient, its standard error, p value and average value (calculated using summ command for each cell i.e. if a farmer has done intercropping for black gram.

I am unable to write a state matrix code.

It would be really helpful if anyone could help me on this.

I wrote the following:

set trace on

levelsof seed_variety_combined, local(cropname)

foreach crop of local cropname{

matrix ResultsTables = J(22,8,.)

matrix rownames ResultsTables = intercropping subsidypct source_of_seed succ_yrs land_preparation_cat storage_method_cat life_saving_irrigation_cat disease_pest_treatment_cat fertiliser_qty_cat spacing_cat seed_treatment_method_cat sowing_method_final category skymet_final

matrix colnames ResultsTables = bg_b bg_se bg_p len_b len_se len_p

levelsof seed_variety_combined, local(cropname)

local indepvar intercropping subsidypct source_of_seed succ_yrs land_preparation_cat storage_method_cat life_saving_irrigation_cat disease_pest_treatment_cat fertiliser_qty_cat spacing_cat seed_treatment_method_cat sowing_method_final category skymet_final

foreach var of local indepvar {

quietly reg yield i.`var' i.district_code i.year if seed_variety_combined == `crop'

*matrix ResultsTables[rownumb(ResultsTables,"`var'"), 1] = e(N)

matrix ResultsTables[rownumb(ResultsTables,"`var'"), 2] = e(b)

matrix ResultsTables[rownumb(ResultsTables,"`var'"), 3] = e(se)

matrix ResultsTables[rownumb(ResultsTables,"`var'"), 4] = e(pval)

quietly sum yield if seed_variety_combined == `v' & e(sample) == 1 & `var'== 1

matrix ResultsTables[`rows', 2] = r(mean)

}

}

Something is wrong in this code, I'm unable to figure out what it is. Also, I do not understand what exactly does 'rownumb' does.

Any help would be highly appreciated.

Thanks in advance.]]>

Best regards,

Lin

]]>

` program redo

syntax [story_inst_control_4]

local antvar = wordcount("`story_inst_control_4'")

noisily display "antvar: `antvar'"`

Unfortunately, Stata now simply will not let me quit. If i press the x in the upper right hand corner of the window, it asks me to save, and then stays open. If I code, "exit" or "exit, clear" it stays open, and merely logs the command in the display window above.

What have I done, and how can I dig myself out of this hole?

]]>

Code:

* Example generated by -dataex-. To install: ssc install dataex clear input byte(k61 gender) float age byte edu 0 1 40 5 0 1 73 5 0 2 54 5 0 1 41 5 0 2 35 5 0 1 49 5 0 1 46 5 0 1 45 5 2 2 37 5 1 1 40 4 end reg k61 i.gender age edu matrix list r(table) mata : table = st_matrix("r(table)") mata : sum(table[4, .] :< 0.2)

I am working on to implement a random effect zip model in Mata. The model includes two random effects: one in the logit part, and another is in poisson part. Just wondering if anyone could give me an example how to do a double integration in Mata.

For example, my cluster level likelihood is as follows.

real colvector hiu1u2(real colvector u1, real colvector u2, real colvector Ai, real colvector Bi, real colvector zi, real colvector yi, real scalar s1, real scalar s2, real scalar t) {

n1=rows(u1)

n2=rows(u2)

r=J(n1*n2,1,0)

for(i=1; i<=n1;++i){

eta=Ai+s1*u1[i]

for(j=1;j<=n2;++j){

zeta=Bi+s2*u2[j]

lambda=exp(zeta)

pi=1/(1+exp(-eta))

p0=zi:*log(exp(eta)+exp(-lambda))+(1:-zi)*(-lambda+yi:*zeta-lngamma(yi+1))+log(1+exp(eta))

r[(i-1)*n1+j]=p0-.5*u1[i]^2-.5*u2[j]^2

}

return(r)

}

Just dont know how to conduct the double integration over u1 & u2. I tried the Quadrature but had problem to give upper and lower bounds for u1 and u2 simultaneously.

Any suggestion will be highly appreciated!

Thank you very much!

]]>

Code:

clear all webuse hsng2 mata: void mydemean(string scalar varlist, string scalar newvars, string scalar touse) { real matrix X, Xn st_view(X, ., varlist, touse) st_view(Xn, ., newvars, touse) Xn[., .] = X :- mean(X) X :- mean(X) Xn } end tempvar touse gen byte `touse' = 0 replace `touse' = 1 in 34/37 loc varlist "1b.region 2.region 3.region 4.region" foreach var in `varlist' { fvrevar `var' loc var "`r(varlist)'" qui replace `var' = . loc newvars "`newvars' `var'" } mata: mydemean("`varlist'", "`newvars'", "`touse'")

Code:

1 2 3 4 +-----------------------------+ 1 | 0 -.25 -.25 -.25 | 2 | 0 .75 -.25 -.25 | 3 | 0 -.25 .75 -.25 | 4 | 0 -.25 -.25 .75 | +-----------------------------+ [symmetric] 1 2 3 4 +-----------------+ 1 | 0 | 2 | 0 0 | 3 | 0 0 0 | 4 | 0 0 0 0 | +-----------------+

I get the same results in all versions from Stata 12 until the most recent update of Stata 16.]]>

For example

vr1 vr2

10 20

20 30

40 50

the vr1 has the value 20for obs2 and for obs 1 in vr2

thank you]]>