Can you use lincom with interaction models coded with ## in Stata? - 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.

Related

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

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

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 -- Durbin Watson statistic by company id in a large dataset

I am trying to run regressions in Stata by company_id using a large dataset. The goal is to get a line for each company_id with results of the regression. I am using the following code that gives me the beta coefficient, std error, adj r-squared and N. But I also need to include the Durbin Watson statistic and have not been successful doing that so far. Can someone help? Thanks.
statsby _b _se r2 = e(r2_a) _N, by (company_id) saving($path\SC_results_`i'.dta, replace): regress ret sptr_ret
A small program that combines regress and dwstat into one command should help. Here's an attempt.
capture program drop reg_dw
program reg_dw, rclass
syntax varlist
regress `varlist'
dwstat
return scalar dw=r(dw)
end
webuse invest2,clear
gen index=_n
tsset index
statsby _b _se r2 = e(r2_a) dw=r(dw) _N, by (company) saving(x.dta, replace): reg_dw invest market
use x, clear
tab _eq2_dw

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.

Stata: How to top-code a variable

Using the auto.dta data, I want to top-code the PRICE variable at the median of top X%. For example, X% could be 3%, 4%, etc. How can I do this in Stata?
In answering your question, I am assuming that you want to replace all the values above, say top 10%, with value say X(top 90% in the following code).
Here is the sample code:
program topcode
sysuse auto, clear
pctile pct = price, nq(10)
dis r(r9)
gen newprice=price
replace newprice=r(r9) if newprice>r(r9)
end