5.1 Viewing univariate statistics

5.1.1 Skim in SkimR

The video and the OpenStax textbook explain the key measure of dispersion and measures of central tendency. They are easy to view in R and there are many tools to do this. The most complete is the skim command in the skimr package.

Note: For reasons I can’t figure out, skim won’t run inside this document. I will post examples in Blackboard.

#skim_without_charts(my_gapminder_data_2003)

skim groups variables by type:

  • character or text data
  • factor or categorical data
  • numeric or continuous data

As you can see above, skim provides a lot of information in a small space. For continuous data (numerics in R) skim provides:

  • variable name (variable),
  • missing values (n_missing),
  • the number of observations or cases (complete and n),
  • the mean (mean),
  • standard deviation (sd),
  • the minimum value (p0),
  • the 25th percentile (p25),
  • the 50th percentile the median (p50),
  • the 75th percentile (p75),
  • the maximum value (p100),
  • and a small histogram (noted as hist).

For categorical data (factors in R) skim provides a count of each category.

It also lists character data.

5.1.2 descr in sjmisc

An alternative is the descr command in the sjmisc package.

descr(my_gapminder_data_2003)
## Descriptive Statistics  
## my_gapminder_data_2003  
## N: 163  
## 
##                     co2EmissionsPerPerson   giniCoefficent   mortalityRate      population
## ----------------- ----------------------- ---------------- --------------- ---------------
##              Mean                    4.86            40.04           49.08     35912207.98
##           Std.Dev                    7.26             7.84           51.31    138090625.04
##               Min                    0.02            25.20            3.20        28600.00
##                Q1                    0.46            33.70            9.51      1450000.00
##            Median                    2.00            40.00           24.80      7110000.00
##                Q3                    7.02            43.70           81.70     22500000.00
##               Max                   60.60            64.10          217.00   1320000000.00
##               MAD                    2.69             7.71           27.81      9530152.80
##               IQR                    6.56             9.95           71.85     20510000.00
##                CV                    1.49             0.20            1.05            3.85
##          Skewness                    3.69             0.58            1.15            7.88
##       SE.Skewness                    0.19             0.19            0.19            0.19
##          Kurtosis                   21.21             0.19            0.19           65.43
##           N.Valid                  161.00           163.00          163.00          163.00
##         Pct.Valid                   98.77           100.00          100.00          100.00
## 
## Table: Table continues below
## 
##  
## 
##                     studentSpendingPerCapita      year
## ----------------- -------------------------- ---------
##              Mean                       0.16   2003.00
##           Std.Dev                       0.05      0.00
##               Min                       0.05   2003.00
##                Q1                       0.11   2003.00
##            Median                       0.15   2003.00
##                Q3                       0.20   2003.00
##               Max                       0.28   2003.00
##               MAD                       0.06      0.00
##               IQR                       0.08      0.00
##                CV                       0.34      0.00
##          Skewness                       0.20       NaN
##       SE.Skewness                       0.31      0.19
##          Kurtosis                      -0.90       NaN
##           N.Valid                      61.00    163.00
##         Pct.Valid                      37.42    100.00

It lists similar statistics as skim.

  • variable name (var),
  • type of data (type),
  • label for the variable if available (label)
  • the number of observations or cases (n),
  • missing values (NA_prc),
  • the mean (mean),
  • standard deviation (sd),
  • standard error (se),
  • median (md),
  • the trimmed mean (trimmed),
  • minimum value, maximum value, and range (range),
  • iqr (the interquartile range),
  • a measure of skewness (skew).

5.1.3 Summary in Base R

Base R also has a similar command, summary. It does not provide standard deviation. It does rank the top categories of categorical data.

Below I ran a my small WVS dataset in summary.

summary(ethiopia_small)
##                                             N_REGION_WVS
##  ET-OR Oromia                                     :261  
##  ET-AM Amara                                      :245  
##  ET-SO Somali                                     :208  
##  ET-SN Southern Nations, Nationalities and Peoples:120  
##  ET-TI Tigrai                                     :113  
##  ET-AA Addis Ababa                                : 92  
##  (Other)                                          :191  
##                           N_TOWN              G_TOWNSIZE           G_TOWNSIZE2 
##  Boloso Sore - Wereda        :  50   5,000-10,000  :358   Under 5,000    :196  
##  Hawassa - City administation:  36   10,000-20,000 :287   5000-20000     :645  
##  Omonada - Wereda            :  31   20,000-50,000 :199   20000-100000   :357  
##  Dedo- Wereda                :  30   50,000-100,000:158   100000-500000  : 23  
##  Dire Dawa-Urban             :  30   2,000-5,000   :103   500000 and more:  9  
##  Gedeb - Wereda              :  30   Under 2,000   : 93                        
##  (Other)                     :1023   (Other)       : 32                        
##                                                  H_SETTLEMENT H_URBRURAL 
##  Capital city                                          :101   Urban:296  
##  Regional center                                       :115   Rural:934  
##  District center                                       :284              
##  Another city, town (not a regional or district center): 99              
##  Village                                               :631              
##                                                                          
##                                                                          
##                          Q34     
##  Agree strongly            :541  
##  Agree                     :372  
##  Neither agree nor disagree: 43  
##  Disagree                  :206  
##  Disagree strongly         : 65  
##  NA's                      :  3  
##                                  
##                                            Q241    
##  An essential characteristic of democracy    :531  
##  Not an essential characteristic of democracy:299  
##  9                                           : 87  
##  8                                           : 79  
##  7                                           : 49  
##  (Other)                                     :142  
##  NA's                                        : 43  
##                                            Q242    
##  Not an essential characteristic of democracy:416  
##  An essential characteristic of democracy    :336  
##  2                                           : 75  
##  9                                           : 69  
##  3                                           : 55  
##  (Other)                                     :230  
##  NA's                                        : 49  
##                                            Q243    
##  An essential characteristic of democracy    :808  
##  9                                           :147  
##  8                                           : 92  
##  Not an essential characteristic of democracy: 53  
##  7                                           : 29  
##  (Other)                                     : 42  
##  NA's                                        : 59  
##                                            Q244    
##  An essential characteristic of democracy    :547  
##  Not an essential characteristic of democracy:166  
##  9                                           :122  
##  8                                           : 96  
##  7                                           : 67  
##  (Other)                                     :182  
##  NA's                                        : 50  
##                                            Q245    
##  An essential characteristic of democracy    :431  
##  Not an essential characteristic of democracy:317  
##  9                                           : 96  
##  8                                           : 72  
##  7                                           : 49  
##  (Other)                                     :196  
##  NA's                                        : 69  
##                                            Q246    
##  An essential characteristic of democracy    :628  
##  9                                           :158  
##  Not an essential characteristic of democracy: 95  
##  8                                           : 94  
##  7                                           : 52  
##  (Other)                                     :110  
##  NA's                                        : 93  
##                                            Q247    
##  An essential characteristic of democracy    :459  
##  Not an essential characteristic of democracy:262  
##  9                                           : 97  
##  8                                           : 75  
##  2                                           : 58  
##  (Other)                                     :231  
##  NA's                                        : 48  
##                                            Q248    
##  An essential characteristic of democracy    :690  
##  9                                           :152  
##  8                                           :110  
##  Not an essential characteristic of democracy: 82  
##  7                                           : 58  
##  (Other)                                     : 92  
##  NA's                                        : 46  
##                                            Q249                       Q250     
##  An essential characteristic of democracy    :817   Absolutely important:1044  
##  9                                           :173   9                   :  47  
##  8                                           : 84   5                   :  44  
##  Not an essential characteristic of democracy: 36   8                   :  35  
##  7                                           : 29   Not at all important:  14  
##  (Other)                                     : 31   (Other)             :  36  
##  NA's                                        : 60   NA's                :  10  
##                     Q251                       Q252    
##  5                    :282   Not satisfied at all:328  
##  Not at all democratic:218   5                   :242  
##  Completely democratic:198   Completely satisfied:137  
##  6                    :123   6                   : 96  
##  7                    : 94   7                   : 91  
##  (Other)              :299   (Other)             :317  
##  NA's                 : 16   NA's                : 19  
##                       Q253                   Q254                    Q255    
##  A great deal of respect:169   Very proud      :1061   Very close      :683  
##  Fairly much  respect   :468   Quite proud     : 129   Close           :371  
##  Not much respect       :371   Not very proud  :  25   Not very close  :107  
##  No respect at all      :207   Not at all proud:  14   Not close at all: 64  
##  NA's                   : 15   NA's            :   1   NA's            :  5  
##                                                                              
##                                                                              
##                Q256                   Q257                   Q258    
##  Very close      :542   Very close      :355   Very close      :172  
##  Close           :457   Close           :512   Close           :290  
##  Not very close  :165   Not very close  :246   Not very close  :400  
##  Not close at all: 60   Not close at all:104   Not close at all:266  
##  NA's            :  6   NA's            : 13   NA's            :102  
##                                                                      
##                                                                      
##                Q259         Q260          Q261         E1_LITERACY 
##  Very close      :159   Male  :622   1990   : 89   Literate  :826  
##  Close           :226   Female:608   2000   : 83   Illiterate:404  
##  Not very close  :374                1998   : 77                   
##  Not close at all:349                1995   : 72                   
##  NA's            :122                1985   : 66                   
##                                      1992   : 61                   
##                                      (Other):782                   
##     Q275R                  Q251_recode 
##  Lower :803   Not Democratic     :318  
##  Middle:241   Somewhat Democratic:572  
##  Higher:180   Democratic         :324  
##  NA's  :  6   NA's               : 16  
##                                        
##                                        
## 

5.1.4 Create a Codebook Using the Summarytools package

Use the following command to create a nice codebook. It will open as a file in the bottom right quadant of RStudio. It creates an HTML file that can be opened in any web browser and printed.

view(dfSummary(ethiopia_small))

5.1.5 Single variables

You can also run skim and summary on a single variable.

skim(my_usstate_data_small_2011$exp_correction)
summary(my_gapminder_data_2003$mortalityRate)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    3.20    9.55   24.80   49.08   81.40  217.00