if (!require(AER)) install.packages("AER")
## Loading required package: AER
## Loading required package: car
## Loading required package: carData
## Loading required package: lmtest
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: survival
library(AER)
data("Affairs", package = "AER")  
head(Affairs)
##    affairs gender age yearsmarried children religiousness education occupation
## 4        0   male  37        10.00       no             3        18          7
## 5        0 female  27         4.00       no             4        14          6
## 11       0 female  32        15.00      yes             1        12          1
## 16       0   male  57        15.00      yes             5        18          6
## 23       0   male  22         0.75       no             2        17          6
## 29       0 female  32         1.50       no             2        17          5
##    rating
## 4       4
## 5       4
## 11      4
## 16      5
## 23      3
## 29      5
data("mtcars")
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
# mtcars Description: The mtcars dataset comprises 32 observations on 11 variables, detailing aspects like miles per gallon (MPG), number of cylinders, horsepower, and weight for various car models. This dataset provides insights into how different design choices impact vehicle performance.
# Affairs Description: The Affairs dataset includes information on extramarital affairs, rating of marriage, age, years married, and other demographic factors for a sample of individuals. It offers a glimpse into personal and social factors associated with marital satisfaction and infidelity.
library(ggplot2)
ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  labs(title = "MPG vs Horsepower",
       x = "Horsepower",
       y = "Miles per Gallon") +
  theme_minimal()

ggplot(Affairs, aes(x = rating, y = affairs)) +
  geom_jitter(width = 0.2, height = 0.2, alpha = 0.5) +
  labs(title = "Marriage Rating vs Number of Affairs",
       x = "Marriage Rating",
       y = "Number of Affairs") +
  theme_minimal()

# mtcars: This is a cross-sectional dataset because it captures data at a single point in time, showing the relationship between car specifications and their performance.

# Affairs: This dataset can be considered cross-sectional as well, since it collects information about individuals' marital satisfaction and incidents of affairs at one point in time.
# Covariance is a measure that indicates the extent to which two variables change together. If the value is positive, it means the variables tend to move in the same direction, whereas a negative value indicates they move in opposite directions.

# Variance quantifies the variation of a dataset from its mean, essentially describing the spread of the data points.
# The slope coefficient in a simple linear regression, calculated as Cov(y,x)/Var(x), quantifies the average change in the dependent variable for a one-unit change in the independent variable. This works because dividing the covariance by the variance of x normalizes the relationship between x and y, thereby quantifying the direct influence of x on y in linear terms.
x <- mtcars$mpg
y <- mtcars$hp

cov_yx <- cov(y, x)
var_x <- var(x)

slope_formula <- cov_yx / var_x

model <- lm(y ~ x)
slope_regression <- coef(model)[2]

print(paste(slope_formula))
## [1] "-8.82973099206822"
print(paste(slope_regression))
## [1] "-8.82973099206822"