Sunday, July 31, 2022

Interaction variable (using dummy and date variables)

Hello everyone,

I am trying to create an interaction variable by multiplying a dummy variable (0,1) with a date variable. I am thinking that for all values of 0 multiplied by the date should be 0, while those multiplied by 1 should be the same as the date. I realize that once I format the interaction variable to a date variable, I get a date of 1960m1 for those multiplied by 0. I have included an example and would be grateful to know if there is a way to get 0 values for the interaction variables instead of the '1960m1' date.

Thank you.

[CODE]
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(after_policy date date_after)
1 1993m6 1993m6
0 1991m2 1960m1
0 1992m8 1960m1
1 1993m7 1993m7
0 1991m7 1960m1
end



regress help requested

I have a dataset in which all the dependent variables are for the years 2020 & 2021. I have 4 dependent variables, which are used in 4 different regressions. The independent variables are for the years 2018, 2019, 2020 & 2021.

Here is a sample of the dependent variables:
Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input int year float(raw_return_ AbRet) double Vol1Yr_ float idio_volatility
2018         .         .       . .023706943
2019         .         .       . .023706943
2020         .         . 36.2264 .023706943
2021 -.3217149 -9.685585 28.7132 .023706943
2018         .         .       . .023706943
2019         .         .       . .023706943
2020         .         . 36.2264 .023706943
2021 -.3217149 -9.685585 28.7132 .023706943
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
2018         .         .       . .017640421
2019         .         .       . .017640421
2020  .0612004 -20.10798 28.3259 .017640421
2021         .         . 11.9041 .017640421
end

I have a plenty of independent variables. Here is a sample:

input int year byte(ESGScrFY ENVScrFY SocialScrFY GOVScrFY) float(size ROE profitability cash_over_assets stdebt_over_assets mtb) byte neg_mtb float HistVolatility
2018 13 0 4 26 6.073982 .021759655 .3056392 .3541455 .12199316 13.067905 0 1040.8689
2019 16 0 2 28 6.235095 .02212184 .2627229 .2828731 .09094698 12.02771 0 1040.8689
2020 20 0 8 32 6.534146 .023738274 .28146 .3461379 .1122328 13.204992 0 1040.8689
2021 24 16 17 31 6.548369 .01363123 .22036627 .3311015 .10458087 18.168388 0 1040.8689
2018 13 0 4 26 6.073982 .021759655 .3056392 .3541455 .12199316 13.067905 0 1040.8689
2019 16 0 2 28 6.235095 .02212184 .2627229 .2828731 .09094698 12.02771 0 1040.8689
2020 20 0 8 32 6.534146 .023738274 .28146 .3461379 .1122328 13.204992 0 1040.8689
2021 24 16 17 31 6.548369 .01363123 .22036627 .3311015 .10458087 18.168388 0 1040.8689
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 .
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 .
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 .
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 .
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 .
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 .
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 .
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 .
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 .
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 .
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 .
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 .
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 .
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 .
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 .
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 .
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 .
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 .
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 .
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 .
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 199.5806
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 199.5806
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 199.5806
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 199.5806
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 199.5806
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 199.5806
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 199.5806
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 199.5806
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 199.5806
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 199.5806
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 199.5806
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 199.5806
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 198.37877
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 198.37877
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 198.37877
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 198.37877
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 198.37877
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 198.37877
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 198.37877
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 198.37877
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 198.37877
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 198.37877
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 198.37877
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 198.37877
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 198.37877
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 198.37877
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 198.37877
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 198.37877
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 198.37877
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 198.37877
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 198.37877
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 198.37877
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 180.287
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 180.287
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 180.287
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 180.287
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 180.287
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 180.287
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 180.287
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 180.287
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 180.287
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 180.287
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 180.287
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 180.287
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 180.287
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 180.287
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 180.287
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 180.287
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 180.287
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 180.287
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 180.287
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 180.287
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 170.62106
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 170.62106
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 170.62106
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 170.62106
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 170.62106
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 170.62106
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 170.62106
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 170.62106
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 170.62106
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 170.62106
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 170.62106
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 170.62106
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 170.62106
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 170.62106
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 170.62106
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 170.62106
2018 76 83 73 75 9.837869 .08979048 .0126145 .020834863 .8490396 1.8775734 0 170.62106
2019 75 86 78 71 9.90103 .09026607 .011974652 .016592776 .8493556 1.830828 0 170.62106
2020 78 91 75 77 9.893396 .09819403 .009640835 .07133553 .8490928 1.442319 0 170.62106
2021 77 89 74 76 10.083617 .09861282 .012845165 .065547965 .8628278 1.7681098 0 170.62106
end
[/CODE]

I want to perform a regress where the dependent variable is from 2020 & 2021, but the independent variables are from 2018 & 2019. The following command gives me an error, since the dependent variables do not exist for those years.

regress raw_return_ ESGScrFY ENVScrFY SocialScrFY GOVScrFY size ROE profitability cash_over_assets stdebt_over_assets mtb neg_mtb HistVolatility if year==2018|year==2019, vce(cluster num_CIQ_ID)
no observations
r(2000);

How can I perform this regression where my dependent variable is from 2020 & 2021 and my independent variables are from 2018 & 2019. I would be very grateful for some advice.



Subgroup analysis using Metan command

Dear group,

Stata novice here trying to create meta-analysis forest plots using Stata.
I am really struggling to figure out how to do subgroup analysis using the metan command. Reading the help file, I have figured out "by" command should do the trick, but I must be doing something wrong as Stata keeps on saying "option by not allowed".

The following is the command I have tried to use.

Code:
metan lnor lnlci lnuci if food=="fruit", ///
eform random effect (Odds Ratio) ///
lcols (author year n country ortype) ///
xlabel (0.05 0.1 1 5)///
by income
Please help!

Confidence intervals for percent change

Dear all,

This question might sound stupid but I can't find the way to calculate 95%CI for percent change between frequencies (but not between proportion).

For example:
9630 events in year X
6754 events in year Y
percent change = (9630-6754)/9630= -30%

How can I calculate the 95%CI of the percent change without having the denominator for these events?

Many thanks for your help!

Kind regards,

Manon

A loop problem

Dear all:

I have a problem when dealing with my data, for one variable. There is only "1","2", and "0", I want to find the first "1", and then find the next "2", then find the next "1",and so on like the following example:
from the right to the left
0 0
0 0
1 1
1 0
1 0
1 0
0 0
1 0
2 2
2 0
1 1
1 0

Thank you!
Best wishes

Estimated parameters more than the observations

Hello everyone,
I am writing my Bachelor's thesis.
I am doing a time series analysis and using a VECM model.
I have noticed that the number of my estimates parameters is higher than the observations. Therefore, my supervisor recommended me to add in the thesis a section in which I explain what this implies for my analysis.
However, I am bit lost. I am aware that this may make the results more unreliable and biased. But I honestly do not really know what else to add. Do you have any suggestions?
Thank you in advance for the help,

Stefano

Unbalanced covariates when the treatment variable has 3 levels

Hello, I have performed covariate balancing test and after seeing the result I am confused. Could you please suggest me how to correct the unbalanced covariates when the treatment variable has 3 levels. Thank you very much.
Faruque.

Saturday, July 30, 2022

Looping with paired values but not for every variable

Hi, let's say I want to create a series of dummy variables as follows:

Code:
order work_deg_sector*,sequential
egen dummy1_1970 = anymatch(work_deg_sector_1998_01-work_deg_sector_2005_04) if byear==1970,v(1)
egen dummy1_1971 = anymatch(work_deg_sector_1998_01-work_deg_sector_2006_05) if byear==1971,v(1)
egen dummy1_1972 = anymatch(work_deg_sector_1998_01-work_deg_sector_2007_05) if byear==1972,v(1)
egen dummy1_1973 = anymatch(work_deg_sector_1998_01-work_deg_sector_2008_08) if byear==1973,v(1)
egen dummy1_1974 = anymatch(work_deg_sector_1998_01-work_deg_sector_2009_03) if byear==1974,v(1)

gen dummy1 = .
replace dummy1 = dummy1_1970 if byear==1970
replace dummy1 = dummy1_1971 if byear==1971
replace dummy1 = dummy1_1972 if byear==1972
replace dummy1 = dummy1_1973 if byear==1973
replace dummy1 = dummy1_1974 if byear==1974


egen dummy2_1970 = anymatch(work_deg_sector_1998_01-work_deg_sector_2005_04) if byear==1970,v(2)
egen dummy2_1971 = anymatch(work_deg_sector_1998_01-work_deg_sector_2006_05) if byear==1971,v(2)
egen dummy2_1972 = anymatch(work_deg_sector_1998_01-work_deg_sector_2007_05) if byear==1972,v(2)
egen dummy2_1973 = anymatch(work_deg_sector_1998_01-work_deg_sector_2008_08) if byear==1973,v(2)
egen dummy2_1974 = anymatch(work_deg_sector_1998_01-work_deg_sector_2009_03) if byear==1974,v(2)

gen dummy2 = .
replace dummy2 = dummy2_1970 if byear==1970
replace dummy2 = dummy2_1971 if byear==1971
replace dummy2 = dummy2_1972 if byear==1972
replace dummy2 = dummy2_1973 if byear==1973
replace dummy2 = dummy2_1974 if byear==1974
I want to create multiple dummy variables. Accordingly, I would like a loop that changes the "dummy1" name to "dummy2" when the loop also changes "v(1)" to "v(2)". However, I am not fully sure how to do this. For example, if I run the loop below, then it'll try to create dummy1 and dummy2 twice, using each values that goes into v()

Code:
local dvars dummy1 dummy2 
foreach d of local dvars {
foreach i of numlist 1 2 {

egen `d'_1970 = anymatch(work_deg_sector_1998_01-work_deg_sector_2005_04) if byear==1970,v(`i')
egen `d'_1971 = anymatch(work_deg_sector_1998_01-work_deg_sector_2006_05) if byear==1971,v(`i')
egen `d'_1972 = anymatch(work_deg_sector_1998_01-work_deg_sector_2007_05) if byear==1972,v(`i')
egen `d'_1973 = anymatch(work_deg_sector_1998_01-work_deg_sector_2008_08) if byear==1973,v(`i')
egen `d'_1974 = anymatch(work_deg_sector_1998_01-work_deg_sector_2009_03) if byear==1974,v(`i')

gen `d' = .
replace `d' = `d'_1970 if byear==1970
replace `d' = `d'_1971 if byear==1971
replace `d' = `d'_1972 if byear==1972
replace `d' = `d'_1973 if byear==1973
replace `d' = `d'_1974 if byear==1974


}
}
What would be the most efficient way of creating these multiple dummies? Note that the dummies will have entirely different names (e.g., south, north).

Please let me know if I should clarify anything

"[" invalid command name error while running simple linear regression in STATA

Hi,

I am trying to run the linear regression command (reg dep independent variables) in STATA but apparently, it gives an error when I execute the command.
My dependent variable is: Monthly electricity consumption of the household in summers whereas independent variables are: total dwelling size, total number of adults, total number of children, total number of working members, annual income of the household.
I am using STATA 12.1 version. The invalid command name error "[" is quite confusing as I am not using closed brackets anywhere in the command.
Any help is appreciated.

bar graph

Dear All, Suppose that I have this data set:
Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input str63 Journal_e float IF1_omega_w1
"International Economic Review"                                    .238486
"The Journal of World Economy"                                   .20098802
"China Rural Survey"                                             .09736695
"Modern Economic Science"                                       .072677195
"Researches in Chinese Economic History"                         .04450123
"Economic Research Journal"                                      .03934519
"The Journal of Quantitative & Technical Economics"              .03745284
"Zhejiang Finance"                                               .03477633
"Tourism Science"                                               .029117016
"Ancient and Modern Agriculture"                                 .02865064
"World Economy Studies"                                          .02840467
"Scientific and Technological Management of Land and Resources"  .02704981
"Finance & Trade Economics"                                      .02460474
"Tourism Tribune"                                               .021358477
"Economic Theory and Business Management"                       .019578734
"Journal of Beijing Forestry University"                        .016145939
"Chinese Rural Economy"                                         .011040545
"Journal of Shanghai University of Finance and Economics"       .009785404
"Economic Review"                                               .007974063
"Value Engineering"                                             .005818428
end
I'd like to draw a bar graph. However, The names of Journals (Journal_e) are long. Any suggestions? Thanks.

type mismatch error in declaring a function of a class

Dear Statalist,

I have one question regarding the mata programming. I couldn't figure out why mata reports an error "type mismatch: exp.exp: transmorphic found where struct expected" when it goes to codes
Code:
AA.aa = sum(A1.a)
for function -myfct2-.

Kind regards,
Yugen

Code:
mata:

mata clear

//-------------------------------------------------
class Myclass1 {    
    
    real vector a
    
}

class Myclass1 scalar myfct1 (real vector b)  {
    
    class Myclass1 scalar A

    A.a = b

    return(A)
}

//-------------------------------------------------

class Myclass2  {

    real scalar aa

}

class Myclass2 scalar myfct2 (real vector b)  {
    
    class Myclass2 scalar AA
    
    A1=myfct1(b)
    
    AA.aa = sum(A1.a)
    
    return(AA)
    
    
}


end

traj plugin

Dear all

I would like to run group based trajectory modelling in STATA. Please advise how I can download this plugin and install in a VDI. In the VDI, I do not have access to the internet.
Thank you

Modeling a skewed categorical (ordinal) variable

Hi Statalist,

The response scale of my dependent variable is structured as "Not at all" "To a small extent" "To a moderate extent" and "To a great extent." The survey question asks "Have your organization used social media for the following purposes?" 1) Newsletter, 2) Important announcements, 3) Customer service, 4) Events, 5) General community engagement, and 6) Recruitment.
My current strategy is to build a composite measure of social media use (eg., egen rowmean) where "Not at all" translates to 1, "Moderate extent" to 2, ..., and run it as an OLS model.
But the distribution is highly positively skewed where more than half responded to "Not at all" for most categories. I wonder if I should employ a different strategy (eg., multinomial logit, logit, count models).

My question is...
1. Is it OK to construct a composite score scale and run an OLS?
2. Is there a way I can model it as a composite measure rather than modeling the 6 subdimensions separately?
3. Any other suggestions I would highly appreciate.

Here is an example of my data. Thanks.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str20(sm_newsletter sm_annoucements sm_customer sm_events sm_engagement sm_recruit)
"Not at all"           "To a great extent"    "To a great extent"    "To a great extent"    "To a moderate extent" "To a small extent"   
"Not at all"           "To a small extent"    "Not at all"           "To a small extent"    "Not at all"           "To a small extent"   
"To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"   
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a moderate extent" "To a small extent"    "To a great extent"    "To a moderate extent" "To a small extent"   
"Not at all"           "To a great extent"    "To a moderate extent" "To a great extent"    "To a moderate extent" "To a great extent"   
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"To a great extent"    "To a great extent"    "To a great extent"    "NA"                   "NA"                   "NA"                  
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"   
"To a moderate extent" "To a great extent"    "To a moderate extent" "To a moderate extent" "To a moderate extent" "Not at all"          
"To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"   
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"Not at all"           "To a moderate extent" "To a small extent"    "To a moderate extent" "To a moderate extent" "Not at all"          
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"To a small extent"    "To a moderate extent" "To a moderate extent" "To a moderate extent" "To a moderate extent" "To a moderate extent"
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"To a moderate extent" "To a great extent"    "To a moderate extent" "To a great extent"    "To a moderate extent" "Not at all"          
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"To a moderate extent" "To a great extent"    "To a great extent"    "To a moderate extent" "To a moderate extent" "To a small extent"   
"To a small extent"    "To a small extent"    "To a small extent"    "To a small extent"    "To a small extent"    "To a small extent"   
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"To a great extent"    "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"To a moderate extent" "To a moderate extent" "To a moderate extent" "NA"                   "To a moderate extent" "To a small extent"   
"Not at all"           "Not at all"           "To a great extent"    "To a great extent"    "Not at all"           "To a small extent"   
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a great extent"    "Not at all"           "To a great extent"    "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a moderate extent" "To a moderate extent" "To a moderate extent" "To a moderate extent" "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"To a great extent"    "To a great extent"    "To a moderate extent" "To a great extent"    "To a great extent"    "To a moderate extent"
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a great extent"    "To a great extent"    "To a moderate extent" "To a moderate extent" "To a great extent"   
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"   
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "To a small extent"    "To a small extent"    "To a small extent"   
"To a great extent"    "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a moderate extent" "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a small extent"    "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a small extent"    "To a small extent"    "To a small extent"    "Not at all"           "Not at all"          
"To a small extent"    "To a moderate extent" "To a moderate extent" "To a moderate extent" "To a moderate extent" "To a small extent"   
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"To a moderate extent" "To a moderate extent" "To a moderate extent" "To a moderate extent" "To a moderate extent" "To a moderate extent"
"Not at all"           "To a small extent"    "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "To a small extent"    "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a small extent"    "Not at all"           "To a small extent"    "To a small extent"    "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"    "To a great extent"    "To a small extent"   
"Not at all"           "Not at all"           "To a small extent"    "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"To a small extent"    "To a small extent"    "To a moderate extent" "To a small extent"    "To a moderate extent" "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "To a small extent"    "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "To a small extent"    "Not at all"          
"To a moderate extent" "To a moderate extent" "To a moderate extent" "To a moderate extent" "To a moderate extent" "To a moderate extent"
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a small extent"    "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a small extent"    "To a small extent"    "Not at all"           "Not at all"           "Not at all"          
"NA"                   "NA"                   "NA"                   "NA"                   "NA"                   "NA"                  
"Not at all"           "Not at all"           "Not at all"           "To a moderate extent" "To a moderate extent" "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a moderate extent" "Not at all"           "Not at all"           "To a small extent"    "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "NA"                   "Not at all"          
"Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"           "Not at all"          
"Not at all"           "To a great extent"    "To a small extent"    "Not at all"           "Not at all"           "Not at all"          
end

Friday, July 29, 2022

Generate data set using cross tabulation cell frequency

Dear all,
Is it possible to generate data set include two variable using cross tabulation cell frequency
exposed unexposed Total
case 6 50 56
noncases 33 94 127
total 39 144 183

Thanks so much for any of your help

Regards
Suga

realise a(n)=a(n-1)*b(n) in stata

Dear all:

Sorry for bothering you. I have a problem when I use stata, could you give me some adive? So, I am using time series data, tr and afd are two variables, i is one of all the periods, and I want to replace the tr(i) by tr(i-1)*afd(i) in a loop for i from 1 to the whole periods, but I can't make it after several tries. The code is as follows.
Code:
 set trace on
local j =_N
forvalues i=1(1)`j'{
    local k = `i'-1
      replace tr = tr[`k']*afd[`i']
}
set trace off
Best wishes,
Xinkai Mao

How to extract selected features from LASSO in stata

Hi,

I have somewhat of a basic question. I am running the lasso linear command and wanted to use that to extract the selected variables and then use them for another estimation.

From what I can tell, I am interested in extracting e(post_sel_vars). However, since I am an R user, I don't know how to extract the vector of variables selected (please note that I am not interested in the coefficients but rather the variable names).

This is what I have right now (the parts highlighted in bold are wrong):

foreach var in $outcome_vars {

lasso linear `var' (i.report_month i.report_year i.tehsil_level i.ao) $ability_vars $network_vars $agsmart_indicators $interactions , selection(adaptive) cluster(end_tehsil) noconstant
//ereturn list

local selected_vars = e(post_sel_vars)
//lassocoef lassoadaptive, display(coef, standardized)


xtreg `var' subplus `selected_vars', i(ao) nonest fe
}

Any help would be greatly appreciated!

Thanks,
Bhavya

Help Extracting Year from Date

Hello everyone, I'm brand new to learning STATA, so I apologize if I am omitting any pertinent information. I have looked through previous posts on this topic, but am having trouble understanding how to code it.

I am attempting to extract the year from the date listed.

The data is currently listed in this format: testdate is 2012-03-12 00:00:00

I would like to simply create a new column with the year from the testdate column.
I really appreciate the help.
Thanks.
Lori

Logical operation using generate

I have a database with three variables: 27 states, 10 years and a GDP for each year. I want to generate a variable with the resulting logical operation: if year=2017, then GDP*2 & if year=2018, then GDP*7 & if year=2019, then GDP*15, and so on.
How can I do that, please?

Logic operations with generate

I have a database with three variables: 27 states, 10 years and a GDP for each year. I want to generate a variable with the resulting logical operation: if year=2017, then GDP*2 & if year=2018, then GDP*7 & if year=2019, then GDP*15, and so on.
How can I do that, please?

Thursday, July 28, 2022

Quade's test (non-parametric ANCOVA)

Hello Stata users
I wondered if you could inform me whether STATA performs the nonparametric ANCOVA test (i.e., Quade's test). I checked nonparametric tests (https://www.stata.com/features/nonparametric-methods/), but unfortunately, I could not find the Quade's test.
Thank you,

Identifying the first & last non-missing AdjClose (i.e. observation) by year for each group

I have 1200 companies that are identified by CIQ_IDs. I also have an id variable within each CIQ_ID, which are numbered 1 through approximately 507 (this is called each_co_id) and I have approximately 507 trading dates that correspond to the years 2020 & 2021. However, some AdjClose prices may be missing. Here is a sample of the data:

Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input str12 CIQ_ID int(each_co_id date) double AdjClose
"IQ100231"      1 21916         .
"IQ100231"      2 21917         .
"IQ100231"      3 21920  24.69812
"IQ100231"      4 21921  24.82523
"IQ100231"      5 21922  24.85066
"IQ100231"      6 21923  24.94599
"IQ100231"      7 21924  25.02226
"IQ100231"      8 21927  24.99006
"IQ100231"      9 21928  24.84192
"IQ100231"     10 21929   24.5392
"IQ100231"     11 21930  24.53276
"IQ100231"     12 21931  24.39107
"IQ100231"     13 21934  24.49412
"IQ100231"     14 21935  24.71954
"IQ100231"     15 21936  24.80972
"IQ100231"     16 21937  25.14463
"IQ100231"     17 21938  24.88056
"IQ100231"     18 21941  24.79683
"IQ100231"     19 21942  24.63582
"IQ100231"     20 21943  24.84836
"IQ100231"     21 21944  23.86293
"IQ100231"     22 21945  24.10767
"IQ100231"     23 21948  24.23005
"IQ100231"     24 21949  23.80496
"IQ100231"     25 21950  24.23649
"IQ100231"     26 21951  24.35242
"IQ100231"     27 21952  24.75819
"IQ100231"     28 21955  24.76463
"IQ100231"     29 21956  24.69378
"IQ100231"     30 21957  24.58429
"IQ100231"     31 21958  24.50056
"IQ100231"     32 21959  24.67446
"IQ100231"     33 21962  24.63582
"IQ100231"     34 21963  24.64226
"IQ100231"     35 21964  24.75819
"IQ100231"     36 21965  24.86768
"IQ100231"     37 21966  24.82904
"IQ100231"     38 21969    24.507
"IQ100231"     39 21970  24.05615
"IQ100231"     40 21971  23.89513
"IQ100231"     41 21972  23.01275
"IQ100231"     42 21973  22.68427
"IQ100231"     43 21976  23.94666
"IQ100231"     44 21977  23.37987
"IQ100231"     45 21978  24.59073
"IQ100231"     46 21979  23.94666
"IQ100231"     47 21980  23.85005
"IQ100231"     48 21983  22.34291
"IQ100231"     49 21984  23.09004
"IQ100231"     50 21985  22.23342
"IQ100231"     51 21986  20.17883
"IQ100231"     52 21987  22.20122
"IQ100231"     53 21990  20.48798
"IQ100231"     54 21991  21.73104
"IQ100231"     55 21992  21.15782
"IQ100231"     56 21993  20.06289
"IQ100231"     57 21994  18.32389
"IQ100231"     58 21997  17.24185
"IQ100231"     59 21998  18.09203
"IQ100231"     60 21999  18.29169
"IQ100231"     61 22000  19.70865
"IQ100231"     62 22001  19.21916
"IQ100231"     63 22004  19.47035
"IQ100231"     64 22005  18.77475
"IQ100231"     65 22006  18.06626
"IQ100231"     66 22007  18.52356
"IQ100231"     67 22008  17.68626
"IQ100231"     68 22011  18.96153
"IQ100231"     69 22012  19.37373
"IQ100231"     70 22013  19.59002
"IQ100231"     71 22014  20.14056
"IQ100231"     72 22019  19.78664
"IQ100231"     73 22020  20.40272
"IQ100231"     74 22021   19.7211
"IQ100231"     75 22022  19.76698
"IQ100231"     76 22025  20.46826
"IQ100231"     77 22026  20.30441
"IQ100231"     78 22027  19.57691
"IQ100231"     79 22028  19.31475
"IQ100231"     80 22029  19.33441
"IQ100231"     81 22032  19.47204
"IQ100231"     82 22033  20.01603
"IQ100231"     83 22034  20.08812
"IQ100231"     84 22035  20.59934
"IQ100231"     85 22036  19.97015
"IQ100231"     86 22039  19.59657
"IQ100231"     87 22040  19.39995
"IQ100231"     88 22041  19.49171
"IQ100231"     89 22042  18.86907
"IQ100231"     90 22046  18.93461
"IQ100231"     91 22047  19.52448
"IQ100231"     92 22048  19.25576
"IQ100231"     93 22049  18.93461
"IQ100231"     94 22050  18.41029
"IQ100231"     95 22053  18.73144
"IQ100231"     96 22054  18.55448
"IQ100231"     97 22055  19.30164
"IQ100231"     98 22056  18.98049
"IQ100231"     99 22057   19.3934
"IQ100231"    100 22061  19.51137
"IQ100231"    101 22062  19.58346
"IQ100231"    102 22063  20.19954
"IQ100231"    103 22064  20.87461
"IQ100231"    104 22067  20.35684
"IQ100231"    105 22068  20.22576
"IQ100231"    106 22069  20.27164
"IQ100231"    107 22070  20.31096
"IQ100231"    108 22071  20.65177
"IQ100231"    109 22074  20.84184
"IQ100231"    110 22075  21.47758
"IQ100231"    111 22076  21.77907
"IQ100231"    112 22077  21.37927
"IQ100231"    113 22078  21.06468
"IQ100231"    114 22081  19.77353
"IQ100231"    115 22082  19.98981
"IQ100231"    116 22083  19.98981
"IQ100231"    117 22084  20.17333
"IQ100231"    118 22085  19.83252
"IQ100231"    119 22088   19.8915
"IQ100231"    120 22089  19.86529
"IQ100231"    121 22090  19.73421
"IQ100231"    122 22091  19.82596
"IQ100231"    123 22092  19.28198
"IQ100231"    124 22095   19.4786
"IQ100231"    125 22096  19.05914
"IQ100231"    126 22097  19.60313
"IQ100231"    127 22098  19.81285
"IQ100231"    128 22099  19.59657
"IQ100231"    129 22102  19.71454
"IQ100231"    130 22103  19.98326
"IQ100231"    131 22104  19.87184
"IQ100231"    132 22105   19.9636
"IQ100231"    133 22106  19.69688
"IQ100231"    134 22109  20.09029
"IQ100231"    135 22110  19.84358
"IQ100231"    136 22111  19.97693
"IQ100231"    137 22112  20.01027
"IQ100231"    138 22113  20.26365
"IQ100231"    139 22116   20.1703
"IQ100231"    140 22117  19.90359
"IQ100231"    141 22118   20.1703
"IQ100231"    142 22119  20.11029
"IQ100231"    143 22120  19.93693
"IQ100231"    144 22123  19.71689
"IQ100231"    145 22124  19.53019
"IQ100231"    146 22125   19.7969
"IQ100231"    147 22126  19.71022
"IQ100231"    148 22127  19.71689
"IQ100231"    149 22130  19.72355
"IQ100231"    150 22131  19.75023
"IQ100231"    151 22132  20.01027
"IQ100231"    152 22133  19.90359
"IQ100231"    153 22134  19.89692
"IQ100231"    154 22137  20.01694
"IQ100231"    155 22138  20.13696
"IQ100231"    156 22139  20.13696
"IQ100231"    157 22140  20.12363
"IQ100231"    158 22141  19.94359
"IQ100231"    159 22144  20.01027
"IQ100231"    160 22145  19.90359
"IQ100231"    161 22146  19.86358
"IQ100231"    162 22147  19.82357
"IQ100231"    163 22148  19.78356
"IQ100231"    164 22151   19.7969
"IQ100231"    165 22152  20.02361
"IQ100231"    166 22153  19.93693
"IQ100231"    167 22154  19.99694
"IQ100231"    168 22155  19.93693
"IQ100231"    169 22159  20.03028
"IQ100231"    170 22160  19.87692
"IQ100231"    171 22161  19.65021
"IQ100231"    172 22162  19.80357
"IQ100231"    173 22165  19.73022
"IQ100231"    174 22166  19.61687
"IQ100231"    175 22167  19.67688
"IQ100231"    176 22168  19.58353
"IQ100231"    177 22169  19.27681
"IQ100231"    178 22172  19.33682
"IQ100231"    179 22173  19.43017
"IQ100231"    180 22174  19.41683
"IQ100231"    181 22175  19.49685
"IQ100231"    182 22176  19.38349
"IQ100231"    183 22179  19.29014
"IQ100231"    184 22180  19.09011
"IQ100231"    185 22181  19.01009
"IQ100231"    186 22182  18.58335
"IQ100231"    187 22183   18.6967
"IQ100231"    188 22186   18.6967
"IQ100231"    189 22187  18.92341
"IQ100231"    190 22188  18.87007
"IQ100231"    191 22189  19.01009
"IQ100231"    192 22190  18.99676
"IQ100231"    193 22193  19.12345
"IQ100231"    194 22194  19.11678
"IQ100231"    195 22195  19.14345
"IQ100231"    196 22196  19.20346
"IQ100231"    197 22197  19.42755
"IQ100231"    198 22200  19.23062
"IQ100231"    199 22201   19.1016
"IQ100231"    200 22202  18.84357
"IQ100231"    201 22203  18.66701
"IQ100231"    202 22204  18.63985
"IQ100231"    203 22207  18.55837
"IQ100231"    204 22208  18.25279
"IQ100231"    205 22209  18.17131
"IQ100231"    206 22210  18.14415
"IQ100231"    207 22211  19.20346
"IQ100231"    208 22214   18.8911
"IQ100231"    209 22215  18.59232
"IQ100231"    210 22216  18.27996
"IQ100231"    211 22217  17.99476
"IQ100231"    212 22218  18.17131
"IQ100231"    213 22221  18.34786
"IQ100231"    214 22222  18.56516
"IQ100231"    215 22223  18.64664
"IQ100231"    216 22224  18.36144
"IQ100231"    217 22225  18.68059
"IQ100231"    218 22228  18.63306
"IQ100231"    219 22229  19.21704
"IQ100231"    220 22230  19.59052
"IQ100231"    221 22231  19.50903
"IQ100231"    222 22232  19.31211
"IQ100231"    223 22235  19.63126
"IQ100231"    224 22236    19.672
"IQ100231"    225 22237  19.71275
"IQ100231"    226 22238  19.43434
"IQ100231"    227 22239  19.20346
"IQ100231"    228 22242  19.23062
"IQ100231"    229 22243  19.43434
"IQ100231"    230 22244  19.84856
"IQ100231"    231 22245  19.68558
"IQ100231"    232 22246  19.71275
"IQ100231"    233 22249  19.52261
"IQ100231"    234 22250   19.6041
"IQ100231"    235 22251  19.75349
"IQ100231"    236 22252  19.84856
"IQ100231"    237 22253  20.05906
"IQ100231"    238 22256  20.12696
"IQ100231"    239 22257  20.92145
"IQ100231"    240 22258  21.36283
"IQ100231"    241 22259  20.83996
"IQ100231"    242 22260  21.05726
"IQ100231"    243 22263   20.7449
"IQ100231"    244 22264  20.76527
"IQ100231"    245 22265  20.56835
"IQ100231"    246 22266  20.11338
"IQ100231"    247 22267  19.96399
"IQ100231"    248 22270  19.69917
"IQ100231"    249 22271  19.28495
"IQ100231"    250 22272  19.52261
"IQ100231"    251 22273  19.48187
"IQ100231"    252 22278   19.3868
"IQ100231"    253 22279  19.38001
"IQ100231"    254 22280  19.34606
"IQ100231"    255 22284   19.5294
"IQ100231"    256 22285  19.99116
"IQ100231"    257 22286  19.86893
"IQ100231"    258 22287  20.25598
"IQ100231"    259 22288  20.31031
"IQ100231"    260 22291  20.05461
"IQ100231"    261 22292  19.95096
"IQ100231"    262 22293  19.86803
"IQ100231"    263 22294  19.77128
"IQ100231"    264 22295   20.2412
"IQ100231"    265 22298  20.15827
"IQ100231"    266 22299  20.00624
"IQ100231"    267 22300  20.01315
"IQ100231"    268 22301  19.92331
"IQ100231"    269 22302  19.99242
"IQ100231"    270 22305  20.11681
"IQ100231"    271 22306  20.55909
"IQ100231"    272 22307  20.13754
"IQ100231"    273 22308  19.90258
"IQ100231"    274 22309   19.7851
"IQ100231"    275 22312  19.79892
"IQ100231"    276 22313  19.72291
"IQ100231"    277 22314  19.70217
"IQ100231"    278 22315  19.96478
"IQ100231"    279 22316  19.99242
"IQ100231"    280 22319  19.88185
"IQ100231"    281 22320  19.77819
"IQ100231"    282 22321  19.72982
"IQ100231"    283 22322  19.82656
"IQ100231"    284 22323  19.90258
"IQ100231"    285 22326  20.02006
"IQ100231"    286 22327   20.4347
"IQ100231"    287 22328  20.19974
"IQ100231"    288 22329  20.04079
"IQ100231"    289 22330  20.26193
"IQ100231"    290 22333  20.16518
"IQ100231"    291 22334   20.3034
"IQ100231"    292 22335   19.7851
"IQ100231"    293 22336  19.27371
"IQ100231"    294 22337  19.41193
"IQ100231"    295 22340  19.50177
"IQ100231"    296 22341   19.8473
"IQ100231"    297 22342  19.98551
"IQ100231"    298 22343  20.46925
"IQ100231"    299 22344  20.72494
"IQ100231"    300 22347  20.48307
"IQ100231"    301 22348  20.72494
"IQ100231"    302 22349  20.41397
"IQ100231"    303 22350  20.60055
"IQ100231"    304 22351  20.68348
"IQ100231"    305 22354  20.72494
"IQ100231"    306 22355  20.83551
"IQ100231"    307 22356  20.73877
"IQ100231"    308 22357    20.566
"IQ100231"    309 22358  20.72494
"IQ100231"    310 22361  20.73186
"IQ100231"    311 22362  20.72494
"IQ100231"    312 22363  20.78714
"IQ100231"    313 22364  20.94608
"IQ100231"    314 22365  21.11885
"IQ100231"    315 22368  21.23633
"IQ100231"    316 22369  20.91844
"IQ100231"    317 22370  21.05665
"IQ100231"    318 22371  21.22251
"IQ100231"    319 22376   21.4091
"IQ100231"    320 22377  21.37454
"IQ100231"    321 22378  21.08636
"IQ100231"    322 22379  21.11448
"IQ100231"    323 22382  21.05825
"IQ100231"    324 22383   20.8263
"IQ100231"    325 22384  20.89659
"IQ100231"    326 22385  20.91064
"IQ100231"    327 22386  21.05122
"IQ100231"    328 22389  21.07933
"IQ100231"    329 22390  21.00905
"IQ100231"    330 22391  21.16368
"IQ100231"    331 22392  22.04228
"IQ100231"    332 22393  22.07039
"IQ100231"    333 22396  21.72598
"IQ100231"    334 22397  21.62758
"IQ100231"    335 22398  21.76113
"IQ100231"    336 22399  22.02822
"IQ100231"    337 22400  22.07742
"IQ100231"    338 22404  22.14068
"IQ100231"    339 22405  22.50618
"IQ100231"    340 22406  22.51321
"IQ100231"    341 22407   22.7803
"IQ100231"    342 22410  22.60458
"IQ100231"    343 22411  22.93493
"IQ100231"    344 22412  22.67487
"IQ100231"    345 22413  22.47103
"IQ100231"    346 22414  22.63972
"IQ100231"    347 22417  22.66081
"IQ100231"    348 22418  22.04931
"IQ100231"    349 22419  20.77007
"IQ100231"    350 22420  20.35537
"IQ100231"    351 22421  20.83333
"IQ100231"    352 22424  21.09339
"IQ100231"    353 22425  20.91064
"IQ100231"    354 22426  20.74898
"IQ100231"    355 22427  20.77007
"IQ100231"    356 22428  20.88253
"IQ100231"    357 22432  20.68572
"IQ100231"    358 22433  20.60841
"IQ100231"    359 22434  20.81224
"IQ100231"    360 22435  20.54515
"IQ100231"    361 22438  20.57326
"IQ100231"    362 22439  20.43971
"IQ100231"    363 22440  20.36943
"IQ100231"    364 22441  20.39051
"IQ100231"    365 22442     20.51
"IQ100231"    366 22445  20.60841
"IQ100231"    367 22446  20.44674
"IQ100231"    368 22447  20.58029
"IQ100231"    369 22448  20.42566
"IQ100231"    370 22449  20.20074
"IQ100231"    371 22452  20.13748
"IQ100231"    372 22453  20.33428
"IQ100231"    373 22454  20.22885
"IQ100231"    374 22455  20.13748
"IQ100231"    375 22456  20.23588
"IQ100231"    376 22459  20.30617
"IQ100231"    377 22460  20.22885
"IQ100231"    378 22461  20.14451
"IQ100231"    379 22462  20.22885
"IQ100231"    380 22463   20.4608
"IQ100231"    381 22466  20.54515
"IQ100231"    382 22467  20.39754
"IQ100231"    383 22468  20.33428
"IQ100231"    384 22469  20.16966
"IQ100231"    385 22470  20.36291
"IQ100231"    386 22473  20.38438
"IQ100231"    387 22474  20.23408
"IQ100231"    388 22475  20.22692
"IQ100231"    389 22476   20.3486
"IQ100231"    390 22477  20.28418
"IQ100231"    391 22480  19.89052
"IQ100231"    392 22481  19.97641
"IQ100231"    393 22482  19.96925
"IQ100231"    394 22483  20.04798
"IQ100231"    395 22484  20.14819
"IQ100231"    396 22487  20.16966
"IQ100231"    397 22488  20.18398
"IQ100231"    398 22489  20.04798
"IQ100231"    399 22490  20.24839
"IQ100231"    400 22491  20.07661
"IQ100231"    401 22494  20.11956
"IQ100231"    402 22495  20.17682
"IQ100231"    403 22496  19.93347
"IQ100231"    404 22497  19.99788
"IQ100231"    405 22498   20.0122
"IQ100231"    406 22501  19.93347
"IQ100231"    407 22502  20.10524
"IQ100231"    408 22503  20.15535
"IQ100231"    409 22504  20.05514
"IQ100231"    410 22505  20.17682
"IQ100231"    411 22508  20.18398
"IQ100231"    412 22509  20.17682
"IQ100231"    413 22510  19.91915
"IQ100231"    414 22511  19.70443
"IQ100231"    415 22512  19.73306
"IQ100231"    416 22515  19.69727
"IQ100231"    417 22516  19.64717
"IQ100231"    418 22517  19.63285
"IQ100231"    419 22518   19.3394
"IQ100231"    420 22519  19.41813
"IQ100231"    421 22523   19.4897
"IQ100231"    422 22524   19.6257
"IQ100231"    423 22525  19.46107
"IQ100231"    424 22526  19.78316
"IQ100231"    425 22529   19.7259
"IQ100231"    426 22530   19.6257
"IQ100231"    427 22531  19.75453
"IQ100231"    428 22532   19.6257
"IQ100231"    429 22533  19.45392
"IQ100231"    430 22536  19.69727
"IQ100231"    431 22537  19.56128
"IQ100231"    432 22538  19.73306
"IQ100231"    433 22539  19.75453
"IQ100231"    434 22540  19.70443
"IQ100231"    435 22543  19.47539
"IQ100231"    436 22544  19.29645
"IQ100231"    437 22545  19.33224
"IQ100231"    438 22546  19.46107
"IQ100231"    439 22547  19.41813
"IQ100231"    440 22550  19.61138
"IQ100231"    441 22551  19.49686
"IQ100231"    442 22552  19.61138
"IQ100231"    443 22553  19.33224
"IQ100231"    444 22554   19.4396
"IQ100231"    445 22557  19.50402
"IQ100231"    446 22558  19.57559
"IQ100231"    447 22559  19.54696
"IQ100231"    448 22560  19.76586
"IQ100231"    449 22561  19.53237
"IQ100231"    450 22564  18.99244
"IQ100231"    451 22565  18.55466
"IQ100231"    452 22566  18.45981
"IQ100231"    453 22567  18.69329
"IQ100231"    454 22568  18.75166
"IQ100231"    455 22571  18.48169
"IQ100231"    456 22572   18.6714
"IQ100231"    457 22573  18.90488
"IQ100231"    458 22574  18.79544
"IQ100231"    459 22575  18.59844
"IQ100231"    460 22578  18.70788
"IQ100231"    461 22579  18.51088
"IQ100231"    462 22580  18.28469
"IQ100231"    463 22581  18.64221
"IQ100231"    464 22582  18.43062
"IQ100231"    465 22585  18.51088
"IQ100231"    466 22586  18.40873
"IQ100231"    467 22587  18.37225
"IQ100231"    468 22588  18.09499
"IQ100231"    469 22589  18.31388
"IQ100231"    470 22592  18.15336
"IQ100231"    471 22593  18.02932
"IQ100231"    472 22594  18.20443
"IQ100231"    473 22595  18.18254
"IQ100231"    474 22596  18.19714
"IQ100231"    475 22599  18.09499
"IQ100231"    476 22600  17.99284
"IQ100231"    477 22601  17.99284
"IQ100231"    478 22602  17.79584
"IQ100231"    479 22603  17.60613
"IQ100231"    480 22606  18.02202
"IQ100231"    481 22607   18.0658
"IQ100231"    482 22608  17.85421
"IQ100231"    483 22609   17.6718
"IQ100231"    484 22610  17.43102
"IQ100231"    485 22613   16.6576
"IQ100231"    486 22614  16.21982
"IQ100231"    487 22615  16.81812
"IQ100231"    488 22616  17.11727
"IQ100231"    489 22617  16.98594
"IQ100231"    490 22620  16.84001
"IQ100231"    491 22621  16.90568
"IQ100231"    492 22622  16.73786
"IQ100231"    493 22623   16.6649
"IQ100231"    494 22624  16.37305
"IQ100231"    495 22627   16.2636
"IQ100231"    496 22628  16.17604
"IQ100231"    497 22629  17.29968
"IQ100231"    498 22630  17.35076
"IQ100231"    499 22631  17.64991
"IQ100231"    500 22634  17.85421
"IQ100231"    501 22635  18.08039
"IQ100231"    502 22636  18.14606
"IQ100231"    503 22637  18.08039
"IQ100231"    504 22638  18.10958
"IQ100231"    505 22643  17.97825
"IQ100231"    506 22644  18.08039
"IQ100231"    507 22645  17.94906
"IQ10081196"    1 21916         .
"IQ10081196"    2 21917         .
"IQ10081196"    3 21920 135.54602
"IQ10081196"    4 21921 134.69341
"IQ10081196"    5 21922 135.98202
"IQ10081196"    6 21923 133.88924
"IQ10081196"    7 21924 135.85607
"IQ10081196"    8 21927 138.47204
"IQ10081196"    9 21928 137.52254
"IQ10081196"   10 21929 138.28795


end
format %td date

I want to identify the following and use it in generating other variables :
1. The last available i.e. non-missing AdjClose price for 2020
2. The first available i.e. non-missing AdjClose price for 2020

3. The last available i.e. non-missing AdjClose price for 2021
4. The first available i.e. non-missing AdjClose price for 2021

I would be very grateful for some help.

How to collate multiple, individual regression outputs

Looking for some help on the following:

I want to tabulate the univariate association of 8 variables (v1-v8) with an outcome (outcome).
For each variable I would write, for instance:
Code:
logistic outcome v1, coef
I want the outputs that are created to be added as new rows to create a table where the first column has all the variables and their subcategories (some are i.variables), and the columns show the regression outputs as they would when I write the above code for each variable individually.

Many thanks in advance

How to Mark the Demoted Students Out in Long Format Data in Stata?

Hello, Folks,
I have a small dataset like this,
clear
input byte (id year gr kg5 k68 k912)
1 1 0 0 0 0
1 2 1 0 0 0
1 3 2 0 0 0
1 4 3 0 0 0
1 5 4 0 0 0
1 6 5 0 0 0
1 7 6 0 0 0
1 8 7 0 0 0
1 9 8 0 0 0
1 10 9 0 0 0
1 11 . 0 0 0
1 12 9 0 0 0
1 13 10 0 0 0
2 1 0 0 0 0
2 2 1 0 0 0
2 3 2 0 0 0
2 4 3 0 0 0
2 5 4 0 0 0
2 6 5 0 0 0
2 7 6 0 0 0
2 8 7 0 0 0
2 9 8 0 0 0
2 10 9 0 0 0
2 11 10 0 0 0
2 12 . 0 0 0
2 13 9 0 0 0
3 1 0 0 0 0
3 2 . 0 0 0
3 3 . 0 0 0
3 4 . 0 0 0
3 5 . 0 0 0
3 6 . 0 0 0
3 7 . 0 0 0
3 8 . 0 0 0
3 9 . 0 0 0
3 10 9 0 0 0
3 11 . 0 0 0
3 12 . 0 0 0
3 13 9 0 0 0
4 1 0 0 0 0
4 2 . 0 0 0
4 3 . 0 0 0
4 4 . 0 0 0
4 5 . 0 0 0
4 6 . 0 0 0
4 7 . 0 0 0
4 8 . 0 0 0
4 9 8 0 0 0
4 10 . 0 0 0
4 11 10 0 0 0
4 12 9 0 0 0
4 13 10 0 0 0
5 1 0 0 0 0
5 2 1 0 0 0
5 3 2 0 0 0
5 4 3 0 0 0
5 5 4 0 0 0
5 6 . 0 0 0
5 7 4 0 0 0
6 1 0 0 0 0
6 2 1 0 0 0
6 3 2 0 0 0
6 4 3 0 0 0
6 5 4 0 0 0
6 6 5 0 0 0
6 7 6 0 0 0
6 8 8 0 0 0
6 9 . 0 0 0
6 10 7 0 0 0
end

please run the Stata code below to remove extra variables in the data set,
drop k*

The student with ID==6 is a demoted student, as can be seen from the data, the student went back to 7th grade after finishing 8th grade.
The student with ID=3/5 is just a regular student who repeated grades.
As for the students with ID=2/4, the student is both a repeated student and a demoted student.
All scenarios above were regarded as "REPEATED".
What I want is to correctly use a binary variable "REPEATED" indicating if a student repeated grades in a specific grade (REPEATED==1; REPEATED==0; if gr is missing value, then REPEATED==missing value).
Thanks for your Stata code!

Misleading median?

Hi everyone!

I'm doing research on how real estate values impact corporate savings. I have computed all the variables however I noticed in my summary statistics table that there was an odd value for the variable RE value/PPE (msa). As you can see almost all data of the RE value/ppe (msa) matches with the variable RE value/ppe (state), however when looking at the median it differs a lot 0.03 vs 0.19. Can this be caused by the difference in the number of observations or is the difference to little to cause this?

Kind regards,
Rick


First difference of a squared term

Dear all,

I am working with a panel dataset and am trying to run a non-linear (quadratic) regression using the first difference operator (D.). Specifically, my regressors should be the first difference of the linear and quadratic independent variable. My issue is independent of the specific data used, therefore I just name Y my dependent and X my independent variables. The code would be:

gen X_sqr = X^2

reg d.Y d.X d.X_sqr

However, I need to use the command margins and marginsplot to plot how the marginal effect of X on Y varies at different levels of X. Therefore, I though about the following code:

reg d.Y d.(c.X##c.X)

Unfortunately, the output is not the first difference of the square but it is the square of the first difference. The same as if I typed the following:

reg d.Y d.c.X##d.c.X

I have tried to find a solution but the only relevant page I have found is the following:

https://www.statalist.org/forums/for...ferent-results

Where Clyde Schechter pointed out that this relationship is "not expressible with factor-variable notation". However, since this post is from 2014, I am asking again in case something has changed in the last 8 years. I know the marginal effect can be computed manually, but it is worth checking whether margins could be applicable in this context.

I have read the FAQ and tried to be as precise as possible. Apologies for any mistakes.

Thank you in advance to whoever will read this post.

Store significant variables in Logistic Regression

Hi,

I am running a logistic regression with about 30 variables, most of which are categorical variables, and I would like to run a command afterwards that creates a list containing all the significant variables (say at the 5% level) to run some further analysis on those specific variables.

Do you have any idea on how to do this ?

Thanks,
Diego

Wednesday, July 27, 2022

need to count experiences of employee with respect to year

Hi experts
I want to count the experiences of employees (i.e., personalID), with respect to years such as if they worked in 2001 with company 4 as well as with company 8 so the wanted should be 1 no 2. Sample data is as follows
Thanks and Regards

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(id year personalID wanted)
4 2001 1001 1
8 2001 1001 1
4 2003 1001 2
8 2003 1001 2
8 2004 1001 3
4 2004 1001 3
4 2001 1002 1
8 2002 1002 2
4 2002 1002 2
8 2003 1002 3
4 2004 1002 4
8 2001 1003 1
4 2002 1003 2
4 2003 1003 3
8 2004 1003 4
8 2002 1004 1
end

estout: what is the stats label for LR chi2 in logistic regression

I run a logistic regression and would like to print out LR Chi2 statistics using
Code:
estout
Code:
eststo: quietly logit y x1 x2
Code:
esttab, se stats(?)
I use quietly because the regression actually has a lot of dummy variables which I do not want to print them out. What would be stats label for LR Chi2 in the logistic regression?

intercation term same value as main variable

Dear all,

Since I think it is more appropriate to ask this question in a different topic, I created a new topic.
I was wondering what it means if my interaction term has the same value as one of my variables of which it is interacted with, but has the opposite sign.

I am estimating the effect of a policy (that can exist in two forms/binary variable) on district revenue. Since not only the policy, but also the internsity matters (irrespective of the policy, the intensity is always positive), I included an interaction term. However it seems like the value of the interaction term is almost equal to the policy variable if policy==1. If the policy takes on the value of 1 because the first policy is in place, it then seems like there is no effect of intensity correct? I was wondering if this indicates some mistake.

Code:
 xtreg lnDistrict_Revenue L.i.P##L.c.Intensity c.L.lnUrbanPopulation##c.L.lnUrbanPopulation c.L.lnPropertyvalue c.L.lnGrant##c.L.lnGrant c.L.lnIncome_percapita c.L.ShareUnemployed c.L.ShareElderly c.L.ShareYoung L.lnSpending i.Year, fe cluster(District)
 
Fixed-effects (within) regression               Number of obs     =      3,204
Group variable: District                        Number of groups  =        298
 
R-sq:                                           Obs per group:
     within  = 0.7285                                         min =          6
     between = 0.0179                                         avg =       10.8
     overall = 0.0325                                         max =         11
 
                                                F(23,297)         =     172.78
corr(u_i, Xb)  = -0.9326                        Prob > F          =     0.0000
 
                                                          (Std. Err. adjusted for 298 clusters in District)
-----------------------------------------------------------------------------------------------------------
                                          |               Robust
                       lnDistrict_Revenue |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
------------------------------------------+----------------------------------------------------------------
                                      L.P |
                                       1  |   .0351146   .0151471     2.32   0.021     .0053053    .0649238
                                          |
                                Intensity |
                                      L1. |     .19091   .0975182     1.96   0.051    -.0010041    .3828242
                                          |
                         L.P#cL.Intensity |
                                       1  |  -.1870479   .1081467    -1.73   0.085    -.3998788     .025783
                                          |
                        lnUrbanPopulation |
                                      L1. |   2.370568   1.676327     1.41   0.158    -.9284165    5.669553
                                          |
cL.lnUrbanPopulation#cL.lnUrbanPopulation |  -.1596966   .0787233    -2.03   0.043    -.3146229   -.0047704
                                          |
                          lnPropertyvalue |
                                      L1. |  -.7620712   .0773144    -9.86   0.000    -.9142247   -.6099178
                                          |
                                  lnGrant |
                                      L1. |   .9593062   .2999702     3.20   0.002     .3689699    1.549642
                                          |
                    cL.lnGrant#cL.lnGrant |  -.0228989   .0081084    -2.82   0.005    -.0388561   -.0069416
                                          |
                       lnIncome_percapita |
                                      L1. |   .0228362   .1156852     0.20   0.844    -.2048304    .2505027
                                          |
                          ShareUnemployed |
                                      L1. |  -.0163021   .0095847    -1.70   0.090    -.0351648    .0025605
                                          |
                             ShareElderly |
                                      L1. |  -.0026668    .002733    -0.98   0.330    -.0080453    .0027117
                                          |
                               ShareYoung |
                                      L1. |  -.0039125    .003059    -1.28   0.202    -.0099326    .0021076
                                          |
                               lnSpending |
                                      L1. |  -.0034049   .0030913    -1.10   0.272    -.0094886    .0026787
                                          |
                                     Year |
                                    2002  |   .0454305   .0154763     2.94   0.004     .0149733    .0758876
                                    2003  |    .110855   .0188005     5.90   0.000      .073856     .147854
                                    2004  |   .1813692   .0234291     7.74   0.000     .1352611    .2274773
                                    2005  |   .1985591   .0317878     6.25   0.000     .1360012     .261117
                                    2006  |   .1323212   .0377318     3.51   0.001     .0580657    .2065767
                                    2007  |   .1298228    .042398     3.06   0.002     .0463842    .2132615
                                    2008  |   .1263483   .0453511     2.79   0.006     .0370981    .2155986
                                    2009  |   .1168673   .0481765     2.43   0.016     .0220568    .2116778
                                    2010  |   .1327302   .0562972     2.36   0.019     .0219383    .2435221
                                    2011  |   .1738778   .0612029     2.84   0.005     .0534314    .2943241
                                          |
                                    _cons |  -5.173469   8.324948    -0.62   0.535    -21.55683    11.20989
------------------------------------------+----------------------------------------------------------------
                                  sigma_u |  .66077134
                                  sigma_e |  .06458734
                                      rho |  .99053626   (fraction of variance due to u_i)
---------------------------------------------------------------------------------------------------

When I split the sample based on policy (0 or 1) instead of the interaction term, It seems like there is no effect of intensity when policy 1 as compared when policy is 0. I know this is different because I am the interacting every variable.


Bootstrap local projections impulse responses/historical decompositions

Dear Statalisters,

I am trying to bootstrap the impulse response function that I obtain from a local projection. The tricky bit is that the object I want to bootstrap is a vector. This is the code that I have so far:

Code:
clear

set obs 200

set seed 12

gen xx=runiform()

gen e=rnormal(0,1)

gen lindpro=0.5*x+e

g time =_n
tsset time

local nlags 3
local irflag = 36
capture program drop lpirf
program lpirf, eclass
    args xx lindpro nlags irflag
    
    * compute IRFs
    local irflagp1 = `irflag'+1
    mat bbb = J(`irflagp1',1,0)

    forvalues h = 0/`irflag' {
        local hp1 = `h'+1
        qui reg F`h'.xx L(0/`nlags').xx L(1/`nlags').lindpro, r
        mat bbb[`hp1',1] = _b[xx]
    }
  
    tempname bb
    matrix `bb'=bbb'
    ereturn clear
    ereturn post `bb'
    ereturn local cmd="bootstrap"

end

lpirf xx lindpro `nlags' `irflag'

matrix list e(b)

bootstrap _b, reps(100) nowarn nodrop: lpirf xx lindpro `nlags' `irflag'
While the code successfully produces the impulse response, unfortunately, it does not work with the bootstrap. I get the error message "insufficient observations to compute bootstrap standard errors no results will be saved".

Now, I could easily compute the standard errors of the impulse responses in a different way, however, ultimately I want to bootstrap the historical decomposition, which is yet again a function of the impulse responses and the data:

Code:
cap drop hdlindpro
gen hdlindpro = 0
forvalues h = 1/`irflag' {
        qui replace hdlindpro = hdlindpro + bbb[`h',1]*L`h'.xx
}
mkmat hdlindpro if hdlindpro!= . , matrix(histdec)
Therefore, I think I have to do it this way. Does anyone have an idea what the problem could be? I tried to include the nodrop option but with no avail.

Many thanks in advance and all the best,

Diego

Multinomial Logit and estout

I am using estout (SSC) on Stata 16.1 to tabulate the results of a multinomial logit regression. I would like to label each column with the categories that are being compared in it (i.e., Base vs. Category N). I am running the following code (as a toy example):

Code:
webuse auto
qui mlogit rep78 weight length turn displacement, base(1)
est store mlogit
estout mlogit, stats(r2_p N, labels("Psuedo R-Squared" "N")) cells(b(star fmt(4)) se(par fmt(4))) label unstack collabels("1v1" "1v2" "1v3" "1v4" "1v5")
Results:
Code:
----------------------------------------------------------------------------------------------------
                           mlogit                                                                  
                                1               2               3               4               5  
                              1v1             1v1             1v1             1v1             1v1  
----------------------------------------------------------------------------------------------------
Weight (lbs.)              0.0000         -0.0063         -0.0016         -0.0033         -0.0018  
                              (.)        (0.0048)        (0.0038)        (0.0041)        (0.0049)  
Length (in.)               0.0000          0.0850          0.0402          0.1140          0.1837  
                              (.)        (0.1291)        (0.1060)        (0.1123)        (0.1291)  
Turn Circle (ft.)          0.0000          0.1876         -0.3254         -0.5321         -0.6891  
                              (.)        (0.4778)        (0.4457)        (0.4557)        (0.4838)  
Displacement .. in.)       0.0000          0.0326          0.0216          0.0194         -0.0406  
                              (.)        (0.0298)        (0.0274)        (0.0281)        (0.0415)  
_cons                      0.0000         -9.9248          8.9183          8.2266          5.5910  
                              (.)       (14.1133)       (11.7331)       (12.3532)       (13.9921)  
----------------------------------------------------------------------------------------------------
Psuedo R-Squared           0.2159                                                                  
N                         69.0000                                                                  
----------------------------------------------------------------------------------------------------
I am following the syntax suggested on the estout website to specify the column labels individually, but when I do the column label repeats itself. Is there any way to work around this?

Additionally, is there a way to avoid reporting the blank column for the reference category (first column in the example)?

Thanks!

Loading user table to ICIO for GVC analyses

Hi,

I am using the long-run WIOD table for GVC analysis using the STATA command icio. One needs to load the IO table using the icio_load command in order to run icio.

Using icio_load works fine when loading tables that do not need to manually imported (WIODN, WIODO, Eora, etc.). I am facing an issue calculating the accounting identities when using user provided tables. I have followed the guidelines and have inputted a .csv file with the appropriate dimensions according to the help documentation (598 rows and 702 columns for a given year, since there are 26 countries, 23 sectors, and 4 number of uses. I have removed the country and sector variables, such that the .csv file is only populated with values.). Using the command below, I am able to load the table.
Code:
icio_load, iciot(user, userp("C:/Downloads") tablen(WIOT2000_LR.csv) countryl(countrylist2000.csv))
Running the icio command below, however, I get a conformability error.
Code:
icio, exporter(usa, 2)

Decomposition of gross exports:
Table: user provided
Perspective: exporter
Approach: source
Exporter: USA
Importer: total USA exports
Return: detailed 
Sector of export: 2
  _icio_total_sector_e():  3200  conformability error
            _icio_main():     -  function returned error
                 <istmt>:     -  function returned error
r(3200);
I am unsure where the issue stems from. I have looked into the documentation of the command, but I haven't found useful info. If anyone has had experience with this, some help would be greatly appreciated. Thank you in advance!

Best,
Ramesh

Calling in SQL through ODBC--String variables & choosing data

Hello Statalist ,

I am using Stata 17 on Windows. I am new to the ODBC process and I am trying to call in a large dataset from SQL. The dataset is mostly string variables, including the year variable. I have been successful with smaller datasets--they were able to load fairly quickly and I could manipulate the variables.
The code that works is:

Code:
odbc load, table("MySmallTable")
I want choose what data is loaded based on year to make it more manageable. The code I am trying (without luck) is:

Code:
odbc load if year=="2015", table("MyTable) dsn("DataSource")
Is there a way to:

a) destring all the variables prior to loading to make the data more manageable
b) a way to destring just one variable (in this case year) so that I could try if year>2015
c) a way to indicate which specific values of year I want. for example if year=="2015" & "2016" & "2017"

Thank you so much in advance!
I am not able to share screenshots or the data due to the confidential nature of the data (accessed through a secure lab).

Tuesday, July 26, 2022

Does anyone know a clever way to add R2 to "graph twoway lfit"?

I am using the below code to fit a linear regression line, Private_Hospital is a binary variable

graph twoway lfit Private_Hospital_1 Differential_Distance

I would like to show the R2 on the graph if possible, does anyone know how to do this?

i have looked at the help file and no luck so far

Return how many variables were overwritten (because they had the same name) in a merge?

If I merge two datasets and two variables in the datasets had the same name, how can I get Stata to tell me how many variables (and which) were overwritten?

Ordering Multiples variables

I am having a dataset in wide format for almost 80 years for 100 countries for about 200 variables. Variables bring the 3-letter ISO country code as their first component. I would need to reorder the variables in two different ways, with probably two loops.



The first is to order everything, grouping by each variable separately. In the example, below, that will be like:
Code:
 
  ts FRA_u DEU_u GBR_u USA_u FRA_size  DEU_size  GBR_size USA_size FRA_Δprdm DEU_Δprdm GBR_Δprdm USA_Δprdm

And the second to order everything by country code, similar to :

Code:
ts FRA_u FRA_size FRA_u FRA_Δprdm DEU_u  DEU_size DEU_Δprdm GBR_u GBR_size GBR_Δprdm  USA_u USA_size USA_Δprdm
I will need now two different reordering loops in order to produce my final results.
For more than 100 countries, a reordering for each one is time-consuming



The following is an example sample created just for this purpose. Variables do follow a random order, and as said above, there are more than 200 for more than 100 countries

I have been greatly benefited from this forum every time I posted a question and cannot thank enough!

Best,
Mario


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(ts DEU_u DEU_size DEU_Δprdm FRA_u FRA_size FRA_Δprdm GBR_u GBR_size GBR_Δprdm USA_u USA_size USA_Δprdm)
2000  7.92 19.044518         0 10.22  22.32587  .08333331 5.56 16.809107        0 3.99  14.02601         0
2001  7.77 18.948927         0  8.61 22.141853   .1547619  4.7 17.468409        0 4.73 14.528614         0
2002  8.48  19.23157 .08333331   8.7  22.72115          0 5.04 18.273579        0 5.78 15.041862         0
2003  9.78  19.32564         0  8.31 23.143427          0 4.81  18.95393 .0714286 5.99 15.246542         0
2004 10.73  18.81738         0  8.91 23.044697          0 4.59 19.572525        0 5.53  15.16888         0
2005 11.17  18.77591 .08333337  8.49  23.06704  .23809522 4.75 19.789913        0 5.08 15.055298 .08333331
2006 10.25  18.32777         0  8.45  22.75918  .08333337 5.35 19.919577        0 4.62  15.00832         0
2007  8.66 17.855055 .08333331  7.66  22.43314 -.14285713 5.26  19.74264        0 4.62 15.214797         0
2008  7.52  18.25556         0  7.06 22.562563          0 5.62 20.502636        0 5.78  15.98871         0
2009  7.74   19.9917         0  8.74 24.080805  .14285713 7.54   22.0684        0 9.25 16.822023         0
2010  6.97  19.56321         0  8.87 23.989256          0 7.79 21.641914        0 9.63 16.743158         0
end

Help with selecting type of ANOVA

Hello statalisters,

I am trying to perform an analysis on a dataset that contains information on patient values (r2-r4) from 4 different time points (denoted by the variable fu). These r variables each identify a different body part and the values are electrical signal. Patients are denoted by var recordid. I am trying to perform ANOVA to compare the means between two groups of patients (identified by variable innervation) of a body part over the period of time. I am aware that I should be using the repeated measure ANOVA but am not quite sure how to incorporate comparing the two groups of innervated vs non-innervated patients. Is this supposed to be nested? I have read the ANOVA help file but am not entirely sure based on the examples.

Any help is greatly appreciated.



Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input int recordid byte innervation float(r2 r3 r4) str5 j float fu
29 1 26.6 37.7 26.5 "_12mo" 4
29 1 96.5  100 42.2 "_6mo"  3
29 1  100  100 74.3 "_3mo"  2
30 0 36.3  100  3.9 "_3mo"  2
31 1 64.2 93.3 17.4 "_6wk"  1
31 1 12.7 75.4 20.6 "_3mo"  2
32 0 95.8 98.2 61.2 "_6wk"  1
33 0 75.8 99.9 98.7 "_6wk"  1
33 0  100  100   84 "_3mo"  2
33 0  100  100  100 "_12mo" 4
end





Calculation of Blau Index / Simpson Index

Hello everyone!

I am trying to analyze the degree of national and gender diversity of TMTs of german companies over a period of 10 years. The Blau Index seems to be the ideal measurement, but after trying out different possibilities, I could not find the right solution.

The formula for the Blau Index is the following:

Array

the data set:


Company_ID year gender nationality BlauGender BlauNationality
1 2010 male German
2010 female German
2011 male Austrian
2011 female German
2 2013 male Australian
2013 male German
2013 male Dutch
2014 female Dutch
2014 female Dutch
The data has to be aggregated on a company-year-level. The results of gender diversity and national diversity for every company and every year should be shown in the columns "BlauGender" and "BlauNationality".

I would appreciate any tips or suggestions on how to solve this problem. Thank you very much!

expand data

Dear Stata users,

I have a panel database of countries observed on a yearly basis from 2004 to 2018, I want to expand the observations to 2019!

I would be grateful if you could help me. Thanks

Monday, July 25, 2022

treatment turns on and off

Hello everyone, I hope you are very well,

I have some monthly panel data at the state level, in this database I have a type of treatment that turns on and off over time, that is, it is intermittent and I would like to know how I can evaluate the impact with a treatment of this type since I do not know how to make the estimate.

Thank you!

How to convert my FE-IV model (with intuitions borrowed from the RDD literature) into STATA language - Is IVREGHDFE the one?

Hello dear stata friends!
I am having some confusion regarding the implementation of my Individual-Fixed Effects IV Design in Stata. This might be a bit long-winded, but I really want to make myself clear on what I am trying to achieve, and where I am having issues/confusions. Thanks for your understanding!
Essentially, I am trying to estimate the short- mid- and long-term effects of X(retiring) on Y, using Z(reaching pension eligibility age) as an instrument for X.

Explanation:

Short- and long-term effects:
Short-term effects: The impact of retiring (X) between wave 1 and 2 on the changes in Y between wave 1 and 2
Mid-term effects: The impact of retiring (X) between wave 1 and 2 on the changes in Y between wave 2 and 3
Long-term effects: The Impact of retiring (X) between wave 1 and 2 on the changes in Y between wave 2 and 4

This is what I have done so far:
My data contains 4 waves of surveys.
  1. I have kept only those who are present in all survey waves: 61 084 observations in total i.e. 15 271 individuals per wave.
  2. I have created a treatment group which consists of people who reach/cross their state pension eligibility age between wave 1 and 2. The control group thus = individuals who do not reach pension eligibility age between wave 1 and 2.
  3. To allow the impact of age to differ on both sides of the eligiblity threshold used as instrument, I have centered age and its polynimials by substeacting the state pension age from the individuals age.
The relevant variables are named:
Outcome variable = Y
Retirement = Causal variable of interest = X
Reaching pension eligibility age = Instrument = Z
Centered age = CA
Centered age squared = CA2
Person identifier (string) = mergeid
Person identifier (de-stringed) = id
wave variable = wave
Country = country
Month of interview = int_month
Year of interview = int_year

I would now want to run my FE-IV model in a 2SLS estimation:

Long-term effects:
The Impact of retiring (X) between wave 1 and 2 on the changes in Y between wave 2 and 4
First-stage:
X𝑖𝑡
−2 = 𝛼 + 𝛽1Z𝑖𝑡−2 + 𝛽2CA𝑖𝑡−2 + 𝛽3CA𝑖𝑡−22 + 𝛽4Z𝑖𝑡−2(CA𝑖𝑡−2) +𝛽5Z𝑖𝑡−2(CA𝑖𝑡−22) + 𝛾𝑐(CA𝑖𝑡−2) + 𝛾𝑐(CA𝑖𝑡−22) + 𝛾𝑐[ Z𝑖𝑡−2(CA𝑖𝑡−2) ] + 𝛾𝑐[ Z𝑖𝑡−2(CA𝑖𝑡−22) ] + 𝛿𝑖 + 𝜇𝑡 + 𝜚𝑡 + 𝜀𝑖𝑡
Second-stage:
Y𝑖𝑡 = 𝛼 + 𝛽1X_hat𝑖𝑡
−2 + 𝛽2CA𝑖𝑡−2 + 𝛽3CA𝑖𝑡−22 + 𝛽4Z𝑖𝑡−2(CA𝑖𝑡−2) +𝛽5Z𝑖𝑡−2(CA𝑖𝑡−22) + 𝛾𝑐(CA𝑖𝑡−2) + 𝛾𝑐(CA𝑖𝑡−22) + 𝛾𝑐[ Z𝑖𝑡−2(CA𝑖𝑡−2) ] + 𝛾𝑐[ Z𝑖𝑡−2(CA𝑖𝑡−22) ] + 𝛿𝑖 + 𝜇𝑡 + 𝜚𝑡 + 𝜀𝑖𝑡
Where X_hat𝑖𝑡−2 is the predicted values of X𝑖𝑡−2 from the first stage with Z𝑖𝑡−2 as the excluded instrument. CA𝑖𝑡−2and CA𝑖𝑡−22 denote centered age and its square respectively. 𝛾𝑐 denote country dummies.𝛿𝑖 represents individual-fixed effects. 𝜇𝑡 and 𝜚𝑡 represent separate year- and month fixed effects respectively.

In the model analysing the mid-term effects of X, I estimate the equations above but with all variables measured at t-1 instead of t-2.
In the model analysing the short-term effects of X, I estimate the equations above but with all variables measured at t instead of t-2

FINALLY - THE MAIN DILEMMA:
Since I am fairly new to Stata, I am not sure on how I should approach this in Stata language. I have dealt with basic fixed-effects models and basic IV models separately, however I hade never before combined them.
I have tried using the ivregress command:
Code:
ivregress estimator depvar [varlist1] (varlist2 = varlist_iv)
by trying to add the fe option.
I have also tried using the xtivreg command with the fe option:
Code:
xtivreg depvar [varlist1] (varlist2 =varlistiv), fe
However, I have not managed to obtain what I am explaining above by using any of these command.

By doing some research, I have read that various people who use multiple fixed effects in their models use the ivreghdfe command I suspect that this might be the one I am looking for. After numerous attemps, however, I cannot seem to figure it out. Therefore, I would greately appreciate it if any of you kind people who have the relevant experience could help me with this. If I forgot to provide some important information above, please let me know!

Many thanks in advance!

Best regards, Guri Gray