R Markdown

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/STATISTIC IN R /student_data.csv")
#summary(my_data)

A collection of 5-10 random samples of data (with replacement) from at least 6 columns of data:

1- Each subsample should be as long as roughly 50% percent of your data. We are simulating the act of collecting data from a population where the “population” is represented by the data set you already have.

2- Store each sample set in a separate data frame (e.g., df_i might contain m rows from columns 1-6).

3- These subsamples should include both categorical and continuous (numeric) data.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ 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)
# Create an empty list to store the sample data frames
sample_data_frames <- list()

# Set the seed for reproducibility 
set.seed(123)
# The number of samples:
num_samples <- 8 

# The number of rows in each sample (50% of the data):

sample_size <- round(0.5 * nrow(my_data))

for (i in 1:num_samples) {
  
  sample_indices <- sample(1:nrow(my_data), size = sample_size, replace = TRUE)
  
  # Create the sample data frame
  
  sample_df <- my_data[sample_indices, ]
  
  # Store the sample data frame in the list
  
  sample_data_frames[[paste0("sample_", i)]] <- sample_df
}

To View The 8 Samples Data Frames:

# Print the rows of one of the samples:
View(sample_data_frames$sample_1)
View(sample_data_frames$sample_2)
View(sample_data_frames$sample_3)
View(sample_data_frames$sample_4)
View(sample_data_frames$sample_5)
View(sample_data_frames$sample_6)
View(sample_data_frames$sample_7)
View(sample_data_frames$sample_8)

Create 5 Subsamples With 6 Columns:-

selected_variables <- c("sex","Medu","famsup", "famrel", "health","G3")

# create subsamples 
num_subsamples <- 6 

# Number of rows in each subsample 
fixed_subsample_size <- round(0.5 * nrow(my_data))

# Create a list to store the subsamples

subsample_collection <- list()

# Generate random subsamples and store them in the list

for (i in 1:num_subsamples) {
 
  subsample_indices <- sample(1:nrow(my_data), size = fixed_subsample_size, replace = TRUE)
  
  # Create a random subsample from the sample
  
  random_subsample <-my_data[subsample_indices,selected_variables]
  
  # Store the subsample in the collection
  
  subsample_collection[[i]] <- random_subsample
}

To View Subsample collections(1-6):

# Print Subsamples:
View(subsample_collection[1])
View(subsample_collection[2])
View(subsample_collection[3])
View(subsample_collection[4])
View(subsample_collection[5])
View(subsample_collection[6])

I created a subsample from the student dataset, consisting of 6 columns: sex, Medu (Mother’s Education), famsup (Family Educational Support), famrel (Quality of Family Relationships), health, and G3 (Final Grade). Here’s an interpretation of what is obtained in this subsample:

sex (Gender):

This column indicates the gender of the students. In the subsample, there is a mix of male (M) and female (F) students. This information could be relevant for understanding any gender-related differences in the other variables.

Medu (Mother’s Education):

This column represents the level of education of the students’ mothers. It ranges from 0 (very low) to 4 (very high) which is represent as 0 is none degree, 1 is primary education, 2 is 5th_9th grade, 3 is secondary education and 4 is higher education. The subsample includes a mix of different education levels.

famsup (Family Educational Support):

This is a binary column indicating whether the students receive family educational support (yes) or not (no). In the subsample, some students receive support, while others do not.

famrel (Quality of Family Relationships):

This column represents the quality of family relationships, with values ranging from 1 (very low) to 5 (very high). The subsample includes a range of family relationship qualities.

health:

This variable describes the self-rated health of the students, with higher values indicating better health. The subsample includes students with varying health ratings.

G3 (Final Grade):

This is the final grade of the students. It ranges from 0 to 20. The subsample contains students with a wide range of final grades, including some with high grades and others with low grades.

This subsamples provides a snapshot of the student dataset and will be focused on analyses and insights into the factors that might influence students’ final grades. From this subsample, I can explore various questions and analyze relationships between these variables. For example, Will investigate whether mother’s education level or family support has any impact on students’ final grades or their self-rated health. Additionally, I will explore gender differences in these variables.

Scrutinize these subsamples.

How different are they?

What would you have called an anomaly in one sub-sample that you wouldn’t in another?

Are there aspects of the data that are consistent among all sub-samples?

# Calculate Summary Statistics for subsample_collection[1]:

for (col_name in names(subsample_collection[[1]])) {
  cat("Summary for column:", col_name, "\n")
  
  print(summary(subsample_collection[[1]][[col_name]]))
  
}
## Summary for column: sex 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: Medu 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   2.000   3.000   2.692   4.000   4.000 
## Summary for column: famsup 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: famrel 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   4.000   4.000   3.955   5.000   5.000 
## Summary for column: health 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.00    3.00    4.00    3.54    5.00    5.00 
## Summary for column: G3 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    9.00   11.00   11.14   15.00   20.00

These summary statistics provide an overview of the distribution of values in each column,so we can understand the spread, and range of the data in subsample 1 .

Mother’s Education (Medu):

The “Medu” column ranges from 1 (representing primary education) to 4 (highest education level). The minimum value of 1 indicates that some students have mothers with the lowest education level. The first quartile value of 2indicates that 25% of students have mothers with primary or lower education. The median value of 3 indicates that many students’ mothers have secondary-level education. The mean value of 2.843 is the average mother’s education level in this subsample. The third quartile value of 4indicates that 75% of the students have mothers with higher education.

Family Relationship Quality (famrel):

The “famrel” column ranges from 1 (representing very poor) to 5 (excellent). The minimum value of 1 indicates that some students perceive their family relationships as very poor. The first quartile value of 4 indicates that 25% of students report good family relationship quality. The median value of 4 indicates that many students have moderately positive family relationships. The mean value of 3.919 represents the average perception of family relationship quality. The third quartile value of 4 indicates that 75% of the students perceive their family relationships positively.

Self-Rated Health (health):

The “health” column ranges from 1 (lowest self-rated health) to 5 (highest self-rated health). The minimum value of 1 suggests that some students rate their health as very poor. The first quartile value of 3 indicates that 25% of students have moderate self-rated health. The median value of 4 indicates that many students have reasonably good self-rated health. The mean value of 3.667 is the average self-rated health in this subsample. The third quartile value of 5 indicates that 75% of the students rate their health positively.

Final Grades (G3):

The “G3” column shows final grades, ranging from 0 (lowest grade) to 20 (highest grade), and the maximum value of 20 suggests that some students excelled with the highest possible grade. The minimum value of 0 indicates that some students received the lowest possible grade. The first quartile value of 8 suggests that 25% of students scored below 8. The median value of 11 indicates that many students achieved moderately good grades. The mean value of 10.13 is the average final grade in this subsample.

The third quartile value of 13 indicates that 75% of the students performed well.

# Calculate Summary Statistics for subsample_collection[2]:

for (col_name in names(subsample_collection[[2]])) {
  cat("Summary for column:", col_name, "\n")
  
  print(summary(subsample_collection[[2]][[col_name]]))
  
  
}
## Summary for column: sex 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: Medu 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   2.000   3.000   2.773   4.000   4.000 
## Summary for column: famsup 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: famrel 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   3.250   4.000   3.874   4.000   5.000 
## Summary for column: health 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   3.000   4.000   3.566   5.000   5.000 
## Summary for column: G3 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    9.00   10.00   10.28   13.00   20.00

Mother’s Education (Medu):

The minimum value in the “Medu” column is 0. This might indicate that some data entries have missing or invalid values for mother’s education. The maximum value in this column is 4, representing the highest level of mother’s education. The first quartile value is 2, that means 25% of the data falls below a mother’s education level of 2. The median value is 3, indicating that the middle value in this column is 3, likely representing secondary education. The mean (average) mother’s education level is 2.646. The third quartile value is 4, that means 75% of the data falls below a higher education level.

Family Relationship Quality (famrel):

The minimum value in the “famrel” column is 1, possibly indicating very poor relationships. The maximum value is 5, indicating the highest level of family relationship quality. The first quartile value is 4, indicating that 25% of the data falls below a family relationship quality rating of 4.

The median value is 4, showing that the middle value represents moderately positive family relationships.

The mean family relationship quality rating is 3.929, providing an estimate of the average rating. The third quartile value is 4, that means 75% of the data falls below a rating of 4. ## Self-Rated Health (health):

The minimum value in the “health” column is 1, representing the lowest self-rated health level. The maximum value is 5, indicating the highest self-rated health level. The first quartile value is 3, indicating that 25% of the data falls below a health rating of 3. The median value is 4, showing that the middle value represents reasonably good self-rated health. The mean self-rated health level is 3.586, providing an estimate of the average health rating. The third quartile value is 5,that means 75% of the data falls below a health rating of 5.

Final Grades (G3):

The minimum value in the “G3” column is 0, representing the lowest possible final grade. The maximum value is 19, indicating that the highest final grade in this subsample is 19. The first quartile value is 8, meaning that 25% of the data falls below a final grade of 8. The median value is 11, indicating that the middle value represents moderately good final grades. The mean final grade is 10.2, providing an estimate of the average grade. The third quartile value is 13, suggesting that 75% of the data falls below a final grade of 13.

# Calculate Summary Statistics for subsample_collection[3]:

for (col_name in names(subsample_collection[[3]])) {
  cat("Summary for column:", col_name, "\n")
  
  print(summary(subsample_collection[[3]][[col_name]]))
  # Store the summary statistics in the list
  
}
## Summary for column: sex 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: Medu 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   2.000   3.000   2.783   4.000   4.000 
## Summary for column: famsup 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: famrel 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   3.000   4.000   3.843   4.000   5.000 
## Summary for column: health 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   3.000   4.000   3.621   5.000   5.000 
## Summary for column: G3 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    8.00   10.00   10.26   14.00   19.00

Mother’s Education (Medu):

This summary provides information about the “Medu” (Mother’s Education) column:

The minimum value in this column is 0.000. The maximum value in this column is 4.000. The 1st quartile (25th percentile) value is 2.000, meaning that 25% of the data falls below this value. The median (50th percentile) value is 3.000, representing the middle value in the data. The mean (average) value is 2.742, giving an estimate of the average mother’s education level. The 3rd quartile (75th percentile) value is 4.000, indicating that 75% of the data falls below this value.

Family Relationship Quality (famrel):

This summary provides information about the “famrel” (Family Relationship Quality) column:

The minimum value in this column is 1.000, possibly indicating poor family relationships. The maximum value in this column is 5.000, indicating the highest quality of family relationships. The 1st quartile (25th percentile) value is 3.250, meaning that 25% of the data falls below this value. The median (50th percentile) value is 4.000, indicating the middle value, likely representing moderately positive family relationships. The mean (average) value is 3.884, providing an estimate of the average family relationship quality. The 3rd quartile (75th percentile) value is 4.000,that means 75% of the data falls below this value.

Self-Rated Health (health):

This summary provides information about the “health” column:

The minimum value in this column is 1.000, representing the lowest self-rated health level. The maximum value in this column is 5.000, indicating the highest self-rated health level. The 1st quartile (25th percentile) value is 3.000, indicating that 25% of the data falls below this value. The median (50th percentile) value is 4.000, representing reasonably good self-rated health. The mean (average) value is 3.601, providing an estimate of the average self-rated health level. The 3rd quartile (75th percentile) value is 5.000, meaning that 75% of the data falls below this value.

Final Grades (G3):

The minimum value in this column is 0.00. This indicates that there are students in the subsample who received the lowest possible final grade, which might suggest that some students did not pass or perform poorly. The maximum value in this column is 19.00. This is the highest possible final grade that a student in the subsample achieved. It represents the top end of the grading scale. The 1st quartile value is 8.00. This means that 25% of the students in the subsample received final grades below 8.00. In other words, a quarter of the students scored less than or equal to 8.00, which represents a lower range of final grades. The median value is 10.00. This is the middle value in the dataset when all final grades are arranged in ascending order. It’s also referred to as the “typical” or “middle” grade in this subsample. The mean value is 10.12. This is the average of all the final grades in the subsample. It provides an estimate of the central tendency of the grades. In this case, the average final grade is slightly above the median, indicating that there might be some higher grades pulling the average up. The 3rd quartile value is 13.00. This implies that 75% of the students in the subsample received final grades below 13.00. In other words, the majority of the students scored less than or equal to 13.00.


From the summaries of the three subsamples, They are some differences in the distributions of the variables. Here are some key differences:

In Subsample 1:

In Subsample 1, the mean (average) mother’s education level (Medu) is 2.646. The mean family relationship quality rating (famrel) is 3.929. The mean self-rated health level (health) is 3.586. The mean final grade (G3) is 10.2.

In Subsample 2:

In Subsample 2, the mean mother’s education level (Medu) is slightly higher at 2.717. The mean family relationship quality rating (famrel) is 3.889, which is similar to Subsample 1. The mean self-rated health level (health) is 3.899. The mean final grade (G3) is 9.737.

Subsample 3:

In Subsample 3, the mean mother’s education level (Medu) is 2.742. The mean family relationship quality rating (famrel) is 3.884, which is also similar to Subsample 1. The mean self-rated health level (health) is 3.601. The mean final grade (G3) is 10.12.

These differences suggest variations in the characteristics of the students in each subsample:

Subsample 2 has the lowest mean final grade (G3), indicating that, on average, students in this subsample performed less well academically compared to the other subsamples. Subsample 3 has a slightly higher mean mother’s education level (Medu) compared to Subsample 1, indicating that the parents of students in Subsample 3 may have, on average, a slightly higher level of education. Subsample 3 also has a lower mean self-rated health level (health) compared to the other subsamples, which might suggest that, on average, students in this subsample rate their health slightly lower.

# Calculate Summary Statistics for subsample_collection[4]:

for (col_name in names(subsample_collection[[4]])) {
  cat("Summary for column:", col_name, "\n")
  
  print(summary(subsample_collection[[4]][[col_name]]))
  # Store the summary statistics in the list
  
}
## Summary for column: sex 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: Medu 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   2.000   3.000   2.646   4.000   4.000 
## Summary for column: famsup 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: famrel 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.000   4.000   4.000   4.056   5.000   5.000 
## Summary for column: health 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   3.000   4.000   3.495   5.000   5.000 
## Summary for column: G3 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    8.00   11.00   10.13   13.00   19.00

Medu (Mother’s Education)

The minimum value in this column is 0.000. This might indicate that some data entries have missing or invalid values for mother’s education.

Maximum: The maximum value is 4.000, indicating the highest level of mother’s education.

1st Quartile is 2.000. This means that 25% of the data falls below a mother’s education level of 2.

Median (50th percentile): 3.000. The median represents the middle value in this column and is likely indicative of secondary education.

Mean (average): 2.919. The mean value provides an estimate of the average mother’s education level.

3rd Quartile (75th percentile): 4.000. This means that 75% of the data falls below a higher education level.

famrel (Family Relationship Quality)

The minimum value in the “famrel” column is 1.000, which possibly indicates very poor family relationships.

Maximum: The maximum value is 5.000, indicating the highest level of family relationship quality.

1st Quartile (25th percentile): 4.000. This means that 25% of the data falls below a family relationship quality rating of 4.

Median (50th percentile): 4.000. The median represents moderately positive family relationships.

Mean (average): 3.894. The mean family relationship quality rating provides an estimate of the average rating.

3rd Quartile (75th percentile): 4.000. This means that 75% of the data falls below a rating of 4.

health (Self-Rated Health):

The minimum value in the “health” column is 1.000, representing the lowest self-rated health level.

The maximum value is 5.000, indicating the highest self-rated health level.

1st Quartile (25th percentile): 3.000. This means that 25% of the data falls below a health rating of 3.

Median (50th percentile): 4.000. The median represents reasonably good self-rated health.

Mean (average): 3.566. The mean self-rated health level provides an estimate of the average health rating.

3rd Quartile (75th percentile): 5.000. This means that 75% of the data falls below a health rating of 5.

G3 (Final Grades)

The minimum value in the “G3” column is 0.00, representing the lowest possible final grade.

The maximum value is 19.00, indicating the highest final grade.

1st Quartile (25th percentile): 8.00. This means that 25% of the data falls below a final grade of 8.

Median (50th percentile): 11.00. The median represents moderately good final grades.

Mean (average): 10.33. The mean final grade provides an estimate of the average grade.

3rd Quartile (75th percentile): 13.75. This means that 75% of the data falls below a final grade of 13.75.

The maximum value is 19.00, indicating the highest final grade.

# Calculate Summary Statistics for subsample_collection[5]:

for (col_name in names(subsample_collection[[5]])) {
  cat("Summary for column:", col_name, "\n")
  
  print(summary(subsample_collection[[5]][[col_name]]))
  # Store the summary statistics in the list
  
}
## Summary for column: sex 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: Medu 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   2.000   3.000   2.788   4.000   4.000 
## Summary for column: famsup 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: famrel 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.000   4.000   4.000   4.051   5.000   5.000 
## Summary for column: health 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   3.000   4.000   3.727   5.000   5.000 
## Summary for column: G3 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    9.00   11.00   11.06   14.00   19.00

Mother’s Education (Medu):

The minimum education level is 0, which is unusual and might indicate missing or invalid data.

The highest education level is 4.

About 25% of students have an education level of 2 or lower.

The median (middle value) is 3, representing moderate education.

The average (mean) education level is approximately 2.68.

Family Relationship Quality (famrel):

Family relationship quality ratings range from 1 to 5.

About 25% of students have a rating of 3 or lower.

The median rating is 4, indicating moderately positive family relationships.

The average rating is approximately 3.88.

Self-Rated Health (health):

Self-rated health levels range from 1 to 5.

About 25% of students have a health rating of 2 or lower.

The median health rating is 4, suggesting reasonably good health.

The average health rating is approximately 3.48.

Final Grades (G3):

Final grades vary from 0 to 20.

About 25% of students scored 8 or lower.

The median grade is 10.5, representing moderate performance.

The average grade is approximately 10.32.

# Calculate Summary Statistics for subsample_collection[6]:

for (col_name in names(subsample_collection[[6]])) {
  cat("Summary for column:", col_name, "\n")
  
  print(summary(subsample_collection[[6]][[col_name]]))
  # Store the summary statistics in the list
  
}
## Summary for column: sex 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: Medu 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   2.000   3.000   2.682   4.000   4.000 
## Summary for column: famsup 
##    Length     Class      Mode 
##       198 character character 
## Summary for column: famrel 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   3.000   4.000   3.909   4.000   5.000 
## Summary for column: health 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   2.000   4.000   3.545   5.000   5.000 
## Summary for column: G3 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    8.00   11.00   10.29   14.00   20.00

Mother’s Education (Medu):

The minimum education level is 0, which is unusual and might indicate missing or invalid data.

The highest education level is 4.

About 25% of students have an education level of 2 or lower.

The median (middle value) is 3, representing moderate education.

The average (mean) education level is approximately 2.73.

Family Relationship Quality (famrel):

Family relationship quality ratings range from 1 to 5.

The majority of students have a rating of 4.

The average rating is slightly above 4, indicating generally positive family relationships.

Self-Rated Health (health):

Self-rated health levels range from 1 to 5.

About 25% of students have a health rating of 3 or lower.

The median health rating is 4, suggesting reasonably good health.

The average health rating is approximately 3.61.

What would you have called an anomaly in one sub-sample that you wouldn’t in another?

An anomaly in one sub-sample that might not be considered an anomaly in another sub-sample depends on the characteristics and context of each sub-sample.

In Subsample 1: Anomaly detection may identify a student with a very low final grade (G3 = 0) as an anomaly because the majority of students in this subsample have relatively higher grades.

In Subsample 1: Anomaly detection may identify a student with a mother’s education level (Medu) of 0 as an anomaly since the majority have higher levels of maternal education.

In Subsample 1: Anomaly detection may identify a student with a very low self-rated health score (health = 1) as an anomaly if most students rate their health higher.

In Subsample 5: Anomaly detection may identify a student with a mother’s education level (Medu) of 0 as an anomaly since the majority have higher levels of maternal education.

In Subsample 6: Anomaly detection may identify a student with a mother’s education level (Medu) of 0 as an anomaly since the majority have higher levels of maternal education.

Are there aspects of the data that are consistent among all sub-sampl:

The sub-sampling is a random process, and each sub-sample represents a random selection of data points from the original dataset. Therefore, while certain aspects of the data structure and statistics may remain consistent, the specific values and patterns within each sub-sample are vary due to the random sampling process.

The sub-sampling creates smaller subsets of data from the original dataset, some aspects of the data might remain consistent among all sub-samples. These aspects typically include:

Column Structure: The structure of the data, such as the columns and their data types, will remain consistent across all sub-samples. This is because sub-sampling involves selecting a subset of rows from the original dataset while retaining all columns.

Column Names: The column names or variable names are the same across all sub-samples. This consistency is essential for ensuring that each sub-sample represents the same variables or attributes.

Data Types: The data types of variables (e.g., numeric, character) they are the same across sub-samples.

Basic Statistical Properties: Some basic statistical properties of the data, such as minimum, maximum, mean, median, quartiles, and counts, will be the same in the sub-samples.

Consider how this investigation affects how you might draw conclusions about the data in the future.

This investigation show that certain factors, such as family support, mother’s education, family relationship quality, self-rated health, and final grades, can vary widely between different groups of students.

I noticed that students with highly educated mothers tend to have higher grades in some subsamples, but this pattern might not hold in others. This will let me to think critically about the contextual factors influencing these patterns.

I noticed that family support seems to be a significant factor in some subsamples, and i will look deeply into how family support impacts student performance.

OTHER WAY TO SHOW THE SUMMARY STATISTICS IN A TABLE, IT WILL BE EASIER TO READ AND COMPARE:

# Extract the first subsample
subsample <- subsample_collection[[1]]

# The columns  to include in the summary

selected_columns <- c("G3", "health", "Medu", "sex", "famsup","famrel")

# Create empty data frames to store summary statistics

summary_df_numeric <- data.frame(
  Column = character(0),
  Mean = numeric(0),
  Median = numeric(0),
  Min = numeric(0),
  Max = numeric(0),
  First_Quartile = numeric(0),
  Third_Quartile = numeric(0),
  Count = numeric(0)
)

summary_df_nonnumeric <- data.frame(
  Column = character(0),
  Count = numeric(0)
)

# Loop through each column in the selected columns

for (col_name in selected_columns) {
  # Extract the data from the current column
  
  col_data <- subsample[[col_name]]
  
  if (is.numeric(col_data)) {
    # Calculate summary statistics for numeric columns
    col_mean <- mean(col_data)
    col_median <- median(col_data)
    col_min <- min(col_data)
    col_max <- max(col_data)
    col_1st_quartile <- quantile(col_data, 0.25)
    col_3rd_quartile <- quantile(col_data, 0.75)
    col_count <- length(col_data)
    
    # Add the summary statistics to the numeric data frame
    summary_df_numeric <- rbind(
      summary_df_numeric,
      data.frame(
        Column = col_name,
        Mean = col_mean,
        Median = col_median,
        Min = col_min,
        Max = col_max,
        First_Quartile = col_1st_quartile,
        Third_Quartile = col_3rd_quartile,
        Count = col_count
      )
    )
  } else {
    # For non-numeric columns, display the count of unique values
    unique_count <- length(unique(col_data))
    
    # Add the count of unique values to the non-numeric data frame
    summary_df_nonnumeric <- rbind(
      summary_df_nonnumeric,
      data.frame(
        Column = col_name,
        Count = unique_count
      )
    )
  }
}

# Print the numeric summary data frame
print(summary_df_numeric)
##      Column      Mean Median Min Max First_Quartile Third_Quartile Count
## 25%      G3 11.136364     11   0  20              9             15   198
## 25%1 health  3.540404      4   1   5              3              5   198
## 25%2   Medu  2.691919      3   0   4              2              4   198
## 25%3 famrel  3.954545      4   1   5              4              5   198
# Print the non-numeric summary data frame
print(summary_df_nonnumeric)
##   Column Count
## 1    sex     2
## 2 famsup     2

Summary for the second subsample:

# Extract the second subsample

subsample <- subsample_collection[[2]]

# The columns  to include in the summary

selected_columns <- c("G3", "health", "Medu", "sex", "famsup","famrel")

# Create empty data frames to store summary statistics

summary_df_numeric <- data.frame(
  Column = character(0),
  Mean = numeric(0),
  Median = numeric(0),
  Min = numeric(0),
  Max = numeric(0),
  First_Quartile = numeric(0),
  Third_Quartile = numeric(0),
  Count = numeric(0)
)

summary_df_nonnumeric <- data.frame(
  Column = character(0),
  Count = numeric(0)
)

# Loop through each column in the selected columns

for (col_name in selected_columns) {
  # Extract the data from the current column
  
  col_data <- subsample[[col_name]]
  
  if (is.numeric(col_data)) {
    # Calculate summary statistics for numeric columns
    col_mean <- mean(col_data)
    col_median <- median(col_data)
    col_min <- min(col_data)
    col_max <- max(col_data)
    col_1st_quartile <- quantile(col_data, 0.25)
    col_3rd_quartile <- quantile(col_data, 0.75)
    col_count <- length(col_data)
    
    # Add the summary statistics to the numeric data frame
    summary_df_numeric <- rbind(
      summary_df_numeric,
      data.frame(
        Column = col_name,
        Mean = col_mean,
        Median = col_median,
        Min = col_min,
        Max = col_max,
        First_Quartile = col_1st_quartile,
        Third_Quartile = col_3rd_quartile,
        Count = col_count
      )
    )
  } else {
    # For non-numeric columns, display the count of unique values
    unique_count <- length(unique(col_data))
    
    # Add the count of unique values to the non-numeric data frame
    summary_df_nonnumeric <- rbind(
      summary_df_nonnumeric,
      data.frame(
        Column = col_name,
        Count = unique_count
      )
    )
  }
}

# Print the numeric summary data frame
print(summary_df_numeric)
##      Column      Mean Median Min Max First_Quartile Third_Quartile Count
## 25%      G3 10.282828     10   0  20           9.00             13   198
## 25%1 health  3.565657      4   1   5           3.00              5   198
## 25%2   Medu  2.772727      3   0   4           2.00              4   198
## 25%3 famrel  3.873737      4   1   5           3.25              4   198
# Print the non-numeric summary data frame
print(summary_df_nonnumeric)
##   Column Count
## 1    sex     2
## 2 famsup     2

Summary for the Third subsample:

# Extract the third  subsample

subsample <- subsample_collection[[3]]

# The columns  to include in the summary

selected_columns <- c("G3", "health", "Medu", "sex", "famsup","famrel")

# Create empty data frames to store summary statistics

summary_df_numeric <- data.frame(
  Column = character(0),
  Mean = numeric(0),
  Median = numeric(0),
  Min = numeric(0),
  Max = numeric(0),
  First_Quartile = numeric(0),
  Third_Quartile = numeric(0),
  Count = numeric(0)
)

summary_df_nonnumeric <- data.frame(
  Column = character(0),
  Count = numeric(0)
)

# Loop through each column in the selected columns

for (col_name in selected_columns) {
  # Extract the data from the current column
  
  col_data <- subsample[[col_name]]
  
  if (is.numeric(col_data)) {
    # Calculate summary statistics for numeric columns
    col_mean <- mean(col_data)
    col_median <- median(col_data)
    col_min <- min(col_data)
    col_max <- max(col_data)
    col_1st_quartile <- quantile(col_data, 0.25)
    col_3rd_quartile <- quantile(col_data, 0.75)
    col_count <- length(col_data)
    
    # Add the summary statistics to the numeric data frame
    summary_df_numeric <- rbind(
      summary_df_numeric,
      data.frame(
        Column = col_name,
        Mean = col_mean,
        Median = col_median,
        Min = col_min,
        Max = col_max,
        First_Quartile = col_1st_quartile,
        Third_Quartile = col_3rd_quartile,
        Count = col_count
      )
    )
  } else {
    # For non-numeric columns, display the count of unique values
    unique_count <- length(unique(col_data))
    
    # Add the count of unique values to the non-numeric data frame
    summary_df_nonnumeric <- rbind(
      summary_df_nonnumeric,
      data.frame(
        Column = col_name,
        Count = unique_count
      )
    )
  }
}

# Print the numeric summary data frame
print(summary_df_numeric)
##      Column      Mean Median Min Max First_Quartile Third_Quartile Count
## 25%      G3 10.262626     10   0  19              8             14   198
## 25%1 health  3.621212      4   1   5              3              5   198
## 25%2   Medu  2.782828      3   1   4              2              4   198
## 25%3 famrel  3.843434      4   1   5              3              4   198
# Print the non-numeric summary data frame
print(summary_df_nonnumeric)
##   Column Count
## 1    sex     2
## 2 famsup     2

Summary for the fourth subsample:

# Extract the fourth subsample

subsample <- subsample_collection[[4]]

# The columns  to include in the summary

selected_columns <- c("G3", "health", "Medu", "sex", "famsup","famrel")

# Create empty data frames to store summary statistics

summary_df_numeric <- data.frame(
  Column = character(0),
  Mean = numeric(0),
  Median = numeric(0),
  Min = numeric(0),
  Max = numeric(0),
  First_Quartile = numeric(0),
  Third_Quartile = numeric(0),
  Count = numeric(0)
)

summary_df_nonnumeric <- data.frame(
  Column = character(0),
  Count = numeric(0)
)

# Loop through each column in the selected columns

for (col_name in selected_columns) {
  # Extract the data from the current column
  
  col_data <- subsample[[col_name]]
  
  if (is.numeric(col_data)) {
    # Calculate summary statistics for numeric columns
    col_mean <- mean(col_data)
    col_median <- median(col_data)
    col_min <- min(col_data)
    col_max <- max(col_data)
    col_1st_quartile <- quantile(col_data, 0.25)
    col_3rd_quartile <- quantile(col_data, 0.75)
    col_count <- length(col_data)
    
    # Add the summary statistics to the numeric data frame
    summary_df_numeric <- rbind(
      summary_df_numeric,
      data.frame(
        Column = col_name,
        Mean = col_mean,
        Median = col_median,
        Min = col_min,
        Max = col_max,
        First_Quartile = col_1st_quartile,
        Third_Quartile = col_3rd_quartile,
        Count = col_count
      )
    )
  } else {
    # For non-numeric columns, display the count of unique values
    unique_count <- length(unique(col_data))
    
    # Add the count of unique values to the non-numeric data frame
    summary_df_nonnumeric <- rbind(
      summary_df_nonnumeric,
      data.frame(
        Column = col_name,
        Count = unique_count
      )
    )
  }
}

# Print the numeric summary data frame
print(summary_df_numeric)
##      Column      Mean Median Min Max First_Quartile Third_Quartile Count
## 25%      G3 10.131313     11   0  19              8             13   198
## 25%1 health  3.494949      4   1   5              3              5   198
## 25%2   Medu  2.646465      3   0   4              2              4   198
## 25%3 famrel  4.055556      4   2   5              4              5   198
# Print the non-numeric summary data frame
print(summary_df_nonnumeric)
##   Column Count
## 1    sex     2
## 2 famsup     2

Summary for the fifth subsample:

# Extract the fifth subsample

subsample <- subsample_collection[[5]]

# The columns  to include in the summary

selected_columns <- c("G3", "health", "Medu", "sex", "famsup","famrel")

# Create empty data frames to store summary statistics

summary_df_numeric <- data.frame(
  Column = character(0),
  Mean = numeric(0),
  Median = numeric(0),
  Min = numeric(0),
  Max = numeric(0),
  First_Quartile = numeric(0),
  Third_Quartile = numeric(0),
  Count = numeric(0)
)

summary_df_nonnumeric <- data.frame(
  Column = character(0),
  Count = numeric(0)
)

# Loop through each column in the selected columns

for (col_name in selected_columns) {
  # Extract the data from the current column
  
  col_data <- subsample[[col_name]]
  
  if (is.numeric(col_data)) {
    # Calculate summary statistics for numeric columns
    col_mean <- mean(col_data)
    col_median <- median(col_data)
    col_min <- min(col_data)
    col_max <- max(col_data)
    col_1st_quartile <- quantile(col_data, 0.25)
    col_3rd_quartile <- quantile(col_data, 0.75)
    col_count <- length(col_data)
    
    # Add the summary statistics to the numeric data frame
    summary_df_numeric <- rbind(
      summary_df_numeric,
      data.frame(
        Column = col_name,
        Mean = col_mean,
        Median = col_median,
        Min = col_min,
        Max = col_max,
        First_Quartile = col_1st_quartile,
        Third_Quartile = col_3rd_quartile,
        Count = col_count
      )
    )
  } else {
    # For non-numeric columns, display the count of unique values
    unique_count <- length(unique(col_data))
    
    # Add the count of unique values to the non-numeric data frame
    summary_df_nonnumeric <- rbind(
      summary_df_nonnumeric,
      data.frame(
        Column = col_name,
        Count = unique_count
      )
    )
  }
}

# Print the numeric summary data frame
print(summary_df_numeric)
##      Column      Mean Median Min Max First_Quartile Third_Quartile Count
## 25%      G3 11.060606     11   0  19              9             14   198
## 25%1 health  3.727273      4   1   5              3              5   198
## 25%2   Medu  2.787879      3   0   4              2              4   198
## 25%3 famrel  4.050505      4   2   5              4              5   198
# Print the non-numeric summary data frame
print(summary_df_nonnumeric)
##   Column Count
## 1    sex     2
## 2 famsup     2

Summary for the sixth subsample:

# Extract the sixth subsample

subsample <- subsample_collection[[6]]

# The columns  to include in the summary

selected_columns <- c("G3", "health", "Medu", "sex", "famsup","famrel")

# Create empty data frames to store summary statistics

summary_df_numeric <- data.frame(
  Column = character(0),
  Mean = numeric(0),
  Median = numeric(0),
  Min = numeric(0),
  Max = numeric(0),
  First_Quartile = numeric(0),
  Third_Quartile = numeric(0),
  Count = numeric(0)
)

summary_df_nonnumeric <- data.frame(
  Column = character(0),
  Count = numeric(0)
)

# Loop through each column in the selected columns

for (col_name in selected_columns) {
  # Extract the data from the current column
  
  col_data <- subsample[[col_name]]
  
  if (is.numeric(col_data)) {
    # Calculate summary statistics for numeric columns
    col_mean <- mean(col_data)
    col_median <- median(col_data)
    col_min <- min(col_data)
    col_max <- max(col_data)
    col_1st_quartile <- quantile(col_data, 0.25)
    col_3rd_quartile <- quantile(col_data, 0.75)
    col_count <- length(col_data)
    
    # Add the summary statistics to the numeric data frame
    summary_df_numeric <- rbind(
      summary_df_numeric,
      data.frame(
        Column = col_name,
        Mean = col_mean,
        Median = col_median,
        Min = col_min,
        Max = col_max,
        First_Quartile = col_1st_quartile,
        Third_Quartile = col_3rd_quartile,
        Count = col_count
      )
    )
  } else {
    # For non-numeric columns, display the count of unique values
    unique_count <- length(unique(col_data))
    
    # Add the count of unique values to the non-numeric data frame
    summary_df_nonnumeric <- rbind(
      summary_df_nonnumeric,
      data.frame(
        Column = col_name,
        Count = unique_count
      )
    )
  }
}

# Print the numeric summary data frame
print(summary_df_numeric)
##      Column      Mean Median Min Max First_Quartile Third_Quartile Count
## 25%      G3 10.287879     11   0  20              8             14   198
## 25%1 health  3.545455      4   1   5              2              5   198
## 25%2   Medu  2.681818      3   0   4              2              4   198
## 25%3 famrel  3.909091      4   1   5              3              4   198
# Print the non-numeric summary data frame
print(summary_df_nonnumeric)
##   Column Count
## 1    sex     2
## 2 famsup     2

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.