Monday, February 10, 2014

Descriptive Data Analysis

This week we are going to learn about conducting basic data analysis such as tabulation, cross tabulation, mean, standard deviation, median, mode, minimum, and maximum. You can use the Happiness Survey data to conduct the analysis.


Tabulation

In Stata, command “tab” will give you three things: freq. (or frequency), Percent, and Cumulative Percent. You use tab when your variables are categorical (e.g., gender, political affiliation, ethnicity etc.). Below is an example of how “tab” command looks like with e.g., Gender:


It should be noted that this data coded 1 as Male and 2 as female. So what this result shows is that we have 8 males (or 33.33%) and 16 females (or 66.67%) in your data. Forget about the Cum. column as you will not use it as much (if you want to learn more about it, here is the link). Sometimes you may forget what you coded for 1 and 2. I would recommend relabeling the variable Gender. You would type: label var Gender "gender: 1=male & 2=female". Here how it looks like after you entered that command:


Now you can see that 1 is male and 2 is female right in the results table.

You can also use tab for continuous variables as well if you want to know the number/percentage of a specific value. For example, in your Happiness Survey data, we have a question that asks number of years of education of the participants. The response to this question ranges from at least 12 years of education to higher. Below is an example of tab Edu:

tab Edu


Based on the above table, I want to know how many respondents who have 20 years of education. What it shows you here is that 17.39% (N=4) of your respondents have 20 years of education. It gives you the details of each year of education reported. You can also see that there is one person whose education is 23 years. Therefore, depending on your need of information, you can use tab command for either categorical or continuous variables, but you may find yourself using it more for categorical variables.

Cross-Tabulation 

Cross-tabulation gives you results of frequency and percentage of two groups of variables, such as gender (male vs. female) and pet owning (owning vs. not owning). In Stata, you can do this by using the command "tab" and the two variables right after it: "tab Gender Pet". Here is how it looks like:

tab Gender Pet


Based on the above table, you can see that females (coded as 2, N=12) tend to own more pet (coded as 1 for owning a pet, and 0 not owning one) than males do.

You can also request for percentage as well for easy read. You can add "column" or "col", "cell" and/or "row", right after the comma: "tab Gender Pet, col row cell". Here is how it looks like:

tab Gender Pet, col row cell






Here is how you interpret the result above:


From a row of Gender: among all of the female (coded as 2) who responded to the survey, 75% own a pet.

From a column of Pet: among all of those who responded owning a pet (coded as 1), 80% are females.

From a cell of Pet: among all of the respondents, 52.17% are females owning a pet.



Summary 

In Stata, the command "summary" or "sum" in short gives you Obs (your total number of response or your total number of participants), Mean (or your average score), Stad. Dev. (standard deviation), Min (minimum) and Max (maximum). You use "sum" command basically when you want to know the average of a variable (such as income, age, or likert scale response). Your variables should be continuous, ordinal, interval, or ratio (for more info of scale measurements, you can read via this link). Let's try to find the average or mean of GPA based on your Happiness Survey:

sum GPA


What you got here is the mean value of GPA which is 3.79. It seems that everyone is doing so well as you can see that the deviation is pretty small (SD=0.20). The values of Min and Max tell you the lowest GPA and highest GPA. We covered this as well in your previous class on data cleaning. Later, you will learn how to compare means (of GPA) with different groups or categories (such as gender or ethnicity). Techniques used for mean comparisons are t-test and ANOVA (One way Analysis of Variance). For now, let's just look into mean comparisons using a very basic technique using the command: tab Gender, sum (GPA). This command will give you result of the average GPA across gender. In other words, your question would be "Is there any differences in academic performance among male and female?" Here is how it looks like:

tab Gender, sum (GPA)


The table above tells you that female students tend to perform better than their male counterparts. The average GPA for female is 3.834 and for male 3.7.


Mode

Mode is the number that appears to be repeatedly more often than other numbers. As far as I know, Stata does not have a command for Mode. I may have missed it, but you all can help me search for it. But do not worry about it as Mode is not commonly used. Just to give you a whole picture of how to find mode in your Stata report, I use the Edu variable as an example. You use the command "tab" to give you a list of all the responses. Type tab Edu and you will get the following look:

tab Edu



Based on the table above, the 17 years of education appears to be the most common number reported by the participants--6 people reported that they have 17 years of education. So 17 is your mode value.


Median 

Media is the value in the middle. It is useful for some variables such as income, as mean may be misleading if a few people earn way too high or too low. According to 2010 U.S. Census, the income is reported using median. An except below is how it is written (full link to the report is here):

For the purpose of illustration, let's use the variable Edu from our Happiness survey, and try to find out what is the median (we do not have income variable though). In Stata, the median can be generated via the use of "sum" but with an additional request by using "detail" option after comma. Here is how it looks like: "sum Edu, detail". Detail can be "d" as a shortcut.

sum Edu, detail 


Based on the above table, the median is the 50%, which is 18 years of education. 

Practice on your own

1. What is the average of number of countries have the respondents been to? 
2. Are there any differences in the number of countries visited based on gender? 
3. Did those who vote during the election have higher GPA than those who did not vote? 
4. Did those who vote during the election like to drink wine than those who did not vote? 
5. Create two questions on your own and run the analysis. 

Good luck! 


Monday, February 3, 2014

Data Cleaning and Editing

This week we will learn about data cleaning and editing. Data cleaning is a procedure to examine if the values of your variables are entered correctly. As you recall from the last week's class, we do data coding in order to make data (participants' responses) more manageable, in a numeric pattern so that statistical software such as Stata, SPSS, or Excel can read, hence allowing you to analyze them. So how do you go about checking if the values of your variables were entered correctly? We can check for accuracy to some extent but not entirely. For example, in your Happiness Survey data (code file), if we were to investigate if the values of variable "political affiliation" were entered correctly, we use the command: "summary" or "sum" in short, of that variable that was coded as “Pol”. The values of this variable range from 1-3 in which 1 indicates Democrat, 2 Republican, and 3 None. When you use the command “sum” it will give you Obs (your number of your respondents), Mean (or the average), Std. Dev. (standard deviation), Min (minimum) and Max (maximum).  So here how it looks like in Stata:  




Note that the possible values of Pol are 1-3, but what it shows you here is Min 1 and Max 4. So there is something wrong with the data that we entered; specifically, the value 4 should not be in the data. So the next step is to check to find out the number 4 based on the ID associated with this number. Now, we are going to use the command "tabulation" or "tab" in short. So we type "tab Pol" in the command box. Here how it looks like:

tab Pol 




Now what we see is that there are 6 respondents that have the value 4. And you know right away that Gail may have made a mistake when she entered the data with these 6 respondents. So we need to check the IDs of these 6 people. Once we know the IDs, then we can go back to your paper questionnaire. This is why we have to always ID your questionnaire before you enter the data, so that we can always go back to the original source. To find out who are these 6 respondents, we use the command: "tab". So now we "tab ID" with enforced condition only for the variable Pol that has the value of 4. The conditional command is "if". So here how it looks like with the conditional command: "tab ID if Pol==4". Note that the equal sign is double, and variables in STATA are case sensitive. Here is how the output looks like: 

tab ID if Pol==4



Now you know which questionnaire to look into. So if you have the questionnaires in front of you, you can look them up with the above IDs which are: 1, 5, 6, 7, 9, 14. Then, the next step is to fix it using the command "edit". Other conditional command will be used as well such as "|". This "|" tells Stata to select certain number of ID you would like obtain. Here, we want to obtain IDs: 1, 5, 6, 7, 9, and 14, only for Pol equal to 4. Here is how you should type in your command box: "edit Pol if (ID==1 | ID==5 | ID==6 | ID==7 | ID==9 | ID==14)". Note that we use parenthesis right after "if" so Stata knows that you want to request 1 and 5 and 6 and so on. You can also do it one at a time. For example, "edit Pol if ID==1", "edit Pol if ID==5" and so on. Either way that you are comfortable with. So here is how it looks like after you type, "edit Pol if (ID==1 | ID==5 | ID==6 | ID==7 | ID==9 | ID==14)" in your command box: 

edit Pol if (ID==1 | ID==5 | ID==6 | ID==7 | ID==9 | ID==14)




Now Stata gives you all the IDs that you requested that has the value 4 for Pol. Now you can go ahead and replace the value that you found in your paper questionnaire for Pol variable, right in above Window. 

PRACTICE ON YOUR OWN

Now it is your turn to practice on your own. To do this, please download the Excel file HERE. I changed some values in it for practice purpose. Then do the following things: 

1. Click on File
2. Import
3. Excel spreadsheet
4. Browse to the Excel file that you just downloaded from the CourseSite then Open 
5. Check on the box that says: Import first row as variable names 
6. Ok 

Here is what you are supposed to do. I have purposefully selected three variables for you to work on: 

1-Wine (Do you drink wine?). The response is 0 for No and 1 for Yes. So it is between 0 and 1. 
2-Hincred (I am incredibly happy). The response is 1-5 in which 1 is Less True and 5 is More True. 
3-Pfocus (I have difficulty concentrating). The response is 0-3 in which 0 is Never and 3 is Always. 

Your task is to find out if these three variables contain any values that are entered by mistakes. For example, you know that the variable Wine contains just 0 and 1, so the values that are larger than 1 indicates mistake and that is your task to find that out. More specifically, you what you will need to show me is the IDs with the values that were mistakenly entered. Here is exactly what I want from you: 

1-Wine, the IDs are: ..........................
2-Hincred, the IDs are: ......................
3-Pfocus, the IDs are: ........................

I look forward for your results. 

Good Luck!