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
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
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
shapiro.test(mydata$living_area_sqft)
##
## Shapiro-Wilk normality test
##
## data: mydata$living_area_sqft
## W = 0.97407, p-value = 0.3362
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
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
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
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
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
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
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)