Data wrangling
As you can see not all formats of our variables are adjusted. We need to prepare the appropriate formats of our variables according to their measurement scales and future usage.
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 table and TAI
In the first stage of our analysis we are going to group our data in the form of the simple frequency table.
First, let’s take a look at the distribution of prices of apartments in our sample and verify the tabular accuracy using TAI measure:
|
## # classes Goodness of fit Tabular accuracy
## 10.0000000 0.9780872 0.8508467
As we can see - TAI index is quite high. 0.85 means that we can accept the proposed design of the frequency table.
Basic plots
In this section we should present our data using basic (pre-installed with R) graphics. Choose the most appropriate plots according to the scale of chosen variables. Investigate the heterogeneity of the distribution presenting data by groups (i.e. by district, building type etc.). Do not forget about main titles, labels and legend. Read more about graphical parameters here.
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
ggplot2 plots
In this section we will present the same plots but with the use of ggplot2 and ggpubr packages.
Ggplot2 allows to show the average value of each group using the stat_summary() function. No more need to calculate your mean values before plotting!
Using facets
Faceting generates small multiples each showing a different subset of the data. Small multiples are a powerful tool for exploratory data analysis: you can rapidly compare patterns in different parts of the data and see whether they are the same or different. Read more about facets here.
Descriptive statistics #1
Before automatically reporting the full summary table of descriptive statistics, this time your goal is to measure the central tendency of the distribution of prices. Compare mean, median and mode together with positional measures - quantiles - by districts and building types or no. of rooms per apartment.
mean(price_PLN)
median(price_PLN)
sd(price_PLN) #standard deviation
var(price_PLN) #variance
coeff_var<-sd(price_PLN)/mean(price_PLN) #coefficient of variability %
coeff_var
IQR(price_PLN)# difference between quartiles =Q3-Q1
sx<-IQR(price_PLN)/2 #interquartile deviation
coeff_varx<-sx/median(price_PLN) #IQR coefficient of variability %
coeff_varx
min(price_PLN)
max(price_PLN)
quantile(price_PLN,probs=c(0,0.1,0.25,0.5,0.75,0.95,1),na.rm=TRUE)
Summary tables with ‘kable’
Using kable and kableextra packages we can easily create summary tables with graphics and/or statistics.
| rooms | boxplot | histogram | line1 | line2 | points1 |
|---|---|---|---|---|---|
| 1 | |||||
| 2 | |||||
| 3 | |||||
| 4 |
Ok, now we will finally summarize basic central tendency measures for prices by districts/building types using ‘kable’ package. You can customize your final report. See some hints here.
| 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 interpretation goes here…
Descriptive statistics - summary table for quantitative variable using reporttools
We can also produce summary tables in Latex format (available only for PDF reports). Let’s see the summary table by district for both: price in PLN and price in EUR.
vars1<-mieszkania[,1:2]
tableContinuous(vars=vars1,group=mieszkania$district,longtable=FALSE)
Contingency tables using reporttools
We can easily construct contingency tables in Latex. Let’s print summary table by no. of rooms per apartment for districts and building types:
vars2<-mieszkania[,5:6]
tabela<-tableNominal(vars=vars2,group=mieszkania$rooms,longtable=FALSE)