Week 1 Discussion

Part I:

Question 1: How many cases were included in the data?

With 50 flowers included from each species, there are a total of 150 cases in this dataset.

Question 2: How many numerical variables are included in the data? Indicate what they are, and if they are continuous or discrete.

There are four numerical variables in this dataset. These variables are sepal length, sepal width, petal length, and petal width. These variables are continuous.

Question 3: How many categorical variables are included in the data, and what are they? List the corresponding levels (categories).

There is only one categorical variable in this dataset: the species of the iris flower. Each species represents a different level, so there are three levels for this variable.

library(datasets)
data(iris)

Analyze data using R libraries

library(psych)
installed.packages("stargazer")
##      Package LibPath Version Priority Depends Imports LinkingTo Suggests
##      Enhances License License_is_FOSS License_restricts_use OS_type Archs
##      MD5sum NeedsCompilation Built Published
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
describeBy(iris[, 1:4], group = iris$Species)
## 
##  Descriptive statistics by group 
## group: setosa
##              vars  n mean   sd median trimmed  mad min max range skew kurtosis
## Sepal.Length    1 50 5.01 0.35    5.0    5.00 0.30 4.3 5.8   1.5 0.11    -0.45
## Sepal.Width     2 50 3.43 0.38    3.4    3.42 0.37 2.3 4.4   2.1 0.04     0.60
## Petal.Length    3 50 1.46 0.17    1.5    1.46 0.15 1.0 1.9   0.9 0.10     0.65
## Petal.Width     4 50 0.25 0.11    0.2    0.24 0.00 0.1 0.6   0.5 1.18     1.26
##                se
## Sepal.Length 0.05
## Sepal.Width  0.05
## Petal.Length 0.02
## Petal.Width  0.01
## ------------------------------------------------------------ 
## group: versicolor
##              vars  n mean   sd median trimmed  mad min max range  skew kurtosis
## Sepal.Length    1 50 5.94 0.52   5.90    5.94 0.52 4.9 7.0   2.1  0.10    -0.69
## Sepal.Width     2 50 2.77 0.31   2.80    2.78 0.30 2.0 3.4   1.4 -0.34    -0.55
## Petal.Length    3 50 4.26 0.47   4.35    4.29 0.52 3.0 5.1   2.1 -0.57    -0.19
## Petal.Width     4 50 1.33 0.20   1.30    1.32 0.22 1.0 1.8   0.8 -0.03    -0.59
##                se
## Sepal.Length 0.07
## Sepal.Width  0.04
## Petal.Length 0.07
## Petal.Width  0.03
## ------------------------------------------------------------ 
## group: virginica
##              vars  n mean   sd median trimmed  mad min max range  skew kurtosis
## Sepal.Length    1 50 6.59 0.64   6.50    6.57 0.59 4.9 7.9   3.0  0.11    -0.20
## Sepal.Width     2 50 2.97 0.32   3.00    2.96 0.30 2.2 3.8   1.6  0.34     0.38
## Petal.Length    3 50 5.55 0.55   5.55    5.51 0.67 4.5 6.9   2.4  0.52    -0.37
## Petal.Width     4 50 2.03 0.27   2.00    2.03 0.30 1.4 2.5   1.1 -0.12    -0.75
##                se
## Sepal.Length 0.09
## Sepal.Width  0.05
## Petal.Length 0.08
## Petal.Width  0.04
stargazer(iris[iris$Species == "setosa", 1:4], type = "text", title = "setosa")
## 
## setosa
## ==========================================
## Statistic    N  Mean  St. Dev.  Min   Max 
## ------------------------------------------
## Sepal.Length 50 5.006  0.352   4.300 5.800
## Sepal.Width  50 3.428  0.379   2.300 4.400
## Petal.Length 50 1.462  0.174   1.000 1.900
## Petal.Width  50 0.246  0.105   0.100 0.600
## ------------------------------------------
stargazer(iris[iris$Species == "versicolor", 1:4], type = "text", title = "versicolor")
## 
## versicolor
## ==========================================
## Statistic    N  Mean  St. Dev.  Min   Max 
## ------------------------------------------
## Sepal.Length 50 5.936  0.516   4.900 7.000
## Sepal.Width  50 2.770  0.314   2.000 3.400
## Petal.Length 50 4.260  0.470   3.000 5.100
## Petal.Width  50 1.326  0.198   1.000 1.800
## ------------------------------------------
stargazer(iris[iris$Species == "virginica", 1:4], type = "text", title = "virginica")
## 
## virginica
## ==========================================
## Statistic    N  Mean  St. Dev.  Min   Max 
## ------------------------------------------
## Sepal.Length 50 6.588  0.636   4.900 7.900
## Sepal.Width  50 2.974  0.322   2.200 3.800
## Petal.Length 50 5.552  0.552   4.500 6.900
## Petal.Width  50 2.026  0.275   1.400 2.500
## ------------------------------------------

Comparing the three species of iris flower, we can see that the petal length and width increased between species from setosa (median: 1.5 cm, 0.2 cm), to versicolor (median: 4.35 cm, 1.30 cm), to viriginica (median: 5.55 cm, 2.00 cm). We see the same pattern for sepal length (setosa [median: 5.0 cm], versicolor [median: 5.90 cm], virginica [median: 6.50 cm]). However, this pattern does not continue for sepal width, with setosa being the widest (median: 3.4 cm), followed by viriginca (median: 3.00 cm), then versicolor (median: 2.80 cm)

library(datasets)
data("USArrests")

Part II:

I chose the USArrests dataset from the base R library. This dataset contains cross-sectional data comparing the rate of arrests for murder, assault, and rape charges across the different states in the USA in 1973, as well as the percentage of each state’s population that lives in urban areas.

attach(USArrests)
plot(USArrests$UrbanPop, USArrests$Murder, main = "Murder Charge Arrest Rate by Urbanization %",
     xlab = "Urbanization % ", ylab = "Murder Charge Arrests per 100,000 ")

model <- lm(Murder ~ UrbanPop, data = USArrests)
abline(model, col = "red", lwd = 2)
r <- cor(USArrests$UrbanPop, USArrests$Murder)
r_squared <- summary(model)$r.squared
legend("topleft",
       legend = c(paste("r =", round(r, 3)),
                  paste("R² =", round(r_squared, 3))),
       bty = "n")

plot(USArrests$UrbanPop, USArrests$Assault, main = "Assault Charge Arrest Rate by Urbanization %",
     xlab = "Urbanization % ", ylab = "Assault Charge Arrests per 100,000 ")

model2 <- lm(Assault ~ UrbanPop, data = USArrests)
abline(model2, col = "red", lwd = 2)
r2 <- cor(USArrests$UrbanPop, USArrests$Assault)
r_squared2 <- summary(model2)$r.squared
legend("topleft",
       legend = c(paste("r =", round(r2, 3)),
                  paste("R² =", round(r_squared2, 3))),
       bty = "n")

plot(USArrests$UrbanPop, USArrests$Rape, main = "Rape Charge Arrest Rate by Urbanization %",
     xlab = "Urbanization % ", ylab = "Rape Charge Arrests per 100,000 ")

model3 <- lm(Rape ~ UrbanPop, data = USArrests)
abline(model3, col = "red", lwd = 2)
r3 <- cor(USArrests$UrbanPop, USArrests$Rape)
r_squared3 <- summary(model3)$r.squared
legend("topleft",
       legend = c(paste("r =", round(r3, 3)),
                  paste("R² =", round(r_squared3, 3))),
       bty = "n")

The relationship between urbanization and arrests for violent crime charges is very weak. However, of the three types of crime, the one with the strongest positive correlation with urbanization is the number of arrests for rape charges per 100,000.