mydata <- read.table("./insurance.csv", header=TRUE, sep=",", dec=".")
head(mydata) #show first 6 units
## age sex bmi children smoker region charges
## 1 19 female 27.900 0 yes southwest 16884.924
## 2 18 male 33.770 1 no southeast 1725.552
## 3 28 male 33.000 3 no southeast 4449.462
## 4 33 male 22.705 0 no northwest 21984.471
## 5 32 male 28.880 0 no northwest 3866.855
## 6 31 female 25.740 0 no southeast 3756.622
library(pastecs)
round(stat.desc(mydata[, -c(2, 5, 6)]), 2) #only show numerical variables
## age bmi children charges
## nbr.val 1338.00 1338.00 1338.00 1338.00
## nbr.null 0.00 0.00 574.00 0.00
## nbr.na 0.00 0.00 0.00 0.00
## min 18.00 15.96 0.00 1121.87
## max 64.00 53.13 5.00 63770.43
## range 46.00 37.17 5.00 62648.55
## sum 52459.00 41027.62 1465.00 17755824.99
## median 39.00 30.40 1.00 9382.03
## mean 39.21 30.66 1.09 13270.42
## SE.mean 0.38 0.17 0.03 331.07
## CI.mean.0.95 0.75 0.33 0.06 649.47
## var 197.40 37.19 1.45 146652372.15
## std.dev 14.05 6.10 1.21 12110.01
## coef.var 0.36 0.20 1.10 0.91
library(psych)
## Warning: Paket 'psych' wurde unter R Version 4.3.2 erstellt
##
## Attache Paket: 'psych'
## Die folgenden Objekte sind maskiert von 'package:ggplot2':
##
## %+%, alpha
## Das folgende Objekt ist maskiert 'package:car':
##
## logit
describeBy(mydata$bmi, mydata$smoker) #show groups of smokers and non-smokers
##
## Descriptive statistics by group
## group: no
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1064 30.65 6.04 30.35 30.49 6 15.96 53.13 37.17 0.28 -0.07 0.19
## ------------------------------------------------------------------------------------------
## group: yes
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 274 30.71 6.32 30.45 30.53 6.78 17.2 52.58 35.38 0.31 -0.07 0.38
library(ggpubr)
ggqqplot(mydata,
"bmi",
facet.by = "smoker") #assess distribution
t.test(mydata$bmi ~ mydata$smoker,
paired = FALSE,
var.equal = FALSE,
alternative = "two.sided")
##
## Welch Two Sample t-test
##
## data: mydata$bmi by mydata$smoker
## t = -0.13352, df = 410.9, p-value = 0.8938
## alternative hypothesis: true difference in means between group no and group yes is not equal to 0
## 95 percent confidence interval:
## -0.8907341 0.7774265
## sample estimates:
## mean in group no mean in group yes
## 30.65180 30.70845
library(effectsize)
##
## Attache Paket: 'effectsize'
## Das folgende Objekt ist maskiert 'package:psych':
##
## phi
## Das folgende Objekt ist maskiert 'package:robustHD':
##
## standardize
effectsize::cohens_d(mydata$bmi ~ mydata$smoker,
pooled_sd = FALSE)
## Cohen's d | 95% CI
## -------------------------
## -9.16e-03 | [-0.14, 0.13]
##
## - Estimated using un-pooled SD.
interpret_cohens_d(-9.16e-03, rules = "sawilowsky2009")
## [1] "tiny"
## (Rules: sawilowsky2009)
library(rstatix)
## Warning: Paket 'rstatix' wurde unter R Version 4.3.2 erstellt
##
## Attache Paket: 'rstatix'
## Die folgenden Objekte sind maskiert von 'package:effectsize':
##
## cohens_d, eta_squared
## Das folgende Objekt ist maskiert 'package:stats':
##
## filter
mydata %>%
group_by(smoker) %>%
shapiro_test(bmi)
## # A tibble: 2 × 4
## smoker variable statistic p
## <chr> <chr> <dbl> <dbl>
## 1 no bmi 0.994 0.000422
## 2 yes bmi 0.991 0.0852
wilcox.test(mydata$bmi ~ mydata$smoker,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "two.sided")
##
## Wilcoxon rank sum test
##
## data: mydata$bmi by mydata$smoker
## W = 145309, p-value = 0.9358
## alternative hypothesis: true location shift is not equal to 0
library(effectsize)
effectsize(wilcox.test(mydata$bmi ~ mydata$smoker,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "two.sided"))
## r (rank biserial) | 95% CI
## ---------------------------------
## -3.15e-03 | [-0.08, 0.07]
interpret_rank_biserial(-3.15e-03)
## [1] "tiny"
## (Rules: funder2019)
mydata2 <- read.table("./Housing.csv", header=TRUE, sep=",", dec=".")
head(mydata2) #show first 6 rows
## price area bedrooms bathrooms stories mainroad guestroom basement hotwaterheating airconditioning parking prefarea
## 1 13300000 7420 4 2 3 yes no no no yes 2 yes
## 2 12250000 8960 4 4 4 yes no no no yes 3 no
## 3 12250000 9960 3 2 2 yes no yes no no 2 yes
## 4 12215000 7500 4 2 2 yes no yes no yes 3 yes
## 5 11410000 7420 4 1 2 yes yes yes no yes 2 no
## 6 10850000 7500 3 3 1 yes no yes no yes 2 yes
## furnishingstatus
## 1 furnished
## 2 furnished
## 3 semi-furnished
## 4 furnished
## 5 furnished
## 6 semi-furnished
tail(mydata2) #show last 6 rows
## price area bedrooms bathrooms stories mainroad guestroom basement hotwaterheating airconditioning parking
## 540 1855000 2990 2 1 1 no no no no no 1
## 541 1820000 3000 2 1 1 yes no yes no no 2
## 542 1767150 2400 3 1 1 no no no no no 0
## 543 1750000 3620 2 1 1 yes no no no no 0
## 544 1750000 2910 3 1 1 no no no no no 0
## 545 1750000 3850 3 1 2 yes no no no no 0
## prefarea furnishingstatus
## 540 no unfurnished
## 541 no unfurnished
## 542 no semi-furnished
## 543 no unfurnished
## 544 no furnished
## 545 no unfurnished
library(dplyr)
##
## Attache Paket: 'dplyr'
## Die folgenden Objekte sind maskiert von 'package:pastecs':
##
## first, last
## Das folgende Objekt ist maskiert 'package:car':
##
## recode
## Die folgenden Objekte sind maskiert von 'package:stats':
##
## filter, lag
## Die folgenden Objekte sind maskiert von 'package:base':
##
## intersect, setdiff, setequal, union
# Adding ID to dataframe, to exclude the IDs that are outliers or have high impact later on in the analysis.
mydata2 <- mydata2 %>%
mutate(ID = 1:n())
head(mydata2)
## price area bedrooms bathrooms stories mainroad guestroom basement hotwaterheating airconditioning parking prefarea
## 1 13300000 7420 4 2 3 yes no no no yes 2 yes
## 2 12250000 8960 4 4 4 yes no no no yes 3 no
## 3 12250000 9960 3 2 2 yes no yes no no 2 yes
## 4 12215000 7500 4 2 2 yes no yes no yes 3 yes
## 5 11410000 7420 4 1 2 yes yes yes no yes 2 no
## 6 10850000 7500 3 3 1 yes no yes no yes 2 yes
## furnishingstatus ID
## 1 furnished 1
## 2 furnished 2
## 3 semi-furnished 3
## 4 furnished 4
## 5 furnished 5
## 6 semi-furnished 6
library(pastecs)
round(stat.desc(mydata2[, -c(6, 7, 8, 9, 10, 12, 13, 14, 15, 16)]), 2) #only show numerical variables
## price area bedrooms bathrooms stories parking
## nbr.val 5.450000e+02 545.00 545.00 545.00 545.00 545.00
## nbr.null 0.000000e+00 0.00 0.00 0.00 0.00 299.00
## nbr.na 0.000000e+00 0.00 0.00 0.00 0.00 0.00
## min 1.750000e+06 1650.00 1.00 1.00 1.00 0.00
## max 1.330000e+07 16200.00 6.00 4.00 4.00 3.00
## range 1.155000e+07 14550.00 5.00 3.00 3.00 3.00
## sum 2.597867e+09 2807045.00 1616.00 701.00 984.00 378.00
## median 4.340000e+06 4600.00 3.00 1.00 2.00 0.00
## mean 4.766729e+06 5150.54 2.97 1.29 1.81 0.69
## SE.mean 8.012083e+04 92.96 0.03 0.02 0.04 0.04
## CI.mean.0.95 1.573841e+05 182.60 0.06 0.04 0.07 0.07
## var 3.498544e+12 4709512.06 0.54 0.25 0.75 0.74
## std.dev 1.870440e+06 2170.14 0.74 0.50 0.87 0.86
## coef.var 3.900000e-01 0.42 0.25 0.39 0.48 1.24
summary(mydata2)
## price area bedrooms bathrooms stories mainroad
## Min. : 1750000 Min. : 1650 Min. :1.000 Min. :1.000 Min. :1.000 Length:545
## 1st Qu.: 3430000 1st Qu.: 3600 1st Qu.:2.000 1st Qu.:1.000 1st Qu.:1.000 Class :character
## Median : 4340000 Median : 4600 Median :3.000 Median :1.000 Median :2.000 Mode :character
## Mean : 4766729 Mean : 5151 Mean :2.965 Mean :1.286 Mean :1.806
## 3rd Qu.: 5740000 3rd Qu.: 6360 3rd Qu.:3.000 3rd Qu.:2.000 3rd Qu.:2.000
## Max. :13300000 Max. :16200 Max. :6.000 Max. :4.000 Max. :4.000
## guestroom basement hotwaterheating airconditioning parking prefarea
## Length:545 Length:545 Length:545 Length:545 Min. :0.0000 Length:545
## Class :character Class :character Class :character Class :character 1st Qu.:0.0000 Class :character
## Mode :character Mode :character Mode :character Mode :character Median :0.0000 Mode :character
## Mean :0.6936
## 3rd Qu.:1.0000
## Max. :3.0000
## furnishingstatus ID
## Length:545 Min. : 1
## Class :character 1st Qu.:137
## Mode :character Median :273
## Mean :273
## 3rd Qu.:409
## Max. :545
library(car)
scatterplot(mydata2$price ~ mydata2$area,
smooth = FALSE,
boxplots = FALSE,
ylab = "price in US-Dollar",
xlab = "area in square meters") #show relationship between price and area
#lm model with dependent and explanatory variables (no Dummys)
fit1 <- lm(price ~ area + bedrooms + bathrooms + stories + parking,
data = mydata2)
mydata2$StdResid <- round(rstandard(fit1), 3) #Standardized residuals for outliers
mydata2$CooksD <- round(cooks.distance(fit1), 3) #Cooks distances for high impact
hist(mydata2$StdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals")
shapiro.test(mydata2$StdResid) #not too important because of sample size
##
## Shapiro-Wilk normality test
##
## data: mydata2$StdResid
## W = 0.96533, p-value = 4.813e-10
hist(mydata2$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histogram of Cooks distances")
head(mydata2[order(mydata2$StdResid), c("ID", "StdResid")], 10) #ten units with lowest value of Cooks distance
## ID StdResid
## 500 500 -2.775
## 402 402 -2.733
## 248 248 -2.629
## 453 453 -2.565
## 404 404 -2.316
## 332 332 -2.266
## 230 230 -2.248
## 176 176 -2.137
## 384 384 -2.074
## 510 510 -2.051
head(mydata2[order(-mydata2$StdResid), c("ID", "StdResid")], 10) #ten units with highest value of stand.
## ID StdResid
## 1 1 4.567
## 5 5 4.403
## 4 4 3.820
## 3 3 3.624
## 21 21 3.211
## 9 9 2.976
## 16 16 2.914
## 23 23 2.875
## 14 14 2.784
## 6 6 2.703
head(mydata2[order(-mydata2$CooksD), c("ID", "CooksD")], 10) #ten units with highest value of Cooks distance
## ID CooksD
## 4 4 0.045
## 6 6 0.043
## 5 5 0.042
## 500 500 0.042
## 248 248 0.041
## 1 1 0.039
## 404 404 0.036
## 3 3 0.032
## 402 402 0.026
## 126 126 0.024
library(dplyr)
mydataC <- mydata2 %>%
filter(!(ID %in% c(1, 3, 4, 5, 6, 9, 14, 16, 21, 248, 404, 500))) #removing outliers and units with high impact
vif(fit1) #check multicolinearity
## area bedrooms bathrooms stories parking
## 1.170959 1.316597 1.252775 1.255202 1.164172
mean(vif(fit1))
## [1] 1.231941
fit1 <- lm(price ~ area + bedrooms + bathrooms + stories + parking,
data = mydataC)
mydataC$StdFitted <- scale(fit1$fitted.values)
library(car)
scatterplot(StdResid ~ StdFitted,
ylab = "Standardized residuals",
xlab = "Standardized fitted values",
smooth = FALSE,
regLine = FALSE,
boxplots = FALSE,
data = mydataC)
library(olsrr)
## Warning: Paket 'olsrr' wurde unter R Version 4.3.2 erstellt
##
## Attache Paket: 'olsrr'
## Das folgende Objekt ist maskiert 'package:datasets':
##
## rivers
ols_test_breusch_pagan(fit1) #check homoskedasticity
##
## Breusch Pagan Test for Heteroskedasticity
## -----------------------------------------
## Ho: the variance is constant
## Ha: the variance is not constant
##
## Data
## ---------------------------------
## Response : price
## Variables: fitted values of price
##
## Test Summary
## -------------------------------
## DF = 1
## Chi2 = 40.42605
## Prob > Chi2 = 2.042006e-10
library(estimatr)
## Warning: Paket 'estimatr' wurde unter R Version 4.3.2 erstellt
fit1 <- lm_robust(price ~ area + bedrooms + bathrooms + stories + parking,
se_type = "HC1",
data = mydataC)
summary(fit1) #Results of regression model
##
## Call:
## lm_robust(formula = price ~ area + bedrooms + bathrooms + stories +
## parking, data = mydataC, se_type = "HC1")
##
## Standard error type: HC1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF
## (Intercept) 75642.5 206761.50 0.3658 7.146e-01 -330535.5 481820.4 527
## area 334.6 27.23 12.2881 1.078e-30 281.1 388.1 527
## bedrooms 90522.4 74469.46 1.2156 2.247e-01 -55771.0 236815.9 527
## bathrooms 1124618.3 128287.45 8.7664 2.543e-17 872600.7 1376635.9 527
## stories 567701.8 57558.71 9.8630 3.589e-21 454629.1 680774.5 527
## parking 243858.5 60837.67 4.0083 6.996e-05 124344.4 363372.7 527
##
## Multiple R-squared: 0.5955 , Adjusted R-squared: 0.5917
## F-statistic: 153.2 on 5 and 527 DF, p-value: < 2.2e-16
sqrt(summary(fit1)$r.squared) #Coefficient of multiple correlation
## [1] 0.7717074
#model to inlcude dummy variables
fit2 <- lm_robust(price ~ area + bedrooms + bathrooms + stories + parking + mainroad + guestroom + basement + hotwaterheating + airconditioning + prefarea + furnishingstatus,
se_type = "HC1",
data = mydataC)
summary(fit2)
##
## Call:
## lm_robust(formula = price ~ area + bedrooms + bathrooms + stories +
## parking + mainroad + guestroom + basement + hotwaterheating +
## airconditioning + prefarea + furnishingstatus, data = mydataC,
## se_type = "HC1")
##
## Standard error type: HC1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF
## (Intercept) 198544 229976.08 0.8633 3.884e-01 -253254 650342 519
## area 249 25.96 9.5900 3.670e-20 198 300 519
## bedrooms 59246 63225.15 0.9371 3.492e-01 -64962 183455 519
## bathrooms 1006324 117071.65 8.5958 9.778e-17 776331 1236316 519
## stories 454941 58449.14 7.7835 3.846e-14 340115 569767 519
## parking 170779 54240.32 3.1486 1.735e-03 64221 277336 519
## mainroadyes 395574 100401.38 3.9399 9.266e-05 198331 592817 519
## guestroomyes 411145 131377.06 3.1295 1.850e-03 153049 669241 519
## basementyes 226691 104731.62 2.1645 3.088e-02 20941 432441 519
## hotwaterheatingyes 623584 252984.50 2.4649 1.403e-02 126584 1120583 519
## airconditioningyes 813791 101660.98 8.0049 7.885e-15 614073 1013508 519
## prefareayes 568302 110077.25 5.1628 3.473e-07 352050 784554 519
## furnishingstatussemi-furnished 14035 105516.55 0.1330 8.942e-01 -193257 221327 519
## furnishingstatusunfurnished -302828 114043.36 -2.6554 8.165e-03 -526872 -78785 519
##
## Multiple R-squared: 0.7102 , Adjusted R-squared: 0.703
## F-statistic: 89.44 on 13 and 519 DF, p-value: < 2.2e-16
71,02 % of variablitiy of price can be explained by linear effect of the size (area) of an house, the amount of bedrooms, the amount of bathrooms, the amount of stories, the amount of parking spaces and the dummy variables mainroad, guestrooms,basement, hotwaterheating, airconditioning, prefarea and status of furnishing.
If the area of the house increases by one squaremeter, the price of the house on average increases by 249 US-Dollars (p<0,001), assuming that all other variables stay the same.
If the bathrooms of the house increase by one room, the price of the house on average increases by 1006324 US-Dollar (p<0,001), assuming that all other variables stay the same.
Given the values of all exp. variables, the houses with a basement cost on average 226691 US-Dollar more compared to the houses without a basement (p=0,031).
Given the values of all explanatory variables, the houses with airconditioning cost on average 813791 US-Dollar more than houses without airconditioning (p<0,001).
Given the values of all explanatory variables, the houses without furnishing cost on average 302828 US-Dollar less than furnished houses (p<0,009).
Based on the p-values, we can not explain the variables bedrooms and the Dummy variable furnishingstatus “semi-furnished” compared to “furnished”.
library(lmtest)
## Warning: Paket 'lmtest' wurde unter R Version 4.3.2 erstellt
## Lade nötiges Paket: zoo
## Warning: Paket 'zoo' wurde unter R Version 4.3.2 erstellt
##
## Attache Paket: 'zoo'
## Die folgenden Objekte sind maskiert von 'package:base':
##
## as.Date, as.Date.numeric
waldtest(fit1, fit2, test = "F")
## Wald test
##
## Model 1: price ~ area + bedrooms + bathrooms + stories + parking
## Model 2: price ~ area + bedrooms + bathrooms + stories + parking + mainroad +
## guestroom + basement + hotwaterheating + airconditioning +
## prefarea + furnishingstatus
## Res.Df Df F Pr(>F)
## 1 527
## 2 519 8 27.329 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
We can reject H0 at p<0,001 and conclude that model 2 with all the dummy variables fits the data better than model 1.
In other words, as expected, the explanatory variables area, bathrooms, stories and parking lead to a higher price when being increased. Additionally, a basement, guestroom, access to mainroad, airconditioning, hotwaterheating and a house in a preferred area lead to a higher price. Buying an unfurnished house is cheaper than buying a furnished house.