-
Notifications
You must be signed in to change notification settings - Fork 0
/
student-mental-health.R
148 lines (126 loc) · 5.74 KB
/
student-mental-health.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
library(tidyverse)
library(plotly)
library(readr)
library(ggplot2)
health <- read.csv("C:\\Users\\Radhika\\Desktop\\Semester 4\\Stat\\Project\\Student Mental health.csv")
# Data Cleaning
names(health) <- c('Timestamp', 'Gender', 'Age', 'Course', 'Year', 'CGPA', 'Married', 'Depression', 'Anxiety', 'Panic_Attack', 'Treatment')
health$Age[is.na(health$Age)] <- median(health$Age, na.rm = TRUE)
#------------------------------------------------------------------------------------------------------------------------------------------------------------
# Data Visualization
# Age Distribution
health %>%
group_by(Age) %>%
summarize(count = n()) %>%
plot_ly(x = ~Age, y = ~count, type = 'bar',
text = ~count,
textposition = 'outside',
marker = list(color = 'rgb(158,202,225)',
line = list(color = 'black',
width = 1.0))) %>%
layout(title = 'Distribution of Age')
# Gender Distribution
Health_SummaryStat <- health %>%
group_by(Gender) %>%
summarise(count = n(),
percentage = round((n()/ nrow(health)), digits = 4))
colors <- c('rgb(211,94,96)','rgb(114,147,203)')
Gender_PieChart <- plot_ly(data = Health_SummaryStat, labels = ~Gender, values = ~percentage,
type = 'pie', sort = FALSE,
textposition = 'inside',
textinfo = 'label+percent',
insidetextfont = list(color = 'White'),
hoverinfo = 'text',
text = ~count,
marker = list(colors = colors,
line = list(color = 'Black', width = 1)),
showlegend = TRUE)
Gender_PieChart <- Gender_PieChart %>% layout(title = 'Pie Chart of Gender')
Gender_PieChart
# Depression
Health_SummaryStat2 <- health %>%
group_by(Depression) %>%
summarise(count = n(),
percentage = round((n()/ nrow(health)), digits = 4))
Depression_PieChart <- plot_ly(data = Health_SummaryStat2, labels = ~Depression, values = ~percentage,
type = 'pie', sort = FALSE,
textposition = 'inside',
textinfo = 'label+percent',
insidetextfont = list(color = 'White'),
hoverinfo = 'text',
text = ~count,
marker = list(colors = colors,
line = list(color = 'Black', width = 1)),
showlegend = TRUE)
Depression_PieChart %>% layout(title = 'Pie Chart of Depression')
# Depression vs Gender
health %>%
count(Gender, Depression, sort = FALSE) %>%
group_by(Gender) %>%
mutate(prop = round((n / sum(n)),digits = 4)) %>%
plot_ly(x = ~Gender, y = ~prop, color = ~Depression, type = "bar",
text = ~paste(Gender, prop*100 ,'%'),
textposition = 'outside') %>%
layout(barmode = 'stack',
title = 'Barplot of Depression amongst Genders')
# CGPA
health$CGPA <- as.factor(health$CGPA)
health %>%
group_by(CGPA)%>%
summarize(count = n()) %>%
plot_ly(x =~CGPA, y=~count, type = 'bar',
text = ~count,
textposition = 'outside',
marker = list(color = 'rgb(158,202,225)',
line = list(color = 'black',
width = 1.0))) %>%
layout(title = 'Distibution of CGPA')
health %>%
count(CGPA, Depression, sort = F) %>%
mutate(proportion = round((n/sum(n)),digits=4)) %>%
plot_ly(x =~CGPA, y=~proportion, color = ~Depression, type = 'bar') %>%
layout(barmode = 'Group',
title = 'Barplot of Depression vs CGPA')
# Courses
health %>%
group_by(Course) %>%
summarise(count = n()) %>%
arrange(desc(count)) %>%
filter(count >2)
health %>%
filter(grepl('BIT|KOE|BCS|Engineering|Biomedical science', Course)) %>%
count(Course, Depression, sort = T) %>%
group_by(Course) %>%
mutate(prop = round((n / sum(n)),digits = 4)) %>%
plot_ly(x = ~Course, y=~n, color = ~Depression, type = "bar",
text = ~paste(Course, n),
textposition = 'outside') %>%
layout(barmode = 'Stacked',
title = 'Barplot of Depression amongst the top 5 Courses')
#------------------------------------------------------------------------------------------------------------------------------------------------------------
#Logistic Regression
CGPA = health$CGPA
Depressed = health$Depression
data <- data.frame(CGPA, Depressed)
data$Depressed = factor(data$Depressed, labels = c(0, 1))
# fitting the logistic regression model
model <- glm(Depressed ~ CGPA, data = data, family = binomial())
summary(model)
# predict the probability of depression based on CGPA
newdata <- data.frame(CGPA = "3.00 - 3.49")
predict(model, newdata, type = "response")
#------------------------------------------------------------------------------------------------------------------------------------------------------------
#Plotting the regression model
Age = health$Age
Depressed = ifelse(health$Depression == "Yes", 1, 0)
data <- data.frame(Age, Depressed)
age_seq <- seq(min(Age), max(Age), length.out = 7)
# predict the probability of depression vs age
probs <- predict(model, newdata = data.frame(Age = age_seq), type = "response")
# plot the logistic regression curve
ggplot(health, aes(x = Age, y = Depressed)) +
geom_point() +
stat_smooth(method="glm", color="green", se=FALSE, method.args = list(family=binomial)) +
xlab("Age") +
ylab("Probability of Depression") +
ggtitle("Logistic Regression Model")