I am trying to figure out a tricky calculation with mata: First, I need to raise a non-integer number. namely 2.71282, to the power of each cell of a vector [1,2]. For illustration purposes, lets say I have a vector with the values 0.5 and 3.5 and now I want Stata to compute 2.71282^0.5 and 2.71282^3.5 and store the results yet again in a new matrix. In reality my vector contains 60 cells; that is why I would love some automatization here. In a second step I am raising each cell of the new vector to the power of 0.5. So both steps somehow resemble each other, however I am failing at both

I am a stata-beginner and originally inteded to go through the vector with the foreach command but I just don't know how to access the individual cells of the vector.

I would be extremely grateful if someone could give me a hint!

Thank a lot and Kind regards,

Mascha]]>

forvalues p=1/$nb_p {

forvalues i=1/$nb_p {

if pair[`p',1]=pair[`i',1] {

mata somme2`i'=somme-totob`p'

}

}

}

The error is "=exp not allowed

r(101);"

When I write if `p'=`i' or if pair[`p',1]=pair[`i',1] it fails.. but when I write if `p'!=`i' or if pair[`p',1]!=pair[`i',1] there is no error;..

I don't understand why, I am lost...

Thanks!

Clara

]]>

I am stuck on the following problem that I would like to model in Stata:

I have this multilevel model:

Outcome scores (3 time points) = B0 + B1 (independent variable 1)

Now I want to add three covariates; each covariate controlling for outcome scores that were completed just before each time point (where the outcome scores were completed again). So, for example; covariate one includes the outcome scores that were rated just before the first time point; the second covariate includes the outcome scores rated just before the second time point, etc.

But how to do so; is it even possible to add these covariates in a way that the covary out the score of the subsequent outcome score but not the other outcome scores? And how can these covariates be added in one coefficient into the model?

Does anyone have any experience with this?

Best regards,

Sanne Bruijniks]]>

I've been having an issue with the cumulative running sum (by row). I have a matrix X that is made of positive numbers and some 0s. My goal is to have the cumulative rowsum, but when it encounters a 0, to replace the matrix with a 0. Instead, of course, Mata is just replacing the matrix by the last sum+0.

Let me show an example:

. mata: x

1 2 3

+-------------------+

1 | 150 0 10 |

2 | 10 0 0 |

3 | 0 10 10 |

+-------------------+

1 2 3

+-------------------+

1 | 150 0 160 |

2 | 10 0 0 |

3 | 0 10 20 |

+-------------------+

mata: x = (150, 0, 10 \ 10, 0, 0 \ 0, 10, 10 )

mata: x_c_goal= (150, 0, 160 \ 10, 0, 0 \ 0, 10, 20 )

mata: nr = rows(x)mata: nc = cols(x)

I'm very new to Mata and the if commands, but I tried doing an if/else and telling it to perform the running sum when it encountered a value higher than 0, or else just assign the original value (0) inside the new matrix. See below:

mata: x_c = J(nr,nc,0)

mata: if x[j,]>0 for (j=1;j<=nr;j++) x_c[j,] = runningsum(x[j,],)

else for (j=1;j<=nr;j++) x_c[j,] = x[j,]

But this doesn't work and gives the error "'x' found where '(' expected".

Does someone know how to resolve this problem?

I know that if I were to replace 0s by missing, and then putting the "1" option in the runningsum(), then it wouldn't perform the running sum where it encounters missings. However, this means that it doesn't perform the sum also for the values in the columns after that..i.e. it gives this result

. mata: x

1 2 3

+-------------------+

1 | 150 . 10 |

2 | 10 . . |

3 | . 10 10 |

+-------------------+

. mata: x_c

1 2 3

+-------------------+

1 | 150 . . |

2 | 10 . . |

3 | . . . |

+-------------------+

And this is not what I want...

I hope I was clear enough...thanks a lot for your help!

Alessandra Stampi-Bombelli

Research Assistant

University of Zurich]]>

If no mata command exists (and I couldn't find one in the manual), I'd love tips on coding this up! Thanks in advance.]]>

Could someone check the following function? Last year, I was able to run a similar function for exporting a structure to a file, but now the fputmatrix() keeps reporting the type mismatch error. It works for matrices and classes but not for structures as it did.

I am using Stata 15.1 just updated on Windows server 2012 R2.

Update: I just tried it using Stata 11.2 again on Windows server 2012 R2 and it works nicely. I really do not understand what is going on...

Many many thanks.

Best,

Federico

clear all

mata

struct _le {

real matrix pop_le40

}

struct _le check()

{

struct _le rowvector _4le

_4le = J(1, 2, _le())

_4le[1].pop_le40 = J(10, 10, 1)

_4le[2].pop_le40 = J(10, 10, 2)

_4le[1].pop_le40

_4le[2].pop_le40

filename = "Z:\Microsimulation\BPCOmicrosim\analysis\bpco \che ck"

fh = fopen(filename, "rw")

fputmatrix(fh, _4le)

fclose(fh)

return(_4le)

}

end

m struct_res = check()

]]>

I have written a foreach loop to get the three different tables and I want to export the results into excel or word. My code is:

clear all

set more off

use "R:\CIHI data\Drug shortage\2018\Sasdata\daily_S_D_20180725.dta"

append using "R:\CIHI data\Drug shortage\2018\Sasdata\daily_Other_20180725.dta"

keep if existing==1

foreach x of varlist market_aig_route_form2 market_ai_route_form2 market_ai {

bysort date `x' : egen n_DINdate_market1= count (din_pdin)

bysort date `x' : egen n_shortagedate_market1= sum (sd)

gen n_DIN_shortage=1 if n_DINdate_market1==n_shortagedate_market1

keep if n_DIN_shortage==1

duplicates drop `x' n_DINdate_market1, force

bysort n_DINdate_market1: egen n_market= count (`x')

//rename _freq _freq1

duplicates drop n_market n_DINdate_market1, force

sort n_DINdate_market1

tab2xl n_market n_DINdate_market1 using results, row(1) col(1)

}

My question is that how can I get the result in the excel for each loop? I tried tab2xl command but it does not turn into a desired result I want and for each loop either

I would appreciate your help and many thanks for the useful website

Aida]]>

Code:

margins bplan, pwcompare

variable wt_vn_dw already defined

r(110);

Here is my following code:

stset duration [pweight = wt_vn_dw], failure(outcome_v2==1) scale(1)

stcrreg TM1_Male educ TM1_AGE ownership IND_EXP SU_EXP TOT_EMP TOT_HOURS_v2 TM_SIZEH GR_PREF BUS_HITECH CPT_W1LAG i.Industry ask ib(1999).project ib(0).bplan_form bplanlag ib(0).bplan#c.bplanlag , compete(outcome==3)

Is this because margins can calculate sample weighted competing risk regression data? It some how is linked to the variable that uses sampling weights wt_vn_dw for the analysis, but in my research in the issue I am not certain how to proceed to resolve the issue. Any insight our guidance to resources would be most useful. Thank you for your time.]]>

I'm conducting the command gvselect, but it gives me the following error:e(ll) not reported by regress

I do not understand why, I ran the regression again using eretrun list and it gives me the e(ll)

Does anybody have a clue what goes wrong here?

Thanks for thinking along in advance,

Best regards,

Sanne

syntax:

regress BDIm6 BDIbas

predict residchangescore, residuals

ssc install gvselect

gvselect <term> SelfreporteduseofAD NumberofC s1bg1 s1bg2 s1bg3 s1bg4 s1bg5 s1bg6 s1bg7 s1bg9 s1bg10 s1bg11 s1bg13 s1bg14 s1bg15 s1bg17 s1bg18 s1bg19 s1bg20 s1bg25 s1bg28 s1bg32 Happinessbas Moodssbas s1g22 RAND36sociaaltrans RAND36fysiektrans RAND36rolbeperkingfysiektrans RAND36rolbeperkingemotrans RAND36mentalegeztrans RAND36vitatrans RAND36gezvertrans RAND36pijntrans s1eq6 ACMTQaut ACMTQcon CCTStotal totalCCLbaseline totalCCLdepbaseline totalCCLanxbaseline RDQdepsymp RDQomgaanmetstress RDQposgez RDQfunctioneren RDQtevredenheidleven RDQalgemeenwelbevinden IPTskills IPTpreference CGTpreference s1et1 s1et2 s1et3 s1et4 s1et5 s1et6 s1et7 s1et8 s1et9 baselineBADSactivatie baselineBADSvermijdingruminatie baselineBADSwerkschool baselineBADStotal baselineCTQEM baselineCTQFM baselineCTQEV baselineCTQLV baselineCTQSM BDIOZA RAND36baseline BDIbaseline, nmodel(1): regress residchangescore <term>

]]>

b = ?

I have 10 observations and want to find the values of

Code:

clear input float(eq1 eq2) 174 22 60 14 252 24 . . . . . . . . . . . . . . end

Code:

clear mata mata void mysolver(todo, p, lnf, S, H) { a = p[1] b = p[2] lnf = ((b^2)+(3*a)-174)^2\ (a+b-22)^2 } S = optimize_init() optimize_init_evaluator(S, &mysolver()) optimize_init_evaluatortype(S, "v0") optimize_init_params(S, (1,1)) optimize_init_which(S, "min" ) optimize_init_tracelevel(S,"none") optimize_init_conv_ptol(S, 1e-16) optimize_init_conv_vtol(S, 1e-16) p = optimize(S) p 1 2 +-----------+ 1 | 10 12 | +-----------+ end