#Loading packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#Importing data
url <- url("https://raw.githubusercontent.com/drahmedshaheen/data/main/depression.csv")
depression <- read.csv(url) %>% janitor::clean_names() %>%
mutate_if(is.character, ~as.factor(as.character(.)))
str(depression)
## 'data.frame': 1301 obs. of 36 variables:
## $ a : Factor w/ 1293 levels "1/17/2021 1:28:57",..: 1167 1170 1168 1169 1171 1172 1173 1174 1175 1176 ...
## $ year : int 4 4 4 4 2 4 6 2 6 4 ...
## $ university : Factor w/ 7 levels "","Ain Shams University Faculty of Medicine",..: 4 4 4 4 4 4 4 4 4 4 ...
## $ sex : Factor w/ 2 levels "Female","Male": 2 2 2 1 1 1 1 2 1 1 ...
## $ academic_performance : Factor w/ 4 levels "","Negative effect",..: 4 4 3 2 3 4 2 2 3 4 ...
## $ relationship_with_your_family: Factor w/ 4 levels "","Negative effect",..: 4 4 4 4 3 2 2 3 4 2 ...
## $ social_relationships : Factor w/ 6 levels "","Negative",..: 2 2 5 2 5 2 2 2 2 2 ...
## $ eating_habits : Factor w/ 3 levels "","No","Yes": 2 2 3 3 2 2 2 2 3 2 ...
## $ eating_habits_how : Factor w/ 9 levels "","both amount and type",..: 8 8 9 9 1 8 1 8 2 1 ...
## $ gained_weight : Factor w/ 3 levels "","No","Yes": 3 3 2 3 2 3 1 2 3 3 ...
## $ smoking_habits : Factor w/ 9 levels "","I am a non-smoker",..: 2 2 2 7 2 2 2 2 2 2 ...
## $ sleeping_pattern : Factor w/ 6 levels "","disturbed sleeping pattern",..: 2 5 2 2 2 2 6 2 2 5 ...
## $ trouble_sleeping : Factor w/ 4 levels "","Maybe","No",..: 2 3 4 4 4 2 3 2 2 3 ...
## $ sleep_quality : Factor w/ 5 levels "","Fairly bad",..: 2 3 2 2 2 3 5 2 3 5 ...
## $ physical_activity : Factor w/ 7 levels "","I have been less physically active.",..: 4 5 4 4 6 4 4 4 4 4 ...
## $ x1 : int 0 2 1 2 2 1 2 2 3 0 ...
## $ x2 : int 1 1 1 0 2 1 1 0 2 0 ...
## $ x3 : int 2 2 1 3 2 1 2 3 1 0 ...
## $ x4 : int 1 0 1 3 1 0 1 2 0 0 ...
## $ x5 : int 2 1 1 2 1 1 3 3 1 2 ...
## $ x6 : int 3 1 1 1 2 1 2 1 2 2 ...
## $ x7 : int 0 1 2 1 1 0 0 1 1 0 ...
## $ x8 : int 2 0 2 1 2 1 1 1 1 1 ...
## $ x9 : int 0 1 1 3 2 0 1 2 1 1 ...
## $ x10 : int 0 1 1 1 2 0 3 3 3 2 ...
## $ x11 : int 1 1 1 1 2 1 2 3 3 1 ...
## $ x12 : int 0 2 1 3 2 1 3 3 2 0 ...
## $ x13 : int 2 0 1 2 2 1 1 3 3 0 ...
## $ x14 : int 0 1 1 0 1 0 2 1 0 1 ...
## $ x15 : int 0 1 1 2 2 0 1 1 1 0 ...
## $ x16 : int 0 1 1 2 2 0 0 2 3 2 ...
## $ x17 : int 0 1 1 1 2 2 1 3 0 0 ...
## $ x18 : int 3 2 1 3 2 1 2 1 3 1 ...
## $ x19 : int 2 1 1 2 3 0 1 2 0 0 ...
## $ x20 : int 0 1 1 1 3 0 1 2 0 0 ...
## $ x21 : int 0 0 1 1 2 0 2 3 0 0 ...
#View(depression)
names(depression)
## [1] "a" "year"
## [3] "university" "sex"
## [5] "academic_performance" "relationship_with_your_family"
## [7] "social_relationships" "eating_habits"
## [9] "eating_habits_how" "gained_weight"
## [11] "smoking_habits" "sleeping_pattern"
## [13] "trouble_sleeping" "sleep_quality"
## [15] "physical_activity" "x1"
## [17] "x2" "x3"
## [19] "x4" "x5"
## [21] "x6" "x7"
## [23] "x8" "x9"
## [25] "x10" "x11"
## [27] "x12" "x13"
## [29] "x14" "x15"
## [31] "x16" "x17"
## [33] "x18" "x19"
## [35] "x20" "x21"
df <- data.frame(`First Name`=c("Ahmed","Noor"),
`Last Name`=c(123,4234))
head(df)
## First.Name Last.Name
## 1 Ahmed 123
## 2 Noor 4234
names(df)
## [1] "First.Name" "Last.Name"
# space, signs(?,!)
df$`money.in$` <- NA
names(df)
## [1] "First.Name" "Last.Name" "money.in$"
janitor::clean_names(df)
## first_name last_name money_in
## 1 Ahmed 123 NA
## 2 Noor 4234 NA
depression$year <- as.factor(as.character(depression$year))
levels(depression$year)
## [1] "1" "2" "3" "4" "5" "6"
#levels(depression$physical_activity)
depression$physical_activity <- factor(depression$physical_activity,
levels = c("I have been less physically active.","I have been more physically active.",
"Less physical activity", "More physical activity" , "No change" ,
"There has not been any change in my physical activity." ) ,
labels = c("Less physical activity","More physical activity",
"Less physical activity", "More physical activity" , "No change" ,
"No change" ))
levels(depression$physical_activity)
## [1] "Less physical activity" "More physical activity" "No change"
depression$sleeping_pattern <- factor(depression$sleeping_pattern,
levels = c( "disturbed sleeping pattern" ,
"Disturbed sleeping pattern",
"Less sleeping hours" ,
"More sleeping hours" ,
"No change" ) ,
labels = c("Disturbed sleeping pattern" ,
"Disturbed sleeping pattern",
"Less sleeping hours" ,
"More sleeping hours" ,
"No change" ))
depression$eating_habits_how <- factor(depression$eating_habits_how,
levels = c("both amount and type" ,"Change in both amount and type of food",
"Change in both the amount and type of food" ,"Change in the amount of food",
"Change in the type of food" , "changed amount of food" ,
"no change" , "type of food" ) ,
labels = c("both amount and type" ,"both amount and type",
"both amount and type" ,"amount of food",
"type of food" , "amount of food" ,
"no change" , "type of food" ))
depression$smoking_habits <- factor(depression$smoking_habits,
levels = c( "I am a non-smoker" ,
"I am a non-smoker." ,
"I am a smoker and my smoking habits did not change since the outreak" ,
"I am a smoker and my smoking habits have not change since the outbreak.",
"I am a smoker and reduced smoking during the pandemic." ,
"I started smoking during the pandemic" ,
"I started smoking during the pandemic." ,
"I stopped smoking during the pandemic." ) ,
labels = c("non-smoker" ,
"non-smoker" ,
"smoker with no change",
"smoker with no change",
"smoker with reduced smoking " ,
"started smoking" ,
"started smoking" ,
"stopped smoking" ))
levels(depression$smoking_habits)
## [1] "non-smoker" "smoker with no change"
## [3] "smoker with reduced smoking " "started smoking"
## [5] "stopped smoking"
head(depression[,c("x1","x2","x4")])
## x1 x2 x4
## 1 0 1 1
## 2 2 1 0
## 3 1 1 1
## 4 2 0 3
## 5 2 2 1
## 6 1 1 0
depression$anxiety <- c((depression$x2 + depression$x4 + depression$x7 + depression$x9 +
depression$x15+ depression$x19 + depression$x20)*2)
class(depression$anxiety)
## [1] "numeric"
summary(depression$anxiety)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00 6.00 14.00 15.04 22.00 42.00
depression$anxiety2 <- depression$anxiety
Normal= c(0:7)
Mild = c(8, 9)
Moderate = c(10:14)
Severe =c(15:19)
Extremely.Severe = c(20:42)
depression$anxiety[depression$anxiety %in% Normal] <- "Normal"
depression$anxiety[depression$anxiety %in% Mild] <- "Mild"
depression$anxiety[depression$anxiety %in% Moderate] <- "Moderate"
depression$anxiety[depression$anxiety %in% Severe] <- "Severe"
depression$anxiety[depression$anxiety %in% Extremely.Severe] <- "Extremely severe"
depression$anxiety <- as.factor(depression$anxiety)
dim(depression)
## [1] 1301 38
plot(depression$anxiety)
depression$deprss = c((depression$x3 + depression$x5 + depression$x10 + depression$x13 +
depression$x16 + depression$x17 + depression$x21)*2)
depression$deprss2 <- depression$deprss %>% as.numeric()
Normal= c(0, 0:9, 9)
Mild = c(10, 10:13, 13)
Moderate = c(14, 14:20, 20)
Severe =c(21, 21:27, 27)
Extremely.Severe = c(28, 28:42, 42)
depression$deprss[depression$deprss %in% Normal] <- "Normal"
depression$deprss[depression$deprss %in% Mild] <- "Mild"
depression$deprss[depression$deprss %in% Moderate] <- "Moderate"
depression$deprss[depression$deprss %in% Severe] <- "Severe"
depression$deprss[depression$deprss %in% Extremely.Severe] <- "Extremely.Severe"
depression$deprss <- as.factor(depression$deprss)
depression$stress = c((depression$x1 + depression$x6 + depression$x8 + depression$x11 + depression$x12 +
depression$x14 + depression$x18 )*2)
depression$stress2 <- depression$stress %>% as.numeric()
Normal= c(0, 0:14, 14)
Mild = c(15, 15:18, 18)
Moderate = c(19, 19:25, 25)
Severe =c(26, 26:33, 33)
Extremely.Severe = c(34, 34:42, 42)
depression$stress[depression$stress %in% Normal] <- "Normal"
depression$stress[depression$stress %in% Mild] <- "Mild"
depression$stress[depression$stress %in% Moderate] <- "Moderate"
depression$stress[depression$stress %in% Severe] <- "Severe"
depression$stress[depression$stress %in% Extremely.Severe] <- "Extremely.Severe"
depression$stress <- as.factor(depression$stress)
hist(depression$stress2)
library("ggpubr")
ggdensity(depression$anxiety2,
main = "Density plot of tooth length",
xlab = "Tooth length")
ggqqplot(depression$anxiety2)
shapiro.test(depression$anxiety2)
##
## Shapiro-Wilk normality test
##
## data: depression$anxiety2
## W = 0.95752, p-value < 2.2e-16
my_data = depression %>% select (stress2, deprss2, anxiety2, year, sex ,academic_performance,
relationship_with_your_family, social_relationships,
eating_habits, eating_habits_how ,gained_weight, smoking_habits,
sleeping_pattern, trouble_sleeping, sleep_quality,physical_activity) %>% as.data.frame()
my_data = my_data %>% mutate_all(as.numeric)
str(my_data)
## 'data.frame': 1301 obs. of 16 variables:
## $ stress2 : num 18 18 16 22 26 12 28 24 28 12 ...
## $ deprss2 : num 12 12 14 24 26 10 24 40 22 12 ...
## $ anxiety2 : num 8 12 16 24 28 2 12 20 10 2 ...
## $ year : num 4 4 4 4 2 4 6 2 6 4 ...
## $ sex : num 2 2 2 1 1 1 1 2 1 1 ...
## $ academic_performance : num 4 4 3 2 3 4 2 2 3 4 ...
## $ relationship_with_your_family: num 4 4 4 4 3 2 2 3 4 2 ...
## $ social_relationships : num 1 1 3 1 3 1 1 1 1 1 ...
## $ eating_habits : num 2 2 3 3 2 2 2 2 3 2 ...
## $ eating_habits_how : num 4 4 3 3 NA 4 NA 4 1 NA ...
## $ gained_weight : num 3 3 2 3 2 3 1 2 3 3 ...
## $ smoking_habits : num 1 1 1 4 1 1 1 1 1 1 ...
## $ sleeping_pattern : num 1 3 1 1 1 1 4 1 1 3 ...
## $ trouble_sleeping : num 2 3 4 4 4 2 3 2 2 3 ...
## $ sleep_quality : num 2 3 2 2 2 3 5 2 3 5 ...
## $ physical_activity : num 1 2 1 1 3 1 1 1 1 1 ...
matrix <- cor(my_data, use = "complete.obs", method = "spearman") %>% as.data.frame()
getwd()
## [1] "/Users/ahmedshaheen/Desktop/CourseFile/depression"
write.csv(matrix, file = "/Users/ahmedshaheen/Desktop/CourseFile/depression/matrix_cor.csv")
depression$stress <- ordered(depression$stress, levels = c("Normal", "Mild", "Moderate", "Severe","Extremely severe"))
depression$anxiety <- ordered(depression$anxiety, levels = c("Normal", "Mild", "Moderate", "Severe","Extremely severe"))
depression$deprss <- ordered(depression$deprss, levels = c("Normal", "Mild", "Moderate", "Severe","Extremely severe"))
fit.log = glm(deprss ~ year + academic_performance +trouble_sleeping+
social_relationships+ eating_habits_how+ smoking_habits+sleep_quality+physical_activity
, data = depression, family = binomial)
summary(fit.log)
##
## Call:
## glm(formula = deprss ~ year + academic_performance + trouble_sleeping +
## social_relationships + eating_habits_how + smoking_habits +
## sleep_quality + physical_activity, family = binomial, data = depression)
##
## Coefficients:
## Estimate Std. Error z value
## (Intercept) 0.63320 1.65729 0.382
## year2 0.35824 0.48289 0.742
## year3 0.81837 0.57008 1.436
## year4 0.13071 0.41712 0.313
## year5 0.02356 0.42051 0.056
## year6 -0.01175 0.50298 -0.023
## academic_performanceNegative effect 3.00324 1.53995 1.950
## academic_performanceNo change 3.01939 1.56433 1.930
## academic_performancePositive effect 2.28569 1.55926 1.466
## trouble_sleepingNo -0.64445 0.32974 -1.954
## trouble_sleepingYes 0.26509 0.42855 0.619
## social_relationshipsNo effect -0.92246 0.30678 -3.007
## social_relationshipsPositive effect -0.72524 0.37563 -1.931
## eating_habits_howamount of food 0.51430 0.31994 1.607
## eating_habits_howtype of food -0.10287 0.36891 -0.279
## eating_habits_howno change 1.32409 1.14384 1.158
## smoking_habitssmoker with no change -0.67522 0.59221 -1.140
## smoking_habitssmoker with reduced smoking 14.67687 828.12259 0.018
## smoking_habitsstarted smoking -0.69028 1.35035 -0.511
## smoking_habitsstopped smoking 13.25397 1018.80460 0.013
## sleep_qualityFairly good -1.48143 0.58458 -2.534
## sleep_qualityVery bad -0.97887 0.82231 -1.190
## sleep_qualityVery good -1.65806 0.65204 -2.543
## physical_activityMore physical activity -0.46821 0.34734 -1.348
## physical_activityNo change -1.19066 0.32816 -3.628
## Pr(>|z|)
## (Intercept) 0.702409
## year2 0.458168
## year3 0.151131
## year4 0.754008
## year5 0.955312
## year6 0.981360
## academic_performanceNegative effect 0.051150 .
## academic_performanceNo change 0.053589 .
## academic_performancePositive effect 0.142681
## trouble_sleepingNo 0.050649 .
## trouble_sleepingYes 0.536198
## social_relationshipsNo effect 0.002639 **
## social_relationshipsPositive effect 0.053518 .
## eating_habits_howamount of food 0.107945
## eating_habits_howtype of food 0.780358
## eating_habits_howno change 0.247033
## smoking_habitssmoker with no change 0.254217
## smoking_habitssmoker with reduced smoking 0.985860
## smoking_habitsstarted smoking 0.609223
## smoking_habitsstopped smoking 0.989620
## sleep_qualityFairly good 0.011271 *
## sleep_qualityVery bad 0.233895
## sleep_qualityVery good 0.010994 *
## physical_activityMore physical activity 0.177666
## physical_activityNo change 0.000285 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 451.59 on 465 degrees of freedom
## Residual deviance: 363.54 on 441 degrees of freedom
## (835 observations deleted due to missingness)
## AIC: 413.54
##
## Number of Fisher Scoring iterations: 14
library(epiDisplay)
## Loading required package: foreign
## Loading required package: survival
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## Loading required package: nnet
##
## Attaching package: 'epiDisplay'
## The following object is masked from 'package:ggplot2':
##
## alpha
logistic.display(fit.log)
##
## Logistic regression predicting deprss
##
## crude OR(95%CI)
## year: ref.=1
## 2 1.03 (0.46,2.34)
## 3 1.4 (0.55,3.59)
## 4 0.82 (0.42,1.63)
## 5 0.95 (0.46,1.93)
## 6 0.66 (0.28,1.52)
##
## academic_performance: ref.=
## Negative effect 12.1 (1.07,136.47)
## No change 7.5 (0.64,88.05)
## Positive effect 4.76 (0.42,54.56)
##
## trouble_sleeping: ref.=Maybe
## No 0.42 (0.25,0.72)
## Yes 2.06 (1,4.23)
##
## social_relationships: ref.=Negative effect
## No effect 0.33 (0.2,0.57)
## Positive effect 0.43 (0.23,0.82)
##
## eating_habits_how: ref.=both amount and type
## amount of food 1.62 (0.94,2.8)
## type of food 0.94 (0.5,1.74)
## no change 3.59 (0.46,28.17)
##
## smoking_habits: ref.=non-smoker
## smoker with no change 0.49 (0.18,1.34)
## smoker with reduced smoking 1313754.48 (0,Inf)
## started smoking 1.14 (0.13,9.9)
## stopped smoking 1313754.48 (0,Inf)
##
## sleep_quality: ref.=Fairly bad
## Fairly good 0.16 (0.05,0.44)
## Very bad 0.32 (0.08,1.37)
## Very good 0.09 (0.03,0.28)
##
## physical_activity: ref.=Less physical activity
## More physical activity 0.39 (0.21,0.72)
## No change 0.23 (0.13,0.4)
##
## adj. OR(95%CI)
## year: ref.=1
## 2 1.43 (0.56,3.69)
## 3 2.27 (0.74,6.93)
## 4 1.14 (0.5,2.58)
## 5 1.02 (0.45,2.33)
## 6 0.99 (0.37,2.65)
##
## academic_performance: ref.=
## Negative effect 20.15 (0.99,412.19)
## No change 20.48 (0.95,439.41)
## Positive effect 9.83 (0.46,208.88)
##
## trouble_sleeping: ref.=Maybe
## No 0.52 (0.28,1)
## Yes 1.3 (0.56,3.02)
##
## social_relationships: ref.=Negative effect
## No effect 0.4 (0.22,0.73)
## Positive effect 0.48 (0.23,1.01)
##
## eating_habits_how: ref.=both amount and type
## amount of food 1.67 (0.89,3.13)
## type of food 0.9 (0.44,1.86)
## no change 3.76 (0.4,35.37)
##
## smoking_habits: ref.=non-smoker
## smoker with no change 0.51 (0.16,1.62)
## smoker with reduced smoking 2366375.95 (0,Inf)
## started smoking 0.5 (0.04,7.07)
## stopped smoking 570327.6 (0,Inf)
##
## sleep_quality: ref.=Fairly bad
## Fairly good 0.23 (0.07,0.71)
## Very bad 0.38 (0.07,1.88)
## Very good 0.19 (0.05,0.68)
##
## physical_activity: ref.=Less physical activity
## More physical activity 0.63 (0.32,1.24)
## No change 0.3 (0.16,0.58)
##
## P(Wald's test) P(LR-test)
## year: ref.=1 0.681
## 2 0.458
## 3 0.151
## 4 0.754
## 5 0.955
## 6 0.981
##
## academic_performance: ref.= 0.027
## Negative effect 0.051
## No change 0.054
## Positive effect 0.143
##
## trouble_sleeping: ref.=Maybe 0.043
## No 0.051
## Yes 0.536
##
## social_relationships: ref.=Negative effect 0.007
## No effect 0.003
## Positive effect 0.054
##
## eating_habits_how: ref.=both amount and type 0.181
## amount of food 0.108
## type of food 0.78
## no change 0.247
##
## smoking_habits: ref.=non-smoker 0.424
## smoker with no change 0.254
## smoker with reduced smoking 0.986
## started smoking 0.609
## stopped smoking 0.99
##
## sleep_quality: ref.=Fairly bad 0.028
## Fairly good 0.011
## Very bad 0.234
## Very good 0.011
##
## physical_activity: ref.=Less physical activity 0.001
## More physical activity 0.178
## No change < 0.001
##
## Log-likelihood = -181.7714
## No. of observations = 466
## AIC value = 413.5428
Publish::publish(fit.log)
## Variable Units OddsRatio CI.95 p-value
## year 1 Ref
## 2 1.43 [0.56;3.69] 0.4581683
## 3 2.27 [0.74;6.93] 0.1511314
## 4 1.14 [0.50;2.58] 0.7540081
## 5 1.02 [0.45;2.33] 0.9553123
## 6 0.99 [0.37;2.65] 0.9813604
## academic_performance Ref
## Negative effect 20.15 [0.99;412.19] 0.0511498
## No change 20.48 [0.95;439.41] 0.0535888
## Positive effect 9.83 [0.46;208.88] 0.1426806
## trouble_sleeping Maybe Ref
## No 0.52 [0.28;1.00] 0.0506494
## Yes 1.30 [0.56;3.02] 0.5361982
## social_relationships Negative effect Ref
## No effect 0.40 [0.22;0.73] 0.0026394
## Positive effect 0.48 [0.23;1.01] 0.0535180
## eating_habits_how both amount and type Ref
## amount of food 1.67 [0.89;3.13] 0.1079452
## type of food 0.90 [0.44;1.86] 0.7803578
## no change 3.76 [0.40;35.37] 0.2470331
## smoking_habits non-smoker Ref
## smoker with no change 0.51 [0.16;1.62] 0.2542173
## smoker with reduced smoking 2366375.95 [0.00;Inf] 0.9858598
## started smoking 0.50 [0.04;7.07] 0.6092226
## stopped smoking 570327.60 [0.00;Inf] 0.9896203
## sleep_quality Fairly bad Ref
## Fairly good 0.23 [0.07;0.71] 0.0112707
## Very bad 0.38 [0.07;1.88] 0.2338951
## Very good 0.19 [0.05;0.68] 0.0109937
## physical_activity Less physical activity Ref
## More physical activity 0.63 [0.32;1.24] 0.1776663
## No change 0.30 [0.16;0.58] 0.0002853
ggplot(depression, mapping= aes(x = anxiety2)) +
geom_histogram(width = 1, fill = "darkred") +
theme_minimal() +
labs(
x = "",
y = "",
title = "Anxiety score by sex") +
facet_wrap(~sex)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
plot1 <- depression %>%
filter(sex=="Male") %>%
ggplot(aes(x = anxiety2)) +
geom_histogram(width = 1, fill = "darkred") +
theme_minimal() +
labs(x = "",y = "Male", title = "Anxiety score by sex")
plot2 <- depression %>%
filter(sex=="Female") %>%
ggplot(aes(x = anxiety2)) +
geom_histogram(width = 1, fill = "darkred") +
theme_minimal() +
labs(x = "",y = "Female", title = "Anxiety score by sex")
library(ggpubr)
figure <- ggarrange(plot1, plot2,
labels = c("A", "B"),
ncol = 2, nrow = 1)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
figure
ggplot(depression,map= aes(x = anxiety, col = sex, fill = sex, group = sex )) +
geom_bar() +
theme_minimal() +
labs(
x = "",
y = "",
title = "Anxiety score by sex")
##Likert Graphs
#preparing DASS data
Data = depression %>% dplyr::select(x1:x21) %>% mutate_all(as.character)
head(Data)
## x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21
## 1 0 1 2 1 2 3 0 2 0 0 1 0 2 0 0 0 0 3 2 0 0
## 2 2 1 2 0 1 1 1 0 1 1 1 2 0 1 1 1 1 2 1 1 0
## 3 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1
## 4 2 0 3 3 2 1 1 1 3 1 1 3 2 0 2 2 1 3 2 1 1
## 5 2 2 2 1 1 2 1 2 2 2 2 2 2 1 2 2 2 2 3 3 2
## 6 1 1 1 0 1 1 0 1 0 0 1 1 1 0 0 0 2 1 0 0 0
Data[Data == 0 ] <- "Did not apply"
Data[Data == 1 ] <- "Applies to some degree"
Data[Data == 2 ] <- "Applies to a considerable degree"
Data[Data == 3 ] <- "Most of the time"
Data <- Data %>% mutate_all(as.factor)
head(Data)
## x1 x2
## 1 Did not apply Applies to some degree
## 2 Applies to a considerable degree Applies to some degree
## 3 Applies to some degree Applies to some degree
## 4 Applies to a considerable degree Did not apply
## 5 Applies to a considerable degree Applies to a considerable degree
## 6 Applies to some degree Applies to some degree
## x3 x4
## 1 Applies to a considerable degree Applies to some degree
## 2 Applies to a considerable degree Did not apply
## 3 Applies to some degree Applies to some degree
## 4 Most of the time Most of the time
## 5 Applies to a considerable degree Applies to some degree
## 6 Applies to some degree Did not apply
## x5 x6
## 1 Applies to a considerable degree Most of the time
## 2 Applies to some degree Applies to some degree
## 3 Applies to some degree Applies to some degree
## 4 Applies to a considerable degree Applies to some degree
## 5 Applies to some degree Applies to a considerable degree
## 6 Applies to some degree Applies to some degree
## x7 x8
## 1 Did not apply Applies to a considerable degree
## 2 Applies to some degree Did not apply
## 3 Applies to a considerable degree Applies to a considerable degree
## 4 Applies to some degree Applies to some degree
## 5 Applies to some degree Applies to a considerable degree
## 6 Did not apply Applies to some degree
## x9 x10
## 1 Did not apply Did not apply
## 2 Applies to some degree Applies to some degree
## 3 Applies to some degree Applies to some degree
## 4 Most of the time Applies to some degree
## 5 Applies to a considerable degree Applies to a considerable degree
## 6 Did not apply Did not apply
## x11 x12
## 1 Applies to some degree Did not apply
## 2 Applies to some degree Applies to a considerable degree
## 3 Applies to some degree Applies to some degree
## 4 Applies to some degree Most of the time
## 5 Applies to a considerable degree Applies to a considerable degree
## 6 Applies to some degree Applies to some degree
## x13 x14
## 1 Applies to a considerable degree Did not apply
## 2 Did not apply Applies to some degree
## 3 Applies to some degree Applies to some degree
## 4 Applies to a considerable degree Did not apply
## 5 Applies to a considerable degree Applies to some degree
## 6 Applies to some degree Did not apply
## x15 x16
## 1 Did not apply Did not apply
## 2 Applies to some degree Applies to some degree
## 3 Applies to some degree Applies to some degree
## 4 Applies to a considerable degree Applies to a considerable degree
## 5 Applies to a considerable degree Applies to a considerable degree
## 6 Did not apply Did not apply
## x17 x18
## 1 Did not apply Most of the time
## 2 Applies to some degree Applies to a considerable degree
## 3 Applies to some degree Applies to some degree
## 4 Applies to some degree Most of the time
## 5 Applies to a considerable degree Applies to a considerable degree
## 6 Applies to a considerable degree Applies to some degree
## x19 x20
## 1 Applies to a considerable degree Did not apply
## 2 Applies to some degree Applies to some degree
## 3 Applies to some degree Applies to some degree
## 4 Applies to a considerable degree Applies to some degree
## 5 Most of the time Most of the time
## 6 Did not apply Did not apply
## x21
## 1 Did not apply
## 2 Did not apply
## 3 Applies to some degree
## 4 Applies to some degree
## 5 Applies to a considerable degree
## 6 Did not apply
mutate_order <- function(x) {
x <- factor(x ,
levels = c("Did not apply",
"Applies to some degree",
"Applies to a considerable degree",
"Most of the time"),
ordered = TRUE)
return(x)
}
Data$x1 = mutate_order(Data$x1)
Data$x2 = mutate_order(Data$x2)
Data$x3 = mutate_order(Data$x3)
levels(Data$x3)
## [1] "Did not apply" "Applies to some degree"
## [3] "Applies to a considerable degree" "Most of the time"
Data = as.data.frame(Data)
library(likert)
## Loading required package: xtable
##
## Attaching package: 'likert'
## The following object is masked from 'package:dplyr':
##
## recode
Result = likert(Data)
summary(Result)
## Item low neutral high mean sd
## 7 x7 33.20523 0 66.79477 2.669485 0.8151822
## 4 x4 36.20292 0 63.79708 2.604151 0.8091485
## 21 x21 36.27978 0 63.72022 2.802460 1.1067041
## 15 x15 40.35357 0 59.64643 2.625673 0.9909989
## 10 x10 40.66103 0 59.33897 2.744043 1.1594980
## 17 x17 41.27594 0 58.72406 2.714835 1.1385563
## 20 x20 41.96772 0 58.03228 2.600307 1.0228767
## 9 x9 42.58263 0 57.41737 2.651806 1.0573073
## 19 x19 42.58263 0 57.41737 2.596464 1.0709966
## 16 x16 47.42506 0 52.57494 2.651038 1.2371093
## 8 x8 47.65565 0 52.34435 2.627978 1.1935537
## 11 x11 47.80938 0 52.19062 2.652575 1.2609410
## 5 x5 48.34743 0 51.65257 2.622598 1.2678557
## 18 x18 48.42429 0 51.57571 2.545734 1.1219930
## 13 x13 48.96234 0 51.03766 2.624904 1.2706610
## 3 x3 52.03689 0 47.96311 2.510377 1.0958347
## 6 x6 52.11376 0 47.88624 2.485012 1.1193947
## 12 x12 55.18832 0 44.81168 2.460415 1.1160409
## 1 x1 58.41660 0 41.58340 2.345888 1.0169148
## 14 x14 59.26211 0 40.73789 2.355880 1.1011279
## 2 x2 76.63336 0 23.36664 1.826287 0.9886825
plot(Result,
type="bar")
social_relationships