To determine the types of variables and levels of measurement for “PassengerId” and “Age,” we need to inspect the data. You can use the following code to examine the structure of the data frame:
mydata <- read.csv("train.csv")
str(mydata[c("PassengerId", "Age")])
## 'data.frame': 891 obs. of 2 variables:
## $ PassengerId: int 1 2 3 4 5 6 7 8 9 10 ...
## $ Age : num 22 38 26 35 35 NA 54 2 27 14 ...
To find the variable with the most missing observations, you can use the following code:
missing_counts <- colSums(is.na(mydata))
most_missing_variable <- names(missing_counts)[which.max(missing_counts)]
missing_counts
## PassengerId Survived Pclass Name Sex Age
## 0 0 0 0 0 177
## SibSp Parch Ticket Fare Cabin Embarked
## 0 0 0 0 0 0
most_missing_variable
## [1] "Age"
The above code calculates the count of missing values for each variable and identifies the variable with the maximum count. The variable stored in the most_missing_variable variable will be the one with the most missing observations.
To impute missing observations for “Age,” “SibSp,” and “Parch” with the column median, you can use the following code:
mydata$Age[is.na(mydata$Age)] <- median(mydata$Age, na.rm = TRUE)
mydata$SibSp[is.na(mydata$SibSp)] <- median(mydata$SibSp, na.rm = TRUE)
mydata$Parch[is.na(mydata$Parch)] <- median(mydata$Parch, na.rm = TRUE)
head(mydata)
## PassengerId Survived Pclass
## 1 1 0 3
## 2 2 1 1
## 3 3 1 3
## 4 4 1 1
## 5 5 0 3
## 6 6 0 3
## Name Sex Age SibSp Parch
## 1 Braund, Mr. Owen Harris male 22 1 0
## 2 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38 1 0
## 3 Heikkinen, Miss. Laina female 26 0 0
## 4 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35 1 0
## 5 Allen, Mr. William Henry male 35 0 0
## 6 Moran, Mr. James male 28 0 0
## Ticket Fare Cabin Embarked
## 1 A/5 21171 7.2500 S
## 2 PC 17599 71.2833 C85 C
## 3 STON/O2. 3101282 7.9250 S
## 4 113803 53.1000 C123 S
## 5 373450 8.0500 S
## 6 330877 8.4583 Q
The code replaces the missing values in each variable with its respective column median.
To install and use the “psych” package for descriptive statistics, you can follow these steps:
# Load the psych package
library(psych)
## Warning: package 'psych' was built under R version 4.3.3
# Descriptive statistics for Age, SibSp, and Parch
describe(mydata[c("Age", "SibSp", "Parch")])
## vars n mean sd median trimmed mad min max range skew kurtosis se
## Age 1 891 29.36 13.02 28 28.83 8.9 0.42 80 79.58 0.51 0.97 0.44
## SibSp 2 891 0.52 1.10 0 0.27 0.0 0.00 8 8.00 3.68 17.73 0.04
## Parch 3 891 0.38 0.81 0 0.18 0.0 0.00 6 6.00 2.74 9.69 0.03
The describe() function from the “psych” package provides various summary statistics for the specified variables. Observe the output to gain insights into the distribution and characteristics of the variables.```{r}
To create a cross-tabulation of “Survived” and “Sex,” you can use the following code:
cross_tab <- table(mydata$Survived, mydata$Sex)
cross_tab
##
## female male
## 0 81 468
## 1 233 109
The table() function generates a contingency table that shows the counts of each combination of the two variables. Analyze the resulting table to observe the relationship between survival and sex.
To create notched boxplots for “Survived” and “Age,” you can use the following code:
boxplot(mydata$Age ~ mydata$Survived, notch = TRUE, horizontal = TRUE)
The boxplot() function creates side-by-side boxplots for the “Age”
variable, grouped by the “Survived” variable. The notches provide a
visual comparison of the medians and their confidence intervals. Observe
the plot to identify any differences in age distribution between the two
survival groups.