QPM Lab 2: Data Visualization & Descriptive Statistics in R and R Commander Viktoryia Schnose & Betul Demirkaya Department of Political Science Washington University, St. Louis September 4, 2013 Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 1
A Reminder From Last Class 1 Open R 2 Write the following in the command line: library(rcmdr) 3 This way, you open the package from the R library. It does not open automatically. 4 A new window should have opened. This is R commander. 5 Go to the website for QPM. 6 Open the Class Datasets. Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 2
Goals for the Class Reading a dataset & viewing the dataset with R commander Re-coding a variable to a factor Descriptive Statistics with R commander Bar graphs with R commander Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 3
Reading Data with R: Answering Two Questions 1 What type of file do you have?.csv = comma separated values.txt = text file.dta = STATA file.spss = SPSS file 2 Where is the file? Saved in your computer Online in a url Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 4
Reading Data with R:.csv or.txt STEP 1: What type of file are we using for the homework? Wednesday, September 7, 2011 Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 5
Reading Data with R:.csv or.txt STEP 2: Where is the file for the dataset for this week s HW? See Appendix for how to enter the URL. Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 6
Brazil Dataset: Viewing the Dataset Active Dataset Click here Variable name Observation number Wednesday, September 7, 2011 Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 7
Brazil Dataset: Converting vote variable into a factor = qualitative variable Wednesday, ViktoryiaSeptember Schnose 7, 2011 & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 8
Brazil Dataset: Converting vote variable into a factor = qualitative variable II Select vote variable. Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 9
Brazil Dataset: Converting vote variable into a factor = qualitative variable III Specify what each numeric value means (read the codebook). Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 10
Brazil Dataset: Frequency Distribution for vote variable Statistics Summaries Frequency distributions Wednesday, September 7, 2011 Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 11
Brazil Dataset: Bar Graph for vote variable (frequency) Graphs Bar Graph vote Frequency 0 200 400 600 800 no yes vote See Appendix for another way of presenting the same data. Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 12
In class assignment Break into groups of 3 or 4 individuals Fill out the sheet. ASK QUESTIONS. If you finish early, try to draw a bar graph for vote variable that shows probabilities. See Appendix for help. Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 13
Appendix: Table of Contents Using R & R Commander Reading.csv or.txt files from the internet Frequency distribution: Difference between R script and R output Another graph for vote variable showing probability R code to obtain the bar graph with probability Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 14
Using R & R Commander Drop down menus Toolbar Script Window: Here you will see R commands generated by the GUI. You can write commands here. Select them by highlighting them and press Submit Output Window: Dark Blue: printed output Red: command used Message Window: Red- Error Message Green - Warning Dark Blue - Other information Thursday, September 1, 2011 Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 15
Introduction Reading Data Re-coding a Variable Frequency Distribution Bar Graph Assignment Appendix Reading Data with R:.csv or.txt From the Internet STEP 3: Writing down the URL for the Brazil dataset. Prof. Montgomery s website Wednesday, September 7, 2011 Viktoryia Schnose & Betul Demirkaya QPM Lab 2: Data Visualization & Descriptive Statistics in R and R Commander 16
Difference between R Script and R Output Script Output Wednesday, September 7, 2011 Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 17
Brazil Dataset: Bar graph for vote variable (probability) Probability 0 20 40 60 80 no yes Did you vote in the last presidential election? Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 18
Brazil Dataset: Bar graph for vote variable (probability) This is how to modify the code to obtain the previous plot. Viktoryia Schnose & Betul Demirkaya QPM Lab 2: DataVisualization & Descriptive Statistics inrandrcommander 19