Homework 2

Saša Kočar

Data set 1

This data set was retrieved from Kaggle.com. House Sales in King County, USA (https://www.kaggle.com/datasets/harlfoxem/housesalesprediction)

data <- read.csv("kc_house_data.csv") 
mydata <- data[, -c(7:21)]
library(psych)
colnames(mydata) <- c("house_id","sale_date","sale_price","num_bedrooms","num_bathrooms","living_area_sqft")
# Sampling 50 units
set.seed(1) # Setting initial point of sampling
mydata <- mydata[sample(nrow(mydata), 50), ]
head(mydata, 10)
##         house_id       sale_date sale_price num_bedrooms num_bathrooms
## 17401 7525410190 20140502T000000     550000            3          1.75
## 4775  2621400080 20150128T000000     275000            4          2.50
## 13218 9826701490 20150225T000000     455000            5          2.00
## 10539  253600160 20140530T000000     384950            3          2.50
## 8462  4006000401 20140811T000000     140000            2          1.00
## 4050  8927600100 20140528T000000     925000            3          2.50
## 13499 1624059219 20141105T000000     850000            2          2.00
## 11571 3888100117 20141110T000000     510000            5          1.50
## 12257 7511000140 20140808T000000     994000            4          2.50
## 17685 2968801240 20140512T000000     211000            3          1.50
##       living_area_sqft
## 17401             2910
## 4775              2120
## 13218             1510
## 10539             1860
## 8462               900
## 4050              2690
## 13499             2640
## 11571             1550
## 12257             3470
## 17685             1350

Description of data

Unit of Observation: Sold House
Sample Size: 21613 (I took a random sample of 50 units)
id: A unique identifier for each house sold.
date: The date on which the house was sold.
price: The price at which the house was sold.
bedrooms: The number of bedrooms in the house.
bathrooms: The number of bathrooms in the house.
sqft_living: The square footage of the living area of the house.

RQ1: Is there a correlation between the square footage of the living area of a house and its sale price?

Descriptive statistics

summary(mydata[ , c( -1, -2)])
##    sale_price       num_bedrooms  num_bathrooms   living_area_sqft
##  Min.   : 140000   Min.   :2.00   Min.   :1.000   Min.   : 750    
##  1st Qu.: 385238   1st Qu.:3.00   1st Qu.:1.750   1st Qu.:1430    
##  Median : 507500   Median :3.00   Median :2.250   Median :2060    
##  Mean   : 566267   Mean   :3.52   Mean   :2.145   Mean   :2112    
##  3rd Qu.: 736500   3rd Qu.:4.00   3rd Qu.:2.500   3rd Qu.:2698    
##  Max.   :1339000   Max.   :6.00   Max.   :3.500   Max.   :4240

Interpretation:

Sale price: The minimum sale price was 140,000 and the maximum sale price was 1,339,000. The first quartile of sale prices is 385,238, which means that 25% of the properties sold for less than this amount. The median sale price is $507,500, which means that 50% of the properties sold for less than this amount and 50% for more than that amount.
Number of bedrooms: The minimum number of bedrooms is 2 and the maximum number of bedrooms is 6. 50% of the properties have less than 3 bedrooms and 50% more than 3 bedrooms. The average number of bedrooms is approximately 3.52.
Number of bathrooms: The minimum number of bathrooms is 1 and the maximum number of bathrooms is 3.5. The first quartile of the number of bathrooms is 1.75, which means that 25% of the properties have less than 1.75 bathrooms.
Living area in square feet: The minimum living area is 750 square feet and the maximum living area is 4240 square feet. The median living area is 2060 square feet, which means that 50% of the properties have less than 2060 square feet of living area and 50% have more than that.
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
scatterplotMatrix(mydata[ , c( -1, -2, -4, -5)], smooth = FALSE)

To make sure the distribution of variables is normal, we will perform shapiro wilk test for normality.

shapiro.test(mydata$sale_price) 
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata$sale_price
## W = 0.92838, p-value = 0.004799

H0: The respective variable is normally distributed.

H1: The respective variable is not normally distributed.

BSD we reject null hypothesis (p<0.01).

shapiro.test(mydata$living_area_sqft)
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata$living_area_sqft
## W = 0.97407, p-value = 0.3362

H0: The respective variable is normally distributed.

H1: The respective variable is not normally distributed.

BSD we can not reject null hypothesis. (p = 0.34)

Because distribution of 1 variable was not normal, we will use Spearman correlation.

library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following object is masked from 'package:psych':
## 
##     describe
## The following objects are masked from 'package:base':
## 
##     format.pval, units
rcorr(as.matrix(mydata[ , c( -1, -2, -4, -5)]),
      type = "spearman") 
##                  sale_price living_area_sqft
## sale_price             1.00             0.67
## living_area_sqft       0.67             1.00
## 
## n= 50 
## 
## 
## P
##                  sale_price living_area_sqft
## sale_price                   0              
## living_area_sqft  0
r = 0,67 –> There is a semi strong positive linear relationship between sale price and living area sqft.
H0: population correlation coefficient between sale price and living area sqft = 0
H1: population correlation coefficient between sale price and living area sqft not equal 0
We reject H0 at p< 0 ,001. There is a semi strong positive linear correlation between sale price and living area sqft.

Data set 2

This data set was retrieved from Kaggle.com. Heart Disease Cleveland UCI. (https://www.kaggle.com/datasets/cherngs/heart-disease-cleveland-uci/data)

mydata1 <- read.csv("heart_cleveland_upload.csv")
mydata2 <- mydata1[ , c(-1, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13)] #Deleting columns that are not relevant for my research
head(mydata2)
##   sex condition
## 1   1         0
## 2   0         0
## 3   0         0
## 4   1         1
## 5   1         0
## 6   1         0
library(psych)
colnames(mydata2) <- c("Sex", "Condition")
mydata2$SexFactor <- factor(mydata2$Sex,
                       levels = c(0, 1),
                       labels = c("Female", "Male"))
mydata2$ConditionFactor <- factor(mydata2$Condition,
                             levels = c(0, 1),
                             labels = c("No_Disease", "Disease"))
head(mydata2, 5)
##   Sex Condition SexFactor ConditionFactor
## 1   1         0      Male      No_Disease
## 2   0         0    Female      No_Disease
## 3   0         0    Female      No_Disease
## 4   1         1      Male         Disease
## 5   1         0      Male      No_Disease

Description of data

Unit of Observation: Patient
Sample Size: 297
Age: Age of the patient in years (Numeric)
Sex: Gender of the patient (0 = Female, 1 = Male) (Categorical)
Resting blood pressure: resting blood pressure in mm Hg on admission to the hospital (Numeric)
Cholesterol: serum cholestoral in mg/dl (Numeric)
Max Heart Rate: Maximum heart rate achieved (Numeric)
Condition: Presence of heart disease (0 = No, 1 = Yes) (Categorical)

RQ2: Does presence of heart disease vary depending on the gender(sex) of patient?

Assumptions:

1. Observations must be independent.

2. Check that all expected frequencies are greater than 5.

3. In larger contingency tables (at least one categorical variable has more than two categories), up to 20% of the expected frequencies can be between 1 and 5, but this will reduce the power of the test. (doesnt apply for 2x2 contigency tables - also my case)

results <- chisq.test(mydata2$SexFactor, mydata2$ConditionFactor,
                      correct = TRUE)
results
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  mydata2$SexFactor and mydata2$ConditionFactor
## X-squared = 21.852, df = 1, p-value = 2.946e-06

H0: There is no association between categorical variables sex and condition.

H1: There is association between categorical variables sex and condition.

BSD we reject null hypothesis (p<0.001) and conclude that there is association between categorical variables sex and condition.

addmargins(results$observed) #empirical frequencies
##                  mydata2$ConditionFactor
## mydata2$SexFactor No_Disease Disease Sum
##            Female         71      25  96
##            Male           89     112 201
##            Sum           160     137 297
round(results$expected, 2) #expected frequencies
##                  mydata2$ConditionFactor
## mydata2$SexFactor No_Disease Disease
##            Female      51.72   44.28
##            Male       108.28   92.72
Here we have to check that all expected frequencies are greater than 5. Since all are greater than 5,we can confirm our assumption is met.
round(results$res, 2)
##                  mydata2$ConditionFactor
## mydata2$SexFactor No_Disease Disease
##            Female       2.68   -2.90
##            Male        -1.85    2.00
There is more than expected number of patients in category female and no disease (a= 0.01)
There is less than expected number of patients in category female and disease (a = 0.01).
There is more than expected number of patients in category male and disease (a = 0.05)
Difference for category male and no disease is statistically not significant.
addmargins(round(prop.table(results$observed), 3))
##                  mydata2$ConditionFactor
## mydata2$SexFactor No_Disease Disease   Sum
##            Female      0.239   0.084 0.323
##            Male        0.300   0.377 0.677
##            Sum         0.539   0.461 1.000
Interpretation:
Out of 297 patients, there is 23.9% of them female and without disease. There is 30% of them male and without disease. There is 8,4% female with disease and 37.7% male with disease. There is 53.9% of all patients without disease and 46.1% of all patients with disease.
addmargins(round(prop.table(results$observed, 1), 3), 2)
##                  mydata2$ConditionFactor
## mydata2$SexFactor No_Disease Disease   Sum
##            Female      0.740   0.260 1.000
##            Male        0.443   0.557 1.000
Interpretation:
26% of all female have disease and 74% of female do not have disease. 55.7% of all men have disease and 44.3% of men do not have disese.
addmargins(round(prop.table(results$observed, 2), 3), 1)
##                  mydata2$ConditionFactor
## mydata2$SexFactor No_Disease Disease
##            Female      0.444   0.182
##            Male        0.556   0.818
##            Sum         1.000   1.000
Interpretation:
out of all patients that have disease, only 18.2% are female and 81.8% are male. Out of all people without disease 44.4% are female and 55.6% are male.
library(effectsize)
## 
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
## 
##     phi
effectsize::cramers_v(mydata2$SexFactor, mydata2$ConditionFactor)
## Cramer's V (adj.) |       95% CI
## --------------------------------
## 0.27              | [0.17, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.27)
## [1] "medium"
## (Rules: funder2019)

BSD we can conclude that there is association between categorical variables gender and condition and the effect is medium.