Part 1
describe(iris)
## vars n mean sd median trimmed mad min max range skew
## Sepal.Length 1 150 5.84 0.83 5.80 5.81 1.04 4.3 7.9 3.6 0.31
## Sepal.Width 2 150 3.06 0.44 3.00 3.04 0.44 2.0 4.4 2.4 0.31
## Petal.Length 3 150 3.76 1.77 4.35 3.76 1.85 1.0 6.9 5.9 -0.27
## Petal.Width 4 150 1.20 0.76 1.30 1.18 1.04 0.1 2.5 2.4 -0.10
## Species* 5 150 2.00 0.82 2.00 2.00 1.48 1.0 3.0 2.0 0.00
## kurtosis se
## Sepal.Length -0.61 0.07
## Sepal.Width 0.14 0.04
## Petal.Length -1.42 0.14
## Petal.Width -1.36 0.06
## Species* -1.52 0.07
There are 150 cases in the data. One for each flower species. Using the psych library and describe function, it shows that n = 150, meaning 150 iris samples in the data set.
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
There are 4 numerical variables in the data: Sepal.Length,Sepal.Width,Petal.Length, and Petal.Width. They are all continuous becuase they move on a range of values.
unique(iris$Species)
## [1] setosa versicolor virginica
## Levels: setosa versicolor virginica
There is one categorial data which is “Species” which has 3 levels(values): setosa, veriscolor, and virginica.
Part 2
I picked the Seatbelt data set which shows the monthly totals of car drivers in Great Britain killed or seriously injured Jan 1969 to Dec 1984. This data set falls into the category of time series as it tracks a singular thing (road casualties) across a time period of 15 years.
data("Seatbelts")
describe(Seatbelts)
## vars n mean sd median trimmed mad min
## DriversKilled 1 192 122.80 25.38 118.5 121.36 22.98 60.00
## drivers 2 192 1670.31 289.61 1631.0 1656.51 259.46 1057.00
## front 3 192 837.22 175.10 828.5 836.32 171.98 426.00
## rear 4 192 401.21 83.10 401.5 398.71 83.77 224.00
## kms 5 192 14993.60 2938.05 14987.0 14984.53 3368.47 7685.00
## PetrolPrice 6 192 0.10 0.01 0.1 0.10 0.02 0.08
## VanKilled 7 192 9.06 3.64 8.0 9.01 4.45 2.00
## law 8 192 0.12 0.33 0.0 0.03 0.00 0.00
## max range skew kurtosis se
## DriversKilled 198.00 138.00 0.53 -0.07 1.83
## drivers 2654.00 1597.00 0.53 0.07 20.90
## front 1299.00 873.00 0.05 -0.46 12.64
## rear 646.00 422.00 0.34 0.10 6.00
## kms 21626.00 13941.00 0.01 -0.70 212.04
## PetrolPrice 0.13 0.05 -0.13 -1.05 0.00
## VanKilled 17.00 15.00 0.13 -0.97 0.26
## law 1.00 1.00 2.32 3.42 0.02
This dataset has 7 numeric variables: DriversKilled,drivers,front,rear,kms,PetrolPrice,and VanKilled. kms and PetrolPrice are continuous, the rest are discrete. This data set has 1 categorical variable: law. It tells whether the law to wear a seat belt was effective that month or not.
install.packages("ggplot2")
## Warning: package 'ggplot2' is in use and will not be installed
now I will make the time plot.I will show driver severe casualties from 1969 to 1984. I will add a vertical line for the seat belt law that was introduced in Jan 31, 1983.
seatbelts_df <- as.data.frame(Seatbelts)
seatbelts_df$Date <- seq(as.Date("1969-01-01"), as.Date("1984-12-01"), by="month")
ggplot(seatbelts_df, aes(x = Date)) +
# First line: Total severe driver casualties
geom_line(aes(y = drivers), linewidth = 0.8) +
# vertical policy line for the seat belt law (Jan 31, 1983)
geom_vline(xintercept = as.numeric(as.Date("1983-01-31")),
color = "#d62728", linetype = "dashed") +
# Label the policy line
annotate("text", x = as.Date("1982-06-01"), y = 2400,
label = "Seat Belt Law\nIntroduced", color = "#d62728", hjust = 1) +
labs(
title = "Monthly Road Casualties in Great Britain (1969–1984)",
x = "Year",
y = "Monthly Count"
) +
theme(
plot.title = element_text(face = "bold"),
legend.position = "bottom"
)
## Warning in scale_x_date(): A <numeric> value was passed to a Date scale.
## ℹ The value was converted to a <Date> object.
seatbelts_df %>%
filter(Date >= "1982-09-01" & Date <= "1983-05-01") %>%
select(Date, drivers, law)
## Date drivers law
## 1 1982-09-01 1594 0
## 2 1982-10-01 1850 0
## 3 1982-11-01 1998 0
## 4 1982-12-01 2079 0
## 5 1983-01-01 1494 0
## 6 1983-02-01 1057 1
## 7 1983-03-01 1218 1
## 8 1983-04-01 1168 1
## 9 1983-05-01 1236 1
The graph show a positive affect of the seat belt law as the number of severe casualties dramatically drops with an immediate affect. You can see from the data that the numbers dropped from 2079 to to 1057 in a matter of 2 months. Towards 1985, the numbers drastically rise again, reasons to that happening can be lack of police enforcement, and people’s disregard the importance of the seat belt and how it can save life.