I. OpenStats Chapter 1, Problem 1.9
nrow(iris)
## [1] 150
There are 150 cases included in the data.
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
sum(sapply(iris,is.numeric))
## [1] 4
There are 4 numerical variables in the data. They are Sepal.Length, Sepal.Width, Petal.Length and Petal.Width.All four variables are continuous.
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
unique(iris$Species)
## [1] setosa versicolor virginica
## Levels: setosa versicolor virginica
There is one categorical variable (Species) with 3 levels: setosa, versicolor, and virginica.
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
The descriptive statistics of the dataset show that there is significant variation across the variables, with the most variation in petal length (range of 5.9). Conversely, petal and sepal width have the least variation, each with a range of 2.4. Petal length and width are negatively skewed, indicating a tendency towards smaller values, whereas sepal length and width are positively skewed, indicating a tendency towards larger values. However, overall, all 4 numeric variables are not strongly skewed. Petal width, which has a skew of -.10 is the closest to 0, indicating the distribution of data in this variable is approximately symmetric. Sepal width has the least variation across the variable (standard deviation of 0.44) and petal length has the greatest variation (standard deviation of 1.77). Effectively, this means that sepal widths tend to be much closer to the mean (3.06) than petal lengths do. The 3 categories in the categorical variable (species) are represented equally in the dataset,
I chose to use the Presidents dataset, which shows quarterly presidential approval ratings from 1945-1974. This would be time series data, as it tracks the changes of one government office (the president) over a period of time.
presidents
## Qtr1 Qtr2 Qtr3 Qtr4
## 1945 NA 87 82 75
## 1946 63 50 43 32
## 1947 35 60 54 55
## 1948 36 39 NA NA
## 1949 69 57 57 51
## 1950 45 37 46 39
## 1951 36 24 32 23
## 1952 25 32 NA 32
## 1953 59 74 75 60
## 1954 71 61 71 57
## 1955 71 68 79 73
## 1956 76 71 67 75
## 1957 79 62 63 57
## 1958 60 49 48 52
## 1959 57 62 61 66
## 1960 71 62 61 57
## 1961 72 83 71 78
## 1962 79 71 62 74
## 1963 76 64 62 57
## 1964 80 73 69 69
## 1965 71 64 69 62
## 1966 63 46 56 44
## 1967 44 52 38 46
## 1968 36 49 35 44
## 1969 59 65 65 56
## 1970 66 53 61 52
## 1971 51 48 54 49
## 1972 49 61 NA NA
## 1973 68 44 40 27
## 1974 28 25 24 24
plot(presidents,xlab="Year",ylab="Approval rating (%)",main="Presidential Approval Rating, 1945-1974")
I noticed there were certain quarters where no data was available. I also created a time series chart showing the presidential approval rating, assuming that in quarters where the data was NA, the approval rating was equal to the the approval rating in the last quarter for which there was available data.
data("presidents")
library(zoo)
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
filled_presidents<-na.locf(presidents)
plot(filled_presidents,xlab="Year",ylab="Approval Rating (%)",main="NA.LOCF Presidential Approval Rating")