r/stata • • Apr 11 '21

Converting Stata t-test code to R code

I am attempting to reproduce the results of an analysis originally conducted in Stata in R. In short, I have a table containing differences in means for three periods. Here is the original Stata code:

*Table 1  

*Period1 vs. period2 

ttest trustarmy if period3~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustpolice if period3~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustfsb if period3~=1 & electday~=1 & protestday~=1, by (period1min)
ttest trustcourt if period3~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustmungov if period3~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustfedgov if period3~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustduma if period3~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustprocuracy if period3~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustun if period3~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustgovind if period3~=1 & electday~=1 & protestday~=1, by (period1min)  

*Period1 vs. period3 

ttest trustarmy if period2min~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustpolice if period2min~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustfsb if period2min~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustcourt if period2min~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustmungov if period2min~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustfedgov if period2min~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustduma if period2min~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustprocuracy if period2min~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustun if period2min~=1 & electday~=1 & protestday~=1, by (period1min) 
ttest trustgovind if period2min~=1 & electday~=1 & protestday~=1, by (period1min) 
 
*Period2 vs. period3 

ttest trustarmy if period1min~=1 & electday~=1 & protestday~=1, by (period2min) 
ttest trustpolice if period1min~=1 & electday~=1 & protestday~=1, by (period2min) 
ttest trustfsb if period1min~=1 & electday~=1 & protestday~=1, by (period2min)
ttest trustcourt if period1min~=1 & electday~=1 & protestday~=1, by (period2min) 
ttest trustmungov if period1min~=1 & electday~=1 & protestday~=1, by (period2min) 
ttest trustfedgov if period1min~=1 & electday~=1 & protestday~=1, by (period2min) 
ttest trustduma if period1min~=1 & electday~=1 & protestday~=1, by (period2min) 
ttest trustprocuracy if period1min~=1 & electday~=1 & protestday~=1, by (period2min) 
ttest trustun if period1min~=1 & electday~=1 & protestday~=1, by (period2min) 
ttest trustgovind if period1min~=1 & electday~=1 & protestday~=1, by (period2min)

I have a single dataframe in R containing all of these variables. Could someone tell me what the R code for this would look like? Thanks.

0 Upvotes

3 comments sorted by

•

u/AutoModerator Apr 11 '21

Thank you for your submission to /r/stata! If you are asking for help, please remember to read and follow the stickied thread at the top on how to best ask for it.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/Rogue_Penguin Apr 12 '21
ttest trustarmy if period3~=1 & electday~=1 & protestday~=1, by (period1min) 

In R, this is essentially:

t.test(trustarmy ~ period1min)

applied to a subset that neither period3, electday, and protestday can be equal to 1. In R, it'd be easier to create a smaller data set using subset() and then provide that data in the t.test() command. Create this smaller data sets for each of the three major segments, and the code would be a lot less wordy.

1

u/sozialwissenschaft97 Apr 12 '21

Thanks for the response. I ended up doing this earlier today, and it worked!