Anze Ambrozic

Homework 2

Research questions:

  1. Is there any relationship between gender and the decision to purchase a product?

  2. Is there any linear correlation between Age and Estimated Salary?

mydata <- read.table("./Social_Network_Ads.csv", header=TRUE, sep=",")
head(mydata)
##    User.ID Gender Age EstimatedSalary Purchased
## 1 15624510   Male  19           19000         0
## 2 15810944   Male  35           20000         0
## 3 15668575 Female  26           43000         0
## 4 15603246 Female  27           57000         0
## 5 15804002   Male  19           76000         0
## 6 15728773   Male  27           58000         0

Unit of observation: 1 person Size of population is 400 people. Since the number of units is 400, I will not do a sample. Variables: User.ID - Specific ID of a person Gender - Gender of a person Age - age of a person EstimatedSalary - Estimation of the salary Purchased - Number representing if a person bought a product (0-NO, 1, YES) Unit of measurements: Age - years, EstimatedSalary - Euros

The data set was taken from Kaggle.com (Social Network Ads).

mydata$GenderF <- factor(mydata$Gender, 
                                levels = c("Male", "Female"), 
                                labels = c(0, 1))
mydata$PurchasedF <- factor(mydata$Purchased, 
                                levels = c(0, 1), 
                                labels = c("No", "Yes"))
library(tidyr)
mydata <- drop_na(mydata) #Removing units with missing values.

1. RESEARCH QUESTION

Is there any relationship between gender and the decision about purchasing a product.

Assumptions: 1. Observation must be independent 2. All expected frequencies are greater than 5 3. In larger contingency tables, up to 20% of the expected frequencies can be between 1 and 5 but this will reduce the power of the test.

Pearson Chi2 test

H0: There is no association between the two categorical variables. H1: There is association between the two categorical variables.

results <- chisq.test(mydata$Gender, mydata$PurchasedF, 
                      correct = TRUE)

results
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  mydata$Gender and mydata$PurchasedF
## X-squared = 0.55509, df = 1, p-value = 0.4562

P-VALUE is 45.62%, since P value is higher than 5%, we cannot reject H0. I assume there is no association between two categorical variables.

library(psych)
describe(mydata)
##                 vars   n        mean       sd   median     trimmed      mad
## User.ID            1 400 15691539.76 71658.32 15694342 15691547.24 92713.65
## Gender*            2 400        1.49     0.50        1        1.49     0.00
## Age                3 400       37.66    10.48       37       37.32    11.86
## EstimatedSalary    4 400    69742.50 34096.96    70000    67421.88 31134.60
## Purchased          5 400        0.36     0.48        0        0.32     0.00
## GenderF*           6 400        1.51     0.50        2        1.51     0.00
## PurchasedF*        7 400        1.36     0.48        1        1.32     0.00
##                      min      max  range  skew kurtosis      se
## User.ID         15566689 15815236 248547 -0.03    -1.19 3582.92
## Gender*                1        2      1  0.04    -2.00    0.03
## Age                   18       60     42  0.23    -0.64    0.52
## EstimatedSalary    15000   150000 135000  0.49    -0.43 1704.85
## Purchased              0        1      1  0.59    -1.65    0.02
## GenderF*               1        2      1 -0.04    -2.00    0.03
## PurchasedF*            1        2      1  0.59    -1.65    0.02
round(results$res, 2)
##              mydata$PurchasedF
## mydata$Gender    No   Yes
##        Female -0.36  0.48
##        Male    0.36 -0.49

Number (Female, Yes) = 0.48 Number 0.48 means observed frequencies are a bit higher than expected frequencies.

addmargins(results$observed)
##              mydata$PurchasedF
## mydata$Gender  No Yes Sum
##        Female 127  77 204
##        Male   130  66 196
##        Sum    257 143 400
round(results$expected, 2)
##              mydata$PurchasedF
## mydata$Gender     No   Yes
##        Female 131.07 72.93
##        Male   125.93 70.07

All expected frequencis are higher than 5, so second and the third assumptions are met.

addmargins(round(prop.table(results$observed), 3))
##              mydata$PurchasedF
## mydata$Gender    No   Yes   Sum
##        Female 0.318 0.192 0.510
##        Male   0.325 0.165 0.490
##        Sum    0.643 0.357 1.000

Explanation of Female,Yes - 0.192 Out of 500 students there are 19.2% students that are Females and decided to buy a product.

addmargins(round(prop.table(results$observed, 1), 3), 2) 
##              mydata$PurchasedF
## mydata$Gender    No   Yes   Sum
##        Female 0.623 0.377 1.000
##        Male   0.663 0.337 1.000

Out of all Females, 37.7% decided to buy a product.

addmargins(round(prop.table(results$observed, 2), 3), 1) 
##              mydata$PurchasedF
## mydata$Gender    No   Yes
##        Female 0.494 0.538
##        Male   0.506 0.462
##        Sum    1.000 1.000

Out of all people that purchased a product, 53.8% were Female.

library(effectsize)
## 
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
## 
##     phi
effectsize::cramers_v(mydata$Gender, mydata$PurchasedF)
## Cramer's V (adj.) |       95% CI
## --------------------------------
## 0.00              | [0.00, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.00)
## [1] "tiny"
## (Rules: funder2019)

Effectsize is 0.00, that means tiny.

oddsratio(mydata$Gender, mydata$PurchasedF)
## Odds ratio |       95% CI
## -------------------------
## 0.84       | [0.56, 1.26]
interpret_oddsratio(0.84)
## [1] "very small"
## (Rules: chen2010)

The ratio of 0.84 means that odds of purchasing are 0.84 times lower for females comapared to males.

CONCLUSION: Based on the data, I found that there is no assosiation between Gender and the decision for the pruchase of a product. Since P-value was higher than 5% (P-vlaue= 45.62%), I cannot reject H0 (There is no assosiation between two categorical variables).

Fisher‘s Exactxact Probability Test of Independence

We use this non-parametric test in case any of the assumptions cannot be met.

H0= Odds ratio is equal to 1. H1= Odds ratio is not equal to 1.

fisher.test(mydata$Gender, mydata$PurchasedF)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  mydata$Gender and mydata$PurchasedF
## p-value = 0.4058
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
##  0.5441175 1.2876336
## sample estimates:
## odds ratio 
##  0.8377376

P value is 0.4058, so we cannot reject H0 = odds ratio is equal to 1. We have confirmed again that there is no assosiation between two categroical variables (Gender, Decision for Purchase)

2. RESEARCH QUESTION

Is there any linear correlation between Age and Estimated Salary?

mydataA <- mydata [, c(1, 3, 4)]
head(mydataA)
##    User.ID Age EstimatedSalary
## 1 15624510  19           19000
## 2 15810944  35           20000
## 3 15668575  26           43000
## 4 15603246  27           57000
## 5 15804002  19           76000
## 6 15728773  27           58000
describe(mydataA)
##                 vars   n        mean       sd   median     trimmed      mad
## User.ID            1 400 15691539.76 71658.32 15694342 15691547.24 92713.65
## Age                2 400       37.66    10.48       37       37.32    11.86
## EstimatedSalary    3 400    69742.50 34096.96    70000    67421.88 31134.60
##                      min      max  range  skew kurtosis      se
## User.ID         15566689 15815236 248547 -0.03    -1.19 3582.92
## Age                   18       60     42  0.23    -0.64    0.52
## EstimatedSalary    15000   150000 135000  0.49    -0.43 1704.85
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
scatterplotMatrix(mydataA[ , -1], smooth=FALSE)

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(mydataA[ , -1]), 
      type = "pearson")
##                  Age EstimatedSalary
## Age             1.00            0.16
## EstimatedSalary 0.16            1.00
## 
## n= 400 
## 
## 
## P
##                 Age    EstimatedSalary
## Age                    0.0018         
## EstimatedSalary 0.0018

Number 0.16 means that there is a positive lienar relationship between Age and Estimated Salary, but correlation is weak.

library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
ggplot(mydataA, aes(x = Age, y = EstimatedSalary)) +
  geom_point()

cor(mydataA$Age, mydataA$EstimatedSalary,
    method = "pearson",
    use = "complete.obs")
## [1] 0.155238

Number 0.155 shows positive but weak correlation between Age and Estimated Salary.

cor.test(mydataA$Age, mydataA$EstimatedSalary,
         method = "pearson",
         use = "complete.obs")
## 
##  Pearson's product-moment correlation
## 
## data:  mydataA$Age and mydataA$EstimatedSalary
## t = 3.135, df = 398, p-value = 0.001846
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.05807021 0.24949213
## sample estimates:
##      cor 
## 0.155238

H0:The correlation is equal to 0. H1:The correlation is not equal to 0.

The P-value is 0.0018, which is less than 5%, that means that I can reject the null hypothesis.

CONCLUSION: Based on the data I conclude that there is linear relationship between Age and Estimated salary, the calculated P-Value is less than 5% (P-value - 0.0018). The calculated correlation is positive but very weak, r is 0.155.