Part 1
How many cases were included in the data?
By using the this function, I can see the n samples of the data set
library(psych)
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
As we can see, there are n = 150 of Irish samples in the data set.
How many numerical variables are included in the data? Indicate what they are, and if they are continuous or discrete.
Using str(iris), we can see that there is 5 variables in the data set.
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
For numerical variables, we will filter it using sapply() then we will have:
sapply(iris, is.numeric)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## TRUE TRUE TRUE TRUE FALSE
In these 5 variables, there is only Species which return the values False meaning it is the only variable that is not numeric. In addition to that, all 4 variables is continuous
How many categorical variables are included in the data, and what are they? List the corresponding levels (categories).
We know from our last questions that there should be 1 categorical variables included in the data, and we can see this more clearly when using the command summary(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa :50
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 versicolor:50
Median :5.800 Median :3.000 Median :4.350 Median :1.300 virginica :50
Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
Based on that, we can see the 3 values in the Species variables are: “setosa”, “versicolor”, “virginica”.
Part 2
The data set I will be using is cars which measures speed and stopping distance of cars. The data set is cross sectional data types.
describe(cars)
## vars n mean sd median trimmed mad min max range skew kurtosis
## speed 1 50 15.40 5.29 15 15.47 5.93 4 25 21 -0.11 -0.67
## dist 2 50 42.98 25.77 36 40.88 23.72 2 120 118 0.76 0.12
## se
## speed 0.75
## dist 3.64
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
View(cars)
Before plotting a scatter plot I will have to download 2 libraries:
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggformula)
## Loading required package: ggplot2
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
## Loading required package: scales
##
## Attaching package: 'scales'
## The following objects are masked from 'package:psych':
##
## alpha, rescale
## Loading required package: ggiraph
## Loading required package: ggridges
##
## New to ggformula? Try the tutorials:
## learnr::run_tutorial("introduction", package = "ggformula")
## learnr::run_tutorial("refining", package = "ggformula")
After that, I need to run this code and it will give me the scatterplot:
example <- cars #Assigning cars data set to example as a filler then use it with the gf_point command to graph the scatter plot
example %>%
gf_point(dist ~ speed, title = "Speed vs Distance")