data()
This dataset describes the speed at which cars were travelling and the distance it took them to come to a stop at said speed. The data was collected in the 1920s and used 50 separate observations.
speed = speed (mph)
dist = distance (ft)
col = “blue”)
?cars #find more info on dataset
data("cars") #Loads the dataset for analysis
cars #View dataset
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
## 7 10 18
## 8 10 26
## 9 10 34
## 10 11 17
## 11 11 28
## 12 12 14
## 13 12 20
## 14 12 24
## 15 12 28
## 16 13 26
## 17 13 34
## 18 13 34
## 19 13 46
## 20 14 26
## 21 14 36
## 22 14 60
## 23 14 80
## 24 15 20
## 25 15 26
## 26 15 54
## 27 16 32
## 28 16 40
## 29 17 32
## 30 17 40
## 31 17 50
## 32 18 42
## 33 18 56
## 34 18 76
## 35 18 84
## 36 19 36
## 37 19 46
## 38 19 68
## 39 20 32
## 40 20 48
## 41 20 52
## 42 20 56
## 43 20 64
## 44 22 66
## 45 23 54
## 46 24 70
## 47 24 92
## 48 24 93
## 49 24 120
## 50 25 85
str(cars) #Summarize data used
## 'data.frame': 50 obs. of 2 variables:
## $ speed: num 4 4 7 7 8 9 10 10 10 11 ...
## $ dist : num 2 10 4 22 16 10 18 26 34 17 ...
plot(cars$speed, cars$dist,
main = "Distance to Stop",
ylab = "Distance (ft)",
xlab = "Speed (mph)",
pch = 19, #changes symbol used
col = "blue")
As can be seen from the dataset and graph. The data collected contains 50 observations from around the same time period. Thus, it must be cross-sectional data.
As per R’s provided description, the dataset describes “The revenue passenger miles flown by commercial airlines in the United States for each year from 1937 to 1960.”
?airmiles
airmiles
## Time Series:
## Start = 1937
## End = 1960
## Frequency = 1
## [1] 412 480 683 1052 1385 1418 1634 2178 3362 5948 6109 5981
## [13] 6753 8003 10566 12528 14760 16769 19819 22362 25340 25343 29269 30514
Since this is not in a data frame, we must convert it to one. The method for this is as follows:
data("airmiles")
df <- data.frame(Year = seq(1937, 1960), Airmiles = airmiles)
df
## Year Airmiles
## 1 1937 412
## 2 1938 480
## 3 1939 683
## 4 1940 1052
## 5 1941 1385
## 6 1942 1418
## 7 1943 1634
## 8 1944 2178
## 9 1945 3362
## 10 1946 5948
## 11 1947 6109
## 12 1948 5981
## 13 1949 6753
## 14 1950 8003
## 15 1951 10566
## 16 1952 12528
## 17 1953 14760
## 18 1954 16769
## 19 1955 19819
## 20 1956 22362
## 21 1957 25340
## 22 1958 25343
## 23 1959 29269
## 24 1960 30514
Now we can use a graph and some other code to summarize the data
str(airmiles)
## Time-Series [1:24] from 1937 to 1960: 412 480 683 1052 1385 ...
ts.plot(df$Airmiles,
main = "Trend in Airmiles Over Time",
ylab = "Airmiles",
type = "b",
col = "blue")
As we can see, the dataset is already described as a time series. Additionally, the dataset describes how one individual trended from 1937 to 1960
Covariance is the statistical measure that quantifies how two random variables change together. It indicates whether there is a positive, negative, or no relationship between the variations in two sets of data points.
Variance is the statistical measure that quantifies the degree of spread or dispersion in a set of data points. It represents how individual data points deviate from the mean of the data set.
As mentioned earlier, cov(y,x) measures how y and x change together and var(x) measures the spread of x. By dividing covariance over variance, we scale the changes in the two variables with a consistent spread of the values of the independent variable.
We’ll demonstrate this using the cars dataset from earlier. If correct, Cov(speed,dist)/Var(speed) will equal\(\beta_1\) from the regression
#Find the covariance
?cov
covariance <- cov(cars$speed,cars$dist)
cat("Covariance:", covariance)
## Covariance: 109.9469
#Find the variance
variance <- var(cars$speed)
cat("Variance:", variance)
## Variance: 27.95918
#Find beta1
beta1 <-covariance/variance
cat("Slope Estimate:", beta1)
## Slope Estimate: 3.932409
#Run regression
model <- lm(dist ~ speed, data = cars)
model
##
## Call:
## lm(formula = dist ~ speed, data = cars)
##
## Coefficients:
## (Intercept) speed
## -17.579 3.932
We can see that the slope estimate (beta1) equals the coefficient for speed from the regression. This shows that this proves it is the correct slope estimator in this instance.