This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
my_data <- read.csv("/Users/shemaaeltayeb/Desktop/Intro to Statistics in R/student_data.csv")
#summary(my_data)
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.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── 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
library(dplyr)
my_data <- read.csv("/Users/shemaaeltayeb/Desktop/Intro to Statistics in R/student_data.csv")
## Group By Higher Education:
grouped_by_higher_education <- my_data %>%
group_by(higher) %>%
summarise(count =n())
print(grouped_by_higher_education)
## # A tibble: 2 × 2
## higher count
## <chr> <int>
## 1 no 20
## 2 yes 375
In the ‘higher’ education category, which represents students aspiring to pursue higher education, the summary reveals a significant disparity. The data shows that a vast majority of students, specifically 375 individuals, express a strong desire to continue their education at a higher level. In contrast, only a small minority of 20 students indicate a lack of interest in pursuing higher education.
In further investigations, we can gain a deeper understanding of the factors and influences behind students’ aspirations for higher education and how these aspirations relate to various aspects of their lives and academic performance. We will examine whether there are gender differences in the desire for higher education. Do more females than males aspire to continue their education beyond the current level?
## Group by Address:
grouped_by_address <- my_data %>%
group_by(address) %>%
summarise(count =n())
print(grouped_by_address)
## # A tibble: 2 × 2
## address count
## <chr> <int>
## 1 R 88
## 2 U 307
The data indicates that a larger proportion of students come from urban areas (307) compared to rural areas (88).
These investigations can help uncover patterns, disparities, or factors that may be associated with the students’ geographic locations (rural vs. urban) and how they relate to various aspects of their lives and academic performance.
## Group by Gender:
grouped_by_gender <- my_data %>%
group_by(sex) %>%
summarise(count =n())
print(grouped_by_gender)
## # A tibble: 2 × 2
## sex count
## <chr> <int>
## 1 F 208
## 2 M 187
The data shows that there are 208 female students and 187 male students.
Further will investigate the gender distribution to see if it has any implications for other variables?
We will explore whether family educational support (‘famsup’) varies by gender and if there differences in the level of support provided by families for education based on the gender of the student?
## Group by School:
grouped_by_school <- my_data %>%
group_by(school) %>%
summarise(count =n())
print(grouped_by_school )
## # A tibble: 2 × 2
## school count
## <chr> <int>
## 1 GP 349
## 2 MS 46
The data reveals a significant difference in the number of students between the two schools. Gabriel Pereira (‘GP’) has a substantially larger student population compared to Mousinho da Silveira (‘MS’).
Several factors could contribute to this difference, such as the schools’ locations, reputations, or admission policies. Further investigation is needed to understand the reasons behind this disparity.
## Group_by_Family_education_support:
grouped_by_famsup <- my_data %>%
group_by(famsup) %>%
summarise(count =n())
print(grouped_by_famsup)
## # A tibble: 2 × 2
## famsup count
## <chr> <int>
## 1 no 153
## 2 yes 242
The data shows that there are 242 students who receive family educational support (‘yes’) and 153 students who do not receive such support (‘no’).
We can investigate whether family educational support has any correlation with academic performance?
We can explore whether there are any gender-based differences in family educational support.?
combinations_of_3_categorical_columns <- c("sex", "higher", "famsup")
grouped_combinations <- my_data %>%
group_by(across(all_of(combinations_of_3_categorical_columns )))
summary_counts <- grouped_combinations %>%
summarise(count = n())
## `summarise()` has grouped output by 'sex', 'higher'. You can override using the
## `.groups` argument.
print(summary_counts)
## # A tibble: 8 × 4
## # Groups: sex, higher [4]
## sex higher famsup count
## <chr> <chr> <chr> <int>
## 1 F no no 1
## 2 F no yes 3
## 3 F yes no 65
## 4 F yes yes 139
## 5 M no no 11
## 6 M no yes 5
## 7 M yes no 76
## 8 M yes yes 95
This output represents a summary of data grouped by three variables: ‘famsup’ , ‘sex’ and ‘higher’. It showing the frequency count of a categorical variable ‘famsup’ within each combination of ‘sex’ and ‘higher’.
There are [234] students who receive family support and aspire to pursue higher education.
Later will investigation, so we can understand how many students in the dataset both have family support and wish to pursue higher education, which could be valuable for making decisions or drawing insights about the relationship between these factors.
combinations_dif_categorical_columns <- c ( "sex","address","higher" )
grouped_dif_combinations <- my_data %>%
group_by(across(all_of(combinations_dif_categorical_columns )))
summary_counts <- grouped_dif_combinations %>%
summarise(count = n())
## `summarise()` has grouped output by 'sex', 'address'. You can override using
## the `.groups` argument.
print(summary_counts)
## # A tibble: 8 × 4
## # Groups: sex, address [4]
## sex address higher count
## <chr> <chr> <chr> <int>
## 1 F R no 2
## 2 F R yes 42
## 3 F U no 2
## 4 F U yes 162
## 5 M R no 4
## 6 M R yes 40
## 7 M U no 12
## 8 M U yes 131
It represent the number of students based on their gender (‘sex’), type of address (‘address’), and whether they aspire to pursue higher education (‘higher’).
Later we can analyze how many males (M) and females (F) aspire to pursue higher education (‘yes’) in both rural (R) and urban (U) areas.
# Calculate the count of "no" and "yes" in the "higher" column
higher_education_counts <- my_data %>%
count(higher)
# Calculate the probability
higher_education_probs <- higher_education_counts %>%
mutate(probability = n / sum(n))
# Display the probabilities
print(higher_education_probs )
## higher n probability
## 1 no 20 0.05063291
## 2 yes 375 0.94936709
There are 20 students who do not aspire to pursue higher education (‘no’) and 375 students who do aspire to pursue higher education (‘yes’).
The probability of students aspiring to pursue higher education (‘yes’) is approximately 0.949, which is nearly 95%. The probability of students not aspiring to pursue higher education (‘no’) is approximately 0.051, which is roughly 5%.
This result suggests that the majority of students in the data set aspire to pursue higher education and that can be used in various ways, such as predicting the likelihood of a new student aspiring to pursue higher education based on similar data patterns. Also can be used for decision-making, such as in educational institutions or policy planning. Additionally, these probabilities can be used as inputs in statistical models or further analyses to explore relationships with other variables or to make predictions.
# Calculate the count of address in the "address" column
address_counts <- my_data %>%
count(address)
# Calculate the probability
address_probs <- address_counts %>%
mutate(probability = n / sum(n))
# Display the probabilities
print(address_probs)
## address n probability
## 1 R 88 0.2227848
## 2 U 307 0.7772152
The data shows that 307 students have urban addresses (‘U’), while 88 students have rural addresses (‘R’). The majority of students in this dataset reside in urban areas.
The probability of having an urban address is approximately 0.777, significantly higher than the probability of having a rural address, which is approximately 0.223.
we can investigate how the type of address might relate to other aspects of students’ lives and education. By conducting these investigations, we can gain insights into how the type of address might impact various aspects of students’ lives and education, helping educators and make informed decisions and provide targeted support where needed.
# Calculate the count of "M" and "F" in the "sex" column
sex_counts <- my_data %>%
count(sex)
# Calculate the probability
sex_probs <- sex_counts %>%
mutate(probability = n / sum(n))
# Display the probabilities
print(sex_probs)
## sex n probability
## 1 F 208 0.5265823
## 2 M 187 0.4734177
The data shows that there are 208 female students and 187 male students in the dataset. This implies that the dataset has a relatively balanced gender distribution, with slightly more female students (approximately 52.66%) than male students (approximately 47.34%).
# Calculate the count of "M" and "F" in the "famsup" column
famsup_counts <- my_data %>%
count(famsup)
# Calculate the probability
famsub_probs <- famsup_counts %>%
mutate(probability = n / sum(n))
# Display the probabilities
print(famsub_probs )
## famsup n probability
## 1 no 153 0.3873418
## 2 yes 242 0.6126582
The data shows that there are 153 students without family educational support (‘no’) and 242 students with family educational support (‘yes’). This indicates that a majority of students in this dataset receive family educational support, accounting for approximately 61.27% of the total, while the remaining 38.73% do not receive such support.
probabilitie_for_combinations <- my_data %>%
group_by(sex, famsup, higher) %>%
summarise(probability = n() / nrow(my_data))
## `summarise()` has grouped output by 'sex', 'famsup'. You can override using the
## `.groups` argument.
print(probabilitie_for_combinations)
## # A tibble: 8 × 4
## # Groups: sex, famsup [4]
## sex famsup higher probability
## <chr> <chr> <chr> <dbl>
## 1 F no no 0.00253
## 2 F no yes 0.165
## 3 F yes no 0.00759
## 4 F yes yes 0.352
## 5 M no no 0.0278
## 6 M no yes 0.192
## 7 M yes no 0.0127
## 8 M yes yes 0.241
The data provides probabilities for various scenarios based on these gender groups and whether students receive family educational support (‘famsup’) and aspire to higher education (‘higher’).
Investigate the probabilities associated with gender and family educational support (‘famsup’). For each gender group (‘F’ and ‘M’), compare the probabilities of students receiving or not receiving family support. Do male or female students tend to receive more family support?
lowest_probability_group <- probabilitie_for_combinations %>%
filter(probability == min(probability))
print(lowest_probability_group)
## # A tibble: 4 × 4
## # Groups: sex, famsup [4]
## sex famsup higher probability
## <chr> <chr> <chr> <dbl>
## 1 F no no 0.00253
## 2 F yes no 0.00759
## 3 M no no 0.0278
## 4 M yes no 0.0127
To investigate the lower probability group, which consists of ‘F’ (female) students with ‘no’ family educational support (‘famsup’ = ‘no’) and ‘no’ aspiration for higher education (‘higher’ = ‘no’).
lowest_probability_group <- famsub_probs %>%
filter(probability == min(probability))
print(lowest_probability_group)
## famsup n probability
## 1 no 153 0.3873418
probabilitie_for_diff_combinations <- my_data %>%
group_by(sex, famsup, address) %>%
summarise(probability = n() / nrow(my_data))
## `summarise()` has grouped output by 'sex', 'famsup'. You can override using the
## `.groups` argument.
print(probabilitie_for_diff_combinations)
## # A tibble: 8 × 4
## # Groups: sex, famsup [4]
## sex famsup address probability
## <chr> <chr> <chr> <dbl>
## 1 F no R 0.0380
## 2 F no U 0.129
## 3 F yes R 0.0734
## 4 F yes U 0.286
## 5 M no R 0.0532
## 6 M no U 0.167
## 7 M yes R 0.0582
## 8 M yes U 0.195
This result offers valuable insights into the distribution of specific student profiles within the dataset based on gender, family support, and home address type, providing a foundation for further analysis and investigation into the factors influencing these probabilities.
The data suggests that female students with family support and urban addresses are more prevalent, while male students with no family support and rural addresses are less common.
Students with urban home addresses (‘U’) tend to have higher probabilities in all combinations compared to students with rural home addresses (‘R’).
Female (‘F’) students are the majority in all combinations.
Male (‘M’) students have lower probabilities in all combinations.
In each ‘sex’ category, students with ‘yes’ family educational support generally have higher probabilities compared to those with ‘no’ family educational support.
lowest_probability_diff_group <- probabilitie_for_diff_combinations %>%
filter(probability == min(probability))
print(lowest_probability_diff_group)
## # A tibble: 4 × 4
## # Groups: sex, famsup [4]
## sex famsup address probability
## <chr> <chr> <chr> <dbl>
## 1 F no R 0.0380
## 2 F yes R 0.0734
## 3 M no R 0.0532
## 4 M yes R 0.0582
lowest_probability_diff_group <- probabilitie_for_diff_combinations %>%
filter(probability == min(probability))%>%
mutate(Anomaly = "Anomaly")
print(lowest_probability_diff_group)
## # A tibble: 4 × 5
## # Groups: sex, famsup [4]
## sex famsup address probability Anomaly
## <chr> <chr> <chr> <dbl> <chr>
## 1 F no R 0.0380 Anomaly
## 2 F yes R 0.0734 Anomaly
## 3 M no R 0.0532 Anomaly
## 4 M yes R 0.0582 Anomaly
probabilitie_for_diff_combinations <- my_data %>%
group_by(sex, famsup, address) %>%
summarise(probability = n() / nrow(my_data))%>%
mutate(Anomaly = "Anomaly")
## `summarise()` has grouped output by 'sex', 'famsup'. You can override using the
## `.groups` argument.
print(probabilitie_for_diff_combinations)
## # A tibble: 8 × 5
## # Groups: sex, famsup [4]
## sex famsup address probability Anomaly
## <chr> <chr> <chr> <dbl> <chr>
## 1 F no R 0.0380 Anomaly
## 2 F no U 0.129 Anomaly
## 3 F yes R 0.0734 Anomaly
## 4 F yes U 0.286 Anomaly
## 5 M no R 0.0532 Anomaly
## 6 M no U 0.167 Anomaly
## 7 M yes R 0.0582 Anomaly
## 8 M yes U 0.195 Anomaly
Based on the analysis of the data and the calculated probabilities, The specific groups that appear to be rarer in the dataset, they are the combinations of ‘sex,’ ‘famsup,’ and ‘higher’ where the probabilities are lower.
Testable hypothesis 1: Students with urban home addresses (‘U’) tend to have higher probabilities in all combinations compared to students with rural home addresses (‘R’) and that could be because they have higher probability of expressing interest in higher education (‘higher’ = ‘yes’) compared to students in rural areas (‘address’ = ‘R’)."
Testable hypothesis 2: Students with family support are more likely to aspire to higher education compared to those without family support."
Testable hypothesis 3: Female students are more likely to aspire to higher education compared to male students."
Testable hypothesis 4 :In each ‘sex’ category, students with ‘yes’ family educational support generally have higher probabilities compared to those with ‘no’ family educational support.
library(ggplot2)
summary_data <- my_data %>%
group_by(sex, famsup) %>%
summarise(probability = n() / nrow(my_data))
## `summarise()` has grouped output by 'sex'. You can override using the `.groups`
## argument.
# Create a bar plot
ggplot(summary_data, aes(x = sex, y = probability, fill = famsup)) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Comparison of Gender and Family Support",
x = "Sex",
y = "Probability",
fill = "Family Support"
) +
theme_minimal()
combinations_dif_categorical_columns <- c ( "famsup","higher","sex" )
grouped_dif_combinations <- my_data %>%
group_by(across(all_of(combinations_dif_categorical_columns )))
summary_counts <- grouped_dif_combinations %>%
summarise(count = n())
## `summarise()` has grouped output by 'famsup', 'higher'. You can override using
## the `.groups` argument.
print(summary_counts)
## # A tibble: 8 × 4
## # Groups: famsup, higher [4]
## famsup higher sex count
## <chr> <chr> <chr> <int>
## 1 no no F 1
## 2 no no M 11
## 3 no yes F 65
## 4 no yes M 76
## 5 yes no F 3
## 6 yes no M 5
## 7 yes yes F 139
## 8 yes yes M 95
library(dplyr)
# Create a data frame with all possible combinations
all_combinations <- expand.grid(
higher = c('yes', 'no'),
famsup = c('yes', 'no')
)
# Anti-join with your original data to find combinations that never show up
missing_combinations <- anti_join(all_combinations, my_data)
## Joining with `by = join_by(higher, famsup)`
# View the missing combinations
print(missing_combinations)
## [1] higher famsup
## <0 rows> (or 0-length row.names)
library(dplyr)
# Group by the two categorical variables famsup and higher education
grouped_data <- my_data %>%
group_by(famsup,higher)
# Count the occurrences of each combination
combination_counts <- grouped_data %>%
summarise(count = n())
## `summarise()` has grouped output by 'famsup'. You can override using the
## `.groups` argument.
# Sort the combinations by count to find the most common and least common
sorted_combinations <- combination_counts %>%
arrange(desc(count))
# View the sorted combinations
print(sorted_combinations)
## # A tibble: 4 × 3
## # Groups: famsup [2]
## famsup higher count
## <chr> <chr> <int>
## 1 yes yes 234
## 2 no yes 141
## 3 no no 12
## 4 yes no 8
The most common combination observed among students aspiring for higher education is the presence of family support. This suggests that having consistent family support tends to correlate with a higher likelihood of pursuing advanced education.
library(ggplot2)
combinations <- data.frame(
higher = c('yes', 'yes', 'no', 'no'),
famsup = c('yes', 'no', 'yes', 'no'),
count = c(234, 141, 8, 12)
)
# Create a grouped bar chart
ggplot(combinations, aes(x = higher, y = count, fill = famsup)) +
geom_bar(stat = 'identity', position = 'dodge') +
labs(
title = 'Combination of Higher Education Aspiration and Family Educational Support',
x = 'Higher Education Aspiration',
y = 'Count'
) +
scale_fill_manual(values = c('yes' = 'blue', 'no' = 'red')) +
theme_minimal()