I have two variables for which I use the sum command to aggregate the values, so I end-up with two numbers. I want to export these two results so I use the matrix function. I also want to divide these two results together and export the result too. It is here that I have a problem. I do not find how to proceed for the division.

I have the following code :

Code:

.estpost tabstat VARIABLE1 if year == 2008, statistics(sum)

.esttab, wide nonumber cell("sum(label(Total Variable1 in 2008))")

.mat II = e(sum)'

.estpost tabstat VARIABLE2 if year == 2008, statistics(sum)

.esttab, wide nonumber cell("sum(label(Total Variable2 in 2008))")

.mat JJ = e(sum)'

.putexcel set "name.xlsx", replace

.putexcel A3 = "2008"

.putexcel B3 = matrix(II)

.putexcel C3 = matrix(JJ)

Thank you in advance.

David]]>

nonconstant invalid.r(198);

thank you]]>

To make this concrete, here's a example.

Code:

local have_list `" "This is" "my list" "to" "test" "' mata want = J(1, 0, "") foreach item of local have_list { mac list _item mata: item = st_local("item") mata: want = want , item } mata: want

I coded an optimization and I would like to repeat that optimization for subsamples randomly drawn from my sample.

Code:

*Here, I define the variables used in the optimization mata: void GMMTL(todo,betas,crit,g,H) { PHI=st_data(.,("phi")) LPHI = st_data(.,("lagphi")) LED = st_data(.,("lagExportD")) Z=st_data(.,("const","withinllogl","withinlogk1","withinllogl2","withinlogk2","withinlogkllogl","withinllogm","withinllogm2","withinllogmllogl","withinlogkllogm")) X=st_data(.,("const","withinlogl1","withinlogk1","withinlogl2","withinlogk2","withinl1k1","withinlogm1","withinlogm2","withinl1m1","withink1m1")) lagX = st_data(.,("const","withinllogl","withinllogk","withinllogl2","withinllogk2","withinllogkllogl","withinllogm","withinllogm2","withinllogmllogl","withinllogkllogm")) Y=st_data(.,("withinlogy")) C=st_data(.,("const")) VAR = (C,PHI,LPHI,LED,Z,X,lagX,Y,C) *Here, I draw a random subsample of my data and replace the original vectors. VAR[,1] = runiform(rows(VAR),1) :> .9 PHI = select(VAR[.,2], VAR[.,1]:~=0) LPHI = select(VAR[.,3], VAR[.,1]:~=0) LED = select(VAR[.,4], VAR[.,1]:~=0) Z = select(VAR[.,4\5\6\7\8\9\10\11\12\13], VAR[.,1]:~=0) X = select(VAR[.,14\15\16\17\18\19\20\21\22\23], VAR[.,1]:~=0) lagX= select(VAR[.,24\25\26\27\28\29\30\31\32\33], VAR[.,1]:~=0) Y=select(VAR[.,34], VAR[.,1]:~=0) C=select(VAR[.,35], VAR[.,1]:~=0) *Here, I define the system of equations to be solved by the optimizer. OMEGA=PHI-X*betas' lagOMEGA=LPHI-lagX*betas' lagOMEGAlagED =diagonal((LPHI-lagX*betas')*LED') lagOMEGAsq= diagonal((LPHI-lagX*betas')*(LPHI-lagX*betas')') lagOMEGAsqlagED = diagonal(diagonal((LPHI-lagX*betas')*(LPHI-lagX*betas')')*LED') lagOMEGAcub= diagonal(lagOMEGAsq*(LPHI-lagX*betas')') lagOMEGAcublagED = diagonal(diagonal(lagOMEGAsq*(LPHI-lagX*betas')')*LED') pollagOMEGA = (C,lagOMEGA,lagOMEGAsq,lagOMEGAcub,lagOMEGAlagED,lagOMEGAsqlagED,lagOMEGAcublagED,LED) g_b = invsym(pollagOMEGA'pollagOMEGA)*pollagOMEGA'OMEGA XI = OMEGA-pollagOMEGA*g_b crit=(Z'XI)'(Z'XI) } *This is just fixing the optimization settings. S=optimize_init() optimize_init_evaluator(S, &GMMTL()) optimize_init_evaluatortype(S,"d0") optimize_init_technique(S,"nm") optimize_init_nmsimplexdeltas(S, 0.1) optimize_init_conv_maxiter(S, 1000) optimize_init_which(S,"min") optimize_init_params(S,(7,0.1911017,0.051978,0.0308493,0.0113114,0.0304485,0.5731468,0.0410968,-0.073725,-0.0350174)) p=optimize(S)

Code:

mata bhat = J(1000, 10, .) end

Code:

forval i=1/1000 {

and closing the loop by using

Code:

bhat[i,] = optimize_result_params(S) }

I have also tried to start the loop within mata using:

Code:

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

Code:

void GMMTL(todo,betas,crit,g,H)

I would appreciate any idea how to fix this. ]]>

such as mkmat to generate matrices, and use functions such as st_data() or st_view() to generate mata.

Is there a universal (the tables generated for various commands is generally applicable), selected (you can choose the corresponding row and column at will), direct (rather than

through a series of steps) command or method to generate Matrix or mata?

For example:

example 1:

Code:

npresent a b c

example 2:

Code:

fsum a b c

Need you help,Your reply is a great help to me, Thank you very much

]]>

// two factor variables,the second column has two levels(1 2), the thrird column has three levels(1 2 3) ,then I will do loop on the two factor variables

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

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

Dij=st_select(D,ij) \\ gen Dij matrix when the second column select i(1 or 2) and the third column select(1 or 2 or 3), then we get six matrixes:D11 D12 D13 D21 D22 D23

colnames = "_i_j" \\ rename the colnames of matrixes Dij

Dij = colnonmissing(Dij)' \\ then perform the same operation on the generated matrix

}

}

end

i don't know how to write the two rows of program in red, can you help? thank you very much]]>

I occasionally find the function

http://repec.org/bocode/h/htm.ado

here

http://repec.org/bocode/d/drany.ado

and other, mostly in codes of

I believe

Code:

. version 16.0 . mata truish(1) <istmt>: 3499 truish() not found r(3499);

Thank you, Sergiy]]>

I am new to mata, and I'm replicating an old code that uses it. Unfortunately, I'm getting the errors `invalid lval r(3000)` after the assignment of elements x, x1, estvcov and estmean (lines 3, 4, 5, 6, 7, 12, 14, 15 of the code below). I understand this error appears when the element is not an lval (https://www.stata.com/manuals13/m-2op_assignment.pdf). However, all of the following items should be matrices, and so I do not understand why this error appears. (Note that simvalues1 and simvalues2 are also matrices).

Would highly appreciate any help!

Thanks.

mata

function pctChangeCI(x, x1, estmean, estvcov) {

// x = (st_matrix("simvalues1"), 1)

// x1 = (st_matrix("simvalues2"), 1)

//x = (1, st_matrix("simvalues1"))

//x1 = (1, st_matrix("simvalues2"))

//estvcov = ( 0.0321425679334, -0.0125258708837030, -0.01224257550358, -0.020567673273258, -0.00081447962364744, 0.001283175119054, -0.00037078276968431) \ (-0.0125258708837, 0.0247147324992786, 0.00572957906882, 0.013578287152935, -0.00021238922227112, -0.000095793687137, 0.00000054591255634) \ (-0.0122425755036, 0.0057295790688211, 0.02474244266765, 0.000709502894560, 0.00038112327914381, -0.000473762169358, 0.00003959881093361) \ (-0.0205676732733, 0.0135782871529354, 0.00070950289456, 0.046983991203950, 0.00015659148543411, 0.000004182942966, -0.00007048925426224) \ (-0.0008144796236, -0.0002123892222711, 0.00038112327914, 0.000156591485434, 0.00010085537196102, -0.000047774418586, 0.00000001528810965)\ ( 0.0012831751191, -0.0000957936871374, -0.00047376216936, 0.000004182942966, -0.00004777441858647, 0.000163152376626, -0.00000408242456036)\ (-0.0003707827697, 0.0000005459125563, 0.00003959881093, -0.000070489254262, 0.00000001528810965, -0.000004082424560, 0.00003239995503067)

//estmean = (-2.53549625658, 2.43252537821, 4.12186913122, 1.05335147161, 0.04816370328, -0.06482512498, -0.06319703473)

conflevel = .95

//estmean = st_matrix("e(b)")

//estvcov = st_matrix("e(V)")

probvec = (.5, (1-conflevel)/2, 1-(1-conflevel)/2)

g_theta = (invlogit(rowsum(x1:*estmean)) - invlogit(rowsum(x:*estmean)))/invlogit(rowsum(x:*estmean))

dg_theta = (invlogit(rowsum(x:*estmean)) * (x1 * logisticden(rowsum(x1:*estmean)) - (x * logisticden(rowsum(x:*estmean)))) - (invlogit(rowsum(x1:*estmean)) - invlogit(rowsum(x:*estmean))) * x * logisticden(rowsum(x:*estmean))) / (invlogit(rowsum(x:*estmean))^2)

g_theta_sd = sqrt(dg_theta * estvcov * dg_theta')

probvec = (invnormal(probvec):*g_theta_sd:+g_theta):*100

return(probvec)

}

end

]]>

I am trying to calculate the distance between two datsets using their latitude & longitude locations and the 'nearstat' command.

Following, the used commands are listed:

************************************************** ****************************

use "$data\Prep_Buffer_DHS.dta", clear

append using "$data\All_Location_Insa_nodup.dta", keep(id latitude longitude)

* Sort so that nearstat will run, putting all projects first

gsort -id

* First count how many projects are within 200 km to know how many loops->to run nearstat

foreach dist in 15 25 50 75 100 200 {

nearstat LATNUM LONGNUM, ///

near(latitude longitude) distvar(_1LSLAdist`dist') ///

ncount(numberwithin`dist') dband(0 `dist') favor(speed)

}

drop _1LSLAdist15 _1LSLAdist25 _1LSLAdist50 _1LSLAdist75 _1LSLAdist100 _1LSLAdist200

************************************************** **********************************

When I run the 'nearstat' command, I get the following error message:

'The order for the nearest neighbor cannot be greater than the number of near features.'

Does anyone know how to interpret the error message? I could'nt find a solution yet.

Thank you very much for your support in advance and warm greetings !

]]>