Stata: Create id by group

Sunday, April 22, 2012

When doing your data analysis, sometimes you will encounter the following situation: in your dataset, everyone has an unique id. However, their IDs are long and each participant has multiple record (or the dataset is in a long format).

To visualize your data, you need to create a new ID for each individual regardless of how many records each person has. For example, the first person has three records, and we would like to assign a new ID 1 for the first person, and the second person would be 2.

Though it sounds difficult and tedious, it is not difficult to do so.  

egen id = group(oldid)

Just one line and your problem will be solved.

Reference: http://www.stata.com/support/faqs/data/group.html

Stata tutorial index

Saturday, May 14, 2011

This is an idex page for Stata tutorial I have written on this blog. I will update this index if I write more. This index also reminds me what I should have written.

Data management

Stata: How to deal with missing values?

Export

Stata: How to export descriptive statistics tables?
Stata: Output correlation table
Stata: Export OLS regression table to Word or Excel
Stata: Export Logistic Regression (Coefficient/Odds ratio) to Word or Excel

Graph

Stata: Draw regression lines across groups

Endnote: Using Filter to import from CSA ILLUMINA (EndNote X4 compatible)

If you are in psychology or education, you would probably use CSA Illumina or Psycinfo a great deal. Sometimes you need to save time by importing references from CSA Illumina or Psycinfo. It is important to note that the correct filter is required for everything to work properly.

How do we do that?

I used an article from CSA Illumina for example:Melby, J. N., Conger, R. D., Fang, S.-A., Wickrama, K. A. S., & Conger, K. J. (2008). Adolescent family experiences and educational attainment during early adulthood. Developmental Psychology, 44(6), 1519-1536. 

After locating the file, you will see a “Save, Print, Email” option at the top. Click it.
1.png

Choose current view record and use full format without references. I personally do not like to import references from the article because these references may not meet your requirements
.
Then click “save”

2.png

Open this txt using endnote.exe, and EndNote will ask you to choose the right filter. We selected e-psyche (CSA).

6.png

The reference should then appear in your endnote library.
5.png

Packing list for conferences

Thursday, April 29, 2010

Packing
  • Passport or Photo ID (i.e., driver license)
  • Boarding pass
  • Camera  (with charger and cable line)
  • Laptop (and charger)
  • Clothes, socks, and PJs
  • Umbrella
  • Mobile phone (and charger)
  • Toothbrush and toothpaste
  • Medicine (in case if you have a cold or stomach ache)
  • Lip balm
  • Dollar bills (in case you want to leave tips)
  • Business cards
  • Hotel and transportation information
  • Nail clippers (in case you travel for a long time)
  • Comb
  • Flash drive (in case you want to share your files or use other people’s computer)
  • Slippers
  • Perfume
Before you leave
  • Backup your files in your laptop
  • Delete sensitive data (in case you use restricted dataset or you have private files)
  • Set your alarm
  • Copy your passport and other official documents
Updates:
2010.5.11: added perfume, shave, and necktie.
2010.5.12: added backing up passport and documents

Stata: Drawing regression lines across groups

Friday, December 18, 2009

When performing regression, one commonly compare groups, and the simple way to observe the differences is through graphs. Stata offers several ways to draw graphs. Here are two options.
 

1.png
Option 1:

regress inc educ male, beta
predict incfnoi if male==0
predict incmnoi if male==1
twoway (connected incmnoi educ if male==1, lcolor(black) ///
lpattern(dot) msymbol(diamond) msize(large)) ///
(connected incfno educ if male ==0, lcolor(black) ///
lpattern(solid) msymbol(circle) msize(large)), ///
ytitle(Income in thousands) xtitle(Education) ///
legend(order(1 "Men" 2 "Women")) scheme(s2manual)


This code snippet is from A gentle introduction to Stata. The results are presented above.
Option 2:

Install the two packages first:

net describe postgr3, from(http://www.ats.ucla.edu/stat/stata/ado/analysis)
net install postgr3.pkg
net describe xi3, from(http://www.ats.ucla.edu/stat/stata/ado/analysis)
net install xi3.pkg


followed by the code:

xi3: regress inc educ male, beta
postgr3 educ, by(male) table


Just two lines, isn’t that cool? Umm? Do you notice anything wrong? If you don’t like  footnotes, such as yhat_, male==0, you can click the highlighted part below and make some changes.

2.png

Select the area you wish to change, and change it in the area indicated by the red arrow.

3.png

This also provides a table with which to ensure everything is correct.

4.png

You can get more information on the UCLA website here: http://www.ats.ucla.edu/stat/stata/ado/analysis/postgr3.htm

You can also use predxcat and predxcon, with the command findit to get these two packages. I think that options 1 and 2 are adequate. Option 2 also allows you to draw interaction easily. I’ll talk about this in my next post.

Deals on Stata book

Saturday, October 31, 2009


a gentle introduction 3rd.png
A gentle introduction to Stata 


This is a perfect book for beginners.


There are 13 chapters:
1 Getting started
2 Entering data
3 Preparing data for analysis
4 Working with commands, do-files, and results
5 Descriptive statistics and graphs for one variable
6 Statistics and graphs for two categorical variables
7 Tests for one or two means
8 Bivariate regression and correlation
9 Analysis of variance
10 Multiple regression
11 Logistic regression
12 Measurement, reliability, and validity
13 Appendix: What's next?

I like this book because it covers concepts related to statistics as well as their application in a single book. It also tells you how to interpret the results obtained from the Stata output. For example, on page 178, after running a multiple regression:

regress csat expense percent income high college

the results with multiple regression equation are shown :

predicted csat = 851.56 + .00335 expense – 2.618 percent + .0001 income + 1.63 high + 2.03 college

This book also provides helpful interpretations:
Controlling for four other variables weakens the coefficient on expense from –.0223 to .00335, which is no longer statistically distinguishable from zero. The unexpected negative relationship between expense and csat found in our earlier simple regression evidently is spurious, and explained by other predictors.
Only the coefficient on percent (percentage of high school graduates taking the SAT) attains significance at the .05 level. We could interpret this “fourth-orer partial regression coefficient” (so called because its calculation adjusts for four other predictors) as follows.
This book includes many graphs, and when I learn stats I like to see what the results look like. This helps me to understand and remember the concepts I have studied. Visit A gentle introduction to Stata and find out today's deal on Amazon.

Stata: How to export descriptive statistics tables?

Friday, September 25, 2009

When performing statistics analysis, the first thing you would probably do is to run descriptive statistics. Knowing how to export tables of descriptive statistics can save a considerable amount of time.

If you would like to obtain the exact results that I did, you could use the following code for your  dataset:

use http://twtcsl.org/dataset/gss2000.dta
tab race
tab race sex
sum race sex age income
tab race, gen(d)
rename d1 dwhite
rename d2 dblack
rename d3 dother
tab sex, gen(d)
rename d1 dmale
rename d2 dfemale
sum dwhite dblack dother dmale age income if !mi(age) & !mi(income)


The results:

5.png

How do we export it? You can use EDIT-COPY TABLE in stata dropdown menu, or write some code to do the work.

estpost sum dwhite dblack dother dmale age income if !mi(age) & !mi(income)
esttab using sum2.rtf, cells("mean(fmt(2)) sd(fmt(2)) min(fmt(1)) max(fmt(0))") nomtitle nonumber replace


The export table looks like this:
1.png

To obtain three digits after the decimal point, change fmt(2) to fmt(3).

If you require more advanced descriptive statistics tables, for example, if you wanted to determine age and income by race, you could use the following codes:

sort race
by race: eststo: estpost sum age income if !mi(age) & !mi(income)
esttab using grp_sum.rtf, cells("mean(fmt(2)) sd(fmt(2))") replace


This result in a neat table:
2.png
If you are unable to export tables, check this article
Stata: Export OLS regression table to Word or Excel
and install the estout package.