Descriptive Statistics

Univariate Statistics

Karol Flisikowski

2022-05-09

Data

In our example this week, we are going to use the fake data - about real estates in Wroclaw - prices by districts, size of apartments and many more.

Data wrangling

As you can see, not all formats of our variables are adapted. We need to prepare appropriate formats of our variables according to their measurement scale and future application.

mieszkania$district<-as.factor(mieszkania$district)
mieszkania$building_type<-as.factor(mieszkania$building_type)
mieszkania$rooms<-factor(mieszkania$rooms,ordered=TRUE)
attach(mieszkania)
mieszkania$price_PLN<-as.numeric(mieszkania$price_PLN)
mieszkania$price_EUR<-as.numeric(mieszkania$price_EUR)

Frequency Tables and TAI

In the first step of our analysis, we will group our data into a simple frequency table.

First, let’s look at the distribution of housing prices in our sample and verify tabular validity using the TAI measure:

Ok, it looks quite ugly, so let’s wrap it up using the ‘kable’ package:

Apartments in Wroclaw - prices in kPLN
x label Freq Percent Valid Percent Cumulative Percent
Valid 350-450 kPLN 9 4.5 4.5 4.5
450-550 kPLN 21 10.5 10.5 15.0
550-650 kPLN 33 16.5 16.5 31.5
650-750 kPLN 36 18.0 18.0 49.5
750-850 kPLN 31 15.5 15.5 65.0
850-950 kPLN 36 18.0 18.0 83.0
950-1050 kPLN 21 10.5 10.5 93.5
1050-1150 kPLN 10 5.0 5.0 98.5
1150-1250 kPLN 2 1.0 1.0 99.5
1250-1350 kPLN 1 0.5 0.5 100.0
Total 200 100.0 100.0
Missing <blank> 0 0.0
<NA> 0 0.0
Total 200 100.0
##        # classes  Goodness of fit Tabular accuracy 
##       10.0000000        0.9780872        0.8508467

As we can see - the TAI index is quite high. 0.85 means that we can accept the proposed construction of the frequency table.

Basic plots

In this section, we should represent our data using basic (pre-installed in R) graphics. Select the most appropriate graphs depending on the scale of the selected variables. Explore the heterogeneity of the distribution by presenting the data by group (e.g., by neighborhood, building type, etc.). Don’t forget about main titles, labels and legends. Read more about graphical parameters here.

Note that the echo = FALSE parameter has been added to the code snippet to prevent printing the R code that generated the graph.

ggplot2 plots

Now, let’s use the ggplot2 and ggpubr libraries to plot.

Ggplot2 allows you to show the average value for each group using the stat_summary() function. You no longer need to calculate average values before creating a graph!

Faceting

Faceting generates small multiples, each showing a different subset of the data. They are a powerful tool for exploratory data analysis: you can quickly compare patterns in different parts of the data and see if they are the same or different. Read more here.

Univariate Statistics

Before automatically reporting the full summary table of descriptive statistics, this time your goal is to measure the central tendency of the price distribution. Compare the mean, median, and mode along with positional measures - quantiles - by neighborhood and building type or number of rooms in an apartment.

    mean(price_PLN)
## [1] 760035
    median(price_PLN)
## [1] 755719.5
    sd(price_PLN) #standard deviation
## [1] 186099.8
    var(price_PLN) #variance
## [1] 34633125960
    coeff_var<-sd(price_PLN)/mean(price_PLN) #coefficient of variability %
    coeff_var
## [1] 0.2448568
    IQR(price_PLN)# difference between quartiles =Q3-Q1 
##      75% 
## 282686.5
    sx<-IQR(price_PLN)/2  #interquartile deviation
    coeff_varx<-sx/median(price_PLN) #IQR coefficient of variability %
    coeff_varx
##       75% 
## 0.1870314
    min(price_PLN)
## [1] 359769
    max(price_PLN)
## [1] 1277691
    quantile(price_PLN,probs=c(0,0.1,0.25,0.5,0.75,0.95,1),na.rm=TRUE)
##        0%       10%       25%       50%       75%       95%      100% 
##  359769.0  518806.8  619073.8  755719.5  901760.2 1054250.8 1277691.0

Ok, we have calculated all of the basic summary statistics above. Let’s wrap them up together now.

rooms boxplot histogram line1 line2 points1
1
2
3
4

Ok, now we will finally summarize the basic measures of central tendency for prices by neighborhood/building type using the ‘kable’ package. Feel free to customize your final report. See some hints here.

Table 1. Apartments in Wroclaw - prices in PLN by number of rooms.
1 room 2 rooms 3 rooms 4 rooms
Min 359769.00 590286.00 632770.00 736669.00
Max 657146.00 888634.00 965829.00 1277691.00
Q1 479684.75 634757.25 769683.75 909371.50
Median 520507.00 677260.00 846303.50 964338.50
Q3 555024.75 717728.50 901078.75 1050976.75
Mean 515518.05 683567.70 833706.02 974809.96
Sd 66951.03 65072.66 86943.90 113819.21
IQR 75340.00 82971.25 131395.00 141605.25
Sx 37670.00 41485.62 65697.50 70802.62
Var % 0.13 0.10 0.10 0.12
IQR Var % 0.14 0.12 0.16 0.15
Skewness -0.20 0.80 -0.42 0.33
Kurtosis -0.38 0.48 -0.83 0.05

Your task this week is to interpret all of the basic measures we have calculated above. Feel free to change this report: adjust formats, add plots and charts etc.

We are going to discuss the results next week together during our class. See you soon!