How can I run a ttest on the CEM matched data? - stata

I have run a CEM matching process on my data using Stata, and now I would like to know how to run a t-test on the variables of the matched data.
/* Simple example of my code; first I run the CEM */
cem age gender education, treatment(treat)
/* Then I want to have a look at the summary statistics of the entire population and the matched data (this code works fine) */
summarize age gender education
summarize age gender education [iweight=cem_weights]
/* But if I want to do a t test only on the matched data, I get an error with the weights */
ttest age, by(treat) /* works fine */
ttest age [iweight=cem_weights], by(treat) /* error saying that weights are not allowed */
How can I run t tests only on the matched data? An option could also be to export the matched data, so how could I do that?

Something like this should do it:
sysuse auto, clear
cem mpg weight rep78, treatment(foreign)
mean price [iweight = cem_weight], over(foreign) //coeflegend
lincom _b[c.price#1.foreign] - _b[c.price#0.foreign]
Personally, I would just use regress with het-robust standard errors (which gets similar results here, and will get even closer with a bigger sample):
regress price i.foreign [iweight = cem_weight], robust

Related

Stata creating output for IV regression with bysort

So I am running an 2SLS model by interview year and I have many interview years and different models. I want to present the first-stage results first and then after reassuring the reader that they are solid move on to the interesting results.
Example of Table A (first stage):
Year DV Coef SE F N
1 A 0.5 0.1 100 1000
2 A 0.8 0.2 10 1500
3 B -0.6 0.4 800 800
Table B with the main results would look the same just without the F-Stat.
I searched on the web about how to create those tables automatically in Stata, but despite finding many questions I didn't find an answer that worked for me. From those different posts and help-files I build something that is nearly there.
It creates the table I want for the main results with the F-Stat together by some variable (Step A in the code). However, when I move on to do the same for the first stage it only saves the last wave as I restore the estimates. I understand why Stata does it like that, but I cannot think of a way of convincing it to do what I want.
clear all
*Install user-written commands
ssc install outreg2, replace
ssc install ivreg210, replace
*load data
sysuse auto, clear
*run example model (obviously the model itself is bogus)
********************************************************
*Step A: creates the IV results by foreign plus the F-Statistic
bys foreign: ///
outreg2 using output1-IV-F, label excel stats(coef se) dec(2) adds(F-Test, e(widstat)) nocons nor2 keep(mpg) replace: ///
ivreg210 price headroom trunk (mpg=rep78 ), savefirst first
*Step B: creates the first stage results in a seperate table
bys foreign: ///
ivreg210 price headroom trunk (mpg=rep78 ), savefirst first
est restore _ivreg210_mpg
outreg2 using output1_1st-stage, replace keep(rep78)
cap erase output1-IV-F
cap erase output1_1st-stage
So ideally I would only run the model once and have the F-Stat in the first-stage table, but I can fix that manually. The biggest issue I have is how to store the estimates when using bysort. If anyone has any suggestions about that, I would greatly appreciate it.
Thanks!
ssc install estout
then you can store whatever result you want for later use, even after a bysort.
eststo clear
sysuse auto, clear
bysort foreign: eststo: reg price weight mpg
esttab, label nodepvar nonumber
This is a round-about solution. It works, but really isn't the proper solution I was/am looking for. The "trick" is to run the 1st stage as a separate model.
clear all
*Install user-written commands
ssc install outreg2, replace
ssc install ivreg210, replace
*load data
sysuse auto, clear
*run example model (obviously the model itself is bogus)
********************************************************
*Step A: creates the IV results by foreign plus the F-Statistic
bys foreign: ///
outreg2 using output1-IV-F, label excel stats(coef se) dec(2) adds(F-Test, e(widstat)) nocons nor2 keep(mpg) replace: ///
ivreg210 price headroom trunk (mpg=rep78 ), savefirst first
*Step B: creates the first stage results in a seperate table
bys foreign: ///
ivreg210 price headroom trunk (mpg=rep78 ), savefirst first
est restore _ivreg210_mpg
outreg2 using output1_1st-stage1, replace keep(rep78)
*************
/* NEW BIT */
*************
*Step C: creates the first stage results in a seperate table
bys foreign: ///
outreg2 using output1_1st_NEW, label excel stats(coef se) dec(2) nocons nor2 keep(rep78) replace: ///
reg mpg headroom trunk rep78
cap erase output1-IV-F
cap erase output1_1st-stage1
cap erase output1_1st_NEW

Can you use lincom with interaction models coded with ## in Stata?

I am trying to use lincom to sum regression coefficients in a Stata model that codes interaction using ##. Exposure and bmi are continuous variables. Sex is binary.
regr bmi c.exposure##sex covar1 covar2 covar3 i.covar4 i.covar5
lincom chemical + chemical#sex
The regression works just fine, but lincom gives the following error:
exposure##sex invalid name
Alternatively, if I code the second line as
lincom chemical + c.chemical##sex
then I get
invalid matrix stripe;
c.l10_mep_i_sg2_pg##sex
Am I doing something wrong or is this not possible with # interaction coding?
Try adding the , coeflegend option at the end of your regression command. This will allow you to see what Stata calls each coefficient.
Here's a reproducible example:
sysuse auto
reg price i.foreign##c.mpg, coeflegend
lincom 1.foreign+ 1.foreign#c.mpg*25
Alternatively, this sort of thing can usually be done much more easily with margins:
margins, dydx(foreign) at(mpg=25)
Both of these give you the marginal effect of foreign origin on price when miles per gallon is 25.

Syntax for bootstrap estimates from ttest command

I am attempting to demonstrated characteristics of various tests for small samples of data. I would like to demonstrate the performance of the t-test, t-test with bootstrap estimation and the ranksum test. I am interested in obtaining the p-value for each test on multiple sets of data using simulate. However, I cannot obtain t-test estimates using the bootstrap prefix and ttest command.
The data is generated by:
clear
set obs 60
gen level = abs(rnormal(0,1))
gen group = "A"
replace group = "B" if [_n] >30
bootstrap, reps(100): ttest level, by(group)
bootstrap _b, reps(100): ttest level, by(group)
bootstrap boot_p = e(p), reps(100): ttest level, by(group)
The errors for each of the procedures in order are:
expression list required
invalid expression: _b
'e(p)' evaluated to missing in full sample
These results are not consistent with the documentation for the bootstrap prefix. Is there some problem with specification of e or r class objects and ttest ?
Edit:
Understanding now that r-class is the correct group of scalars, I still do not generate a variable 'p' given the code provided in the solution. Additionally:
clear
set more off
set obs 60
gen level = abs(rnormal(0,1))
gen group = "A"
replace group = "B" if [_n] >30
bootstrap p=r(p), reps(100): ttest level, by(group)
display r(p)
does not return the p-value.
ttest is an r-class command and stores its reults in r(). You seem to expect for it to save results in e(), like an e-class command. The norm is that the latter kind fit models; ttest is not in this category.
The two-sided p-value is stored in r(p), as indicated in help ttest:
clear
set more off
set obs 60
gen level = abs(rnormal(0,1))
gen group = "A"
replace group = "B" if [_n] >30
bootstrap p=r(p), reps(100): ttest level, by(group)

Stata: Combine table command with ttest and output latex

For regression output, I usually use a combination of eststo to store estimations, estadd to add the R2 and additional tests, then estab to output the lot.
I need to do the same with the table command. I need the mean, median and N for a variable across three by variables and would like to add stars for the result of a ttest==1 on the mean and signtest==1 on the median. I have three by variables, so I've been using table to collate the mean, median and N, which I'm calling like the following pseudo-code:
sysuse auto,clear
table foreign rep78 , ///
contents(mean price median price n price) format(%9.2f)
ttest price==1, by(foreign rep78)
signtest price=1, by(foreign rep78)
I've tried esttab and estpost to no avail. I've also looked at tabstat, tablemat and summarize as alternatives to table, but they don't allow three by variables.
How can I create this table, add the stars for the ttest and signtest p-values and output the full table?
The main point in your question seems to be producing a LaTeX table. However, you show "pseudo-code", that looks pretty much like Stata code, with the caveat that it is illegal.
In particular, for the ttest you can only have one variable in the by() option. But notice that ttest allows also the by: prefix (you can use both, in fact). Their reasons-to-be are different. On the other hand, signtest does not allow a by() option but it does allow the by: prefix. So you should probably clarify what you want to do before creating the table.
If you are trying to use the by: prefix in both cases and afterwards produce a table, you can create a grouping variable, and put the commands in a loop. In this way, you can try tabulating the saved results for each group using the ESTOUT module (by Ben Jann in SSC). Something like:
*clear all
set more off
sysuse auto
keep price foreign rep78
* create group variable
egen grou = group(foreign rep78)
* tests by group
forvalues i = 1/8 {
ttest price == 1 if grou == `i'
signtest price = 1 if grou == `i'
*<complete with estout syntax>
}
See help by, help egen (the group function), help estout and help saved results.

Storing the predictions and coefficients from Stata for n replications

I have a following code where I am trying to replicate the estimation for n times and then generating prediction and coefficients for further use.
capture program drop mypro
program define mypro
drop _all
sysuse auto
bsample
reg mpg price headroom
mat mycoef=e(b)
gen mypri=mycoef[1,1]
gen myhead=mycoef[1,2]
gen mycons=mycoef[1,3]
predict x1b
end
simulate, seed(10) reps(10) nodots : mypro
The simulate by default gives only the coefficients from 10 different samples. However, I am trying to save each sample dataset along with coefficients mpri, myhead, myconst,and x1b . Is it possible to do this using simulate or do I need to use loop?
Updated as per comment of Nick:
capture program drop mypro
program define mypro
set seed 1
local r=10
forvalues i=1/`r'{
drop _all
sysuse auto
bsample
reg mpg price headroom
mat mycoef=e(b)
gen mypri=mycoef[1,1]
gen myhead=mycoef[1,3]
predict x1b
save data`i',replace
}
end
You are calling simulate to run your program to take a bootstrap sample to get regression results.
sysuse auto
bootstrap : reg mpg price headroom
is a much simpler approach. Look at the documentation for bootstrap to learn more.