I'm learning deriv(). I use the log gamma function in practice, and use Mata's digamma() and trigamma() to check whether I do it right.

As can be seen from the output below, the first derivative from deriv() is the same as that from digamma(), but the second derivative from deriv(D,2) is very different from what trigamma() produces. I wonder whether the difference is due to numerical errors. If it is not, then I must misunderstand something. Therefore, I would like to seek your helps.

Code:

cap m: mata drop D cap m: mata drop DLnGamma() mata: void DLnGamma(x,y){ y=lngamma(x) } D = deriv_init() deriv_init_evaluator(D, &DLnGamma()) deriv_init_params(D, 2) Deriv =deriv(D, 1), deriv(D, 2) end mata: Deriv\(digamma(2),trigamma(2)) 1 2 +-----------------------------+ 1 | .4206921333 5.12566e+11 | 2 | .4227843351 .6449340668 | +-----------------------------+

Below is a bit more test. I use deriv() to calculate the first derivative of the digamma function. If I understand correctly, the result should produces the second derivative of the log gamma function (i.e., the result from trigamma().) This time, the results do match. However, I still can't figure out why the code

Code:

cap m: mata drop D cap m: mata drop DDiGamma() mata: void DDiGamma(x,y){ y=digamma(x) } D = deriv_init() deriv_init_evaluator(D, &DDiGamma()) deriv_init_params(D, 2) Deriv =deriv(D, 0), deriv(D, 1) end mata: Deriv\(digamma(2),trigamma(2)) 1 2 +-----------------------------+ 1 | .4227843351 .6449340674 | 2 | .4227843351 .6449340668 | +-----------------------------+

I am currently aiming to use the values of a Mata matrix in a regression in Stata. I have found earlier suggestions, however my situation is slightly different. The solution that I've added below does not work for me since the first assumption that the writer makes does not hold for me. I have a dataset in Stata with 60.000+ variables with unique identifiers "citypairairline". Furthermore I have a variable "airline" which is categorical and could take values 1 to 13, corresponding with the number of columns and rows in the Mata matrix. I now want to return the values of the Mata matrix to Stata such that 13 new variables are created in Stata, with matching columns to the 13 different variables and matching the values of "airline" 1 to 13 to the rows. (or the other way around, it is a symmetric matrix)

Thank you in advance,

Frank

I'll show how, but first I am going to make some assumptions:

1. Thomas didn't say whether the number of observations in the Stata dataset are the same as the number of rows in Results, but what he wrote implied it.

2. Thomas didn't say whether the results in the Stata dataset and the results in Results are in the same order, but I will assume that they are.

Solution 1: Stata/Mata approach -------------------------------

At the outset, in Stata, create the three new variables to hold the results:

. gen Result1 = .

. gen Result2 = .

. gen Result3 = .

Now, back in Mata, after calculations of mata matrix Results, code, :

st_store(., ("Result1", "Result2", "Result3"), Results[|1,3 \ .,.|] )

Explanation: Matrix Results is N x 5, but the first two variables (columns) in the matrix already appear in the data, simply want to add 3, 4, and 5 to the Stata dataset. There are lots of other ways I could have coded st_store().

Among them: : st_store(., "Result1", Results[3,.]) : st_store(., "Result2", Results[4,.]) : st_store(., "Result3", Results[5,.]) or : st_store(., ("Result1", "Result2", "Result3"), Results[(., (3,4,5)] )

The way I chose, storing Results[|1,3 \ .,.|], says to pull the submatrix of Results from top-left 1,3 to bottom right number of rows and number of columns.]]>

I'm using Stata 14.1, and in particular the "optimise" mata routine.

I'm writing some stata and mata code to perform permutation-based inference with inverse probability weights to correct for attrition. I'm generating these weights by performing simple maximum likelihood estimation of a logit model on R samples with randomly permuted treatment assignment vectors. This code is embedded in a mata function which is then called by an external stata routine.

My code is currently similar to the following:

Code:

for (r=1; r<=R; r++) { /* permutation loop */ /* computing OLS starting values (X are covariates, A is outcome) */ data = A,X XpXi = quadcross(X, X) XpXi = invsym(XpXi) binit = XpXi*quadcross(X, A) /* Optimisation ------------------------------- */ S=optimize_init() optimize_init_evaluator(S,&logitf()) optimize_init_evaluatortype(S,"v0") optimize_init_argument(S,1,data) optimize_init_params(S,binit') optimize(S) /* get coefficients */ beta=optimize_result_params(S)' /* compute linear index */ linind = X * beta /* ... more stuff with the estimated coefficients */ } /* permutation loop */

"convergence not achieved"

"Hessian is not negative semidefinite"

This is expected given the small sample size (around 250 observations, with lower numbers due to attrition).

I'm fine with the optimisation failing for some permutations, but I have two questions:

1) How can I manage these errors and avoid the whole stata routine that wraps my function to stop?

2) How can I keep track of how many of the permutations failed to optimise?

Thanks so much for your help!

Giacomo]]>

I wonder whether there is a Mata command for checking the existence of a Mata object.

In Stata, we can use confirm and _rc. Can we do the same thing in Mata?

Thank you very much.]]>

Code:

mata: regexm("{12}", "{([0-9]+)}") ? regexs(1) : ""

Code:

mata: ustrregexm("{12}", "{([0-9]+)}") ? ustrregexs(1) : ""

1. The Unicode ustrregexm() returns a negative number if an error occurs. Here it returns -1, which happens to be true. We need to check for > 0.

2. The error occurs because ustrregexm() requires escaping literal braces, whereas regexm() works with or without escaping. (I think this is because we can now use patterns such as "[0-9]{3}" to match 3 or more digits.)

3. The function ustrregexs(n) will crash if called before a valid match or with an invalid group number, whereas regexs() will print an error message. (regexs() can also be called with no arguments to return all groups.)

Taking care of 1 and 2 avoids 3. The corrected one-liner is

Code:

mata: ustrregexm("{12}", "\{([0-9]+)\}") > 0 ? ustrregexs(1) : ""

Germán]]>

i type this command,

metan n1 mean1 sd1 n2 mean2 sd2, favours(aerobic # control)

but i faced with this problem by STATA:

option favours() not allowed

can anyone help me?

moreover, i type my data sheet as attached file.i must mention that i am a beginner STATA user and your help is very important to conduct my research.

Kind Regard

Masoud]]>

I'm very new to Stata (only a few days), statistics and programming in general so do bear with me if I ask any silly questions or describe things clumsily.

I'm trying to use a cubic spline to interpolate monthly GDP per capita data from yearly GDP per capita for a panel dataset (160 countries and 193 time points) for an undergraduate Sociology project. I understand that it is a bit of a stretch to interpolate 11 data points from 2, and so would refine the method if given the chance, but due to time constraints I have to make do with this method for now.

Previous forum comments suggest using the -csipolate- but I am unable to install the package as I am using a shared computer and do not have permission to update the ado folder. Any suggestions of how I may be able to bypass this?

Meanwhile, I am testing the code with -ipolate- for the moment but am getting strange results. The new variable does not copy over the exising data points in the old variable and the new data points are all recorded as unitary. My code is as follows:

xtset cname1 date2

bysort cname1: ipolate gdpc1 date2, g(gdpc_ipo)

A screenshot of my data editor:

Array

Would greatly appreciate any help. Thank you!]]>

I have written this simple ready-to-execute code for computing the yield-to-maturity of a two-period coupon-paying bond:

Code:

```
clear
input float(AMOUNTISSUED CF_1 CF_2) double YTM
100 23 22 .
end
mata
z=J(1,1,.)
st_view(z,., "YTM AMOUNTISSUED CF_1 CF_2")
function s(ytm,amountissued,cf_1,cf_2) {
return((amountissued+cf_2)/ytm^2+cf_1/ytm - amountissued)
}
for (i=1;i<=rows(z);i++) {
r=mm_root(YTM=.,&s(),epsilon(1),5-epsilon(1),1e-9,1000,z[i,2],z[i,3],z[i,4])
YTM-1
z[i,1]=YTM-1
}
end
```

However, this code is insufficient for my final purpose: I need to define the function s(.) dynamically for a varying number of cash-flows CF_1 ... CF_N.

Given my limited Mata knowledge, I think that the easiest solution is to write the respective formula as a string using Stata and then to evaluate this formula in the function s(ytm). The code would look like this:

Code:

clear input double YTM str20 formula . "(100+23)/ytm^2 + 23/ytm - 100" . "(100+20)/ytm^3 + 20/ytm^2 + 20/ytm- 100" *. "(100+20)/ytm^N + ... + 20/ytm^2 + 20/ytm- 100" end mata z=J(1,1,.) st_view(z,., "YTM") function s(ytm) { return(??????????)} for (i=1;i<=rows(z);i++) { r=mm_root(YTM=.,&s(),epsilon(1),5-epsilon(1),1e-9,1000) YTM-1 z[i,1]=YTM-1 } end

Do you have any suggestions on how to evaluate the string formula "(100+23)/ytm^2 + 23/ytm - 100" using function s(ytm)?

Thank you,

David]]>

Code:

mata c = -y, rangen(0, 0, N)', N, N-1 polyroots(c) end

I am working on time series, and I have several problems.

My subject is about strike in France during the 20th century.

I want to explain the number of days-worked losts with the ideology of the communist party (i.e. the number of vote share for this party at parliament elections) and controlling for others factors like economic growth, inflation, union-density, unemployment, share of industry, agriculture, and service sector, and wages.

My dependent variable is in log and non-stationary and with a downward trend.

My interest variable ( vote share for communist party at parliament elections) is non stationary .

Share of industry and service, wages, and active population are non stationary.

Economic growth, unemployment and inflation, union density are stationary

I used a Dicker Fuller tests and Phillipps Perron test in order to verify the stationarity. Thereafter you can see the ac and pac of my dependant variable. I don't know what type of model should I try.

(I don't have data for the world wars). I know that I have to differentiate my datas, but I don't know how to deal because some are stationaries and some not.

Then I have another question: I want to test the size of the strikes with the same independant variable. My variable of size is stationary and the pac and ac shows me that there is only one year of partiel autocorrelation and one year of autocorrelation. Can I use an OLS standard?

Thank you in advance, and sorry it is the first time that I try to work on time series.

A. Collet,

Array

Array

Array ]]>

Code:

forvalues i = 1/1000 { sort id var_`i' by id (var_`i'): gen newvar_`i' = var_`i'[_N] }

I am currently trying to generate a matrix from stata data in mata, using groups of observations as rows. Normally I would collapse the data to the desired level of observations. I however do not want to ruin the dataset since this would not enable me to run regressions on the original level of observations anymore.

The dataset looks something like an extended version of this: (each observation contains information about a route and airline, where route_airline and route are id (numeric) variables at different levels of observation)

route_airline | route | airline1 | airline2 | airline3 | airline4 |

30011 | 3001 | 1 | 0 | 0 | 0 |

30013 | 3001 | 0 | 0 | 1 | 0 |

30014 | 3001 | 0 | 0 | 0 | 1 |

30021 | 3002 | 1 | 0 | 0 | 0 |

30022 | 3002 | 0 | 1 | 0 | 0 |

30031 | 3003 | 1 | 0 | 0 | 0 |

30032 | 3003 | 0 | 1 | 0 | 0 |

30033 | 3003 | 0 | 0 | 1 | 0 |

Thanks a lot for your help!

Frank]]>

xtgls ROA i.Family_firms##i.Political_Connection i.Public_Firms##i.Political_Connection...(with control variables, but i removed to make it short here).

Thank you all in advance!!

Jackson

VARIABLES | ROA |

0b.Family_firms#0b.Political_Connection | 0 |

(0) | |

0b.Family_firms#1.Political_Connection | 0.799*** |

(0.172) | |

1.Family_firms#0b.Political_Connection | -0.224 |

(0.217) | |

1.Family_firms#1.Political_Connection | 0 |

(0) | |

0b.Public_Firms#0b.Political_Connection | 0 |

(0) | |

0b.Public_Firms#1.Political_Connection | 0.831*** |

(0.0868) | |

1.Public_Firms#0b.Political_Connection | 0.598*** |

(0.205) | |

1o.Public_Firms#1o.Political_Connection | 0 |

(0) | |

Debt_to_Asset | -7.859*** |

(0.157) | |

Debt_to_Equity | -0.443*** |

(0.0271) | |

ln_TT_Asset | 0.00866 |

(0.0414) |

]]>