midwest

Dataset này gồm có 28 biến:

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.1
data(midwest)
linear_regression <- function(data, x_col, y_col){
  lm_model <- lm(formula = paste(y_col,"~", x_col), data = data)
  return(summary(lm_model))
  result <- linear_regression_and_plot(data, "X", "Y")
}
# Vẽ đồ thị phân tán 
ggplot(midwest, aes(x = "x_col", y = "y_col")) +
  geom_point() +
  labs(title = "Đồ thị phân tán giữa x và y",
       x = "x_col",
       y = "y_col") +
  theme_minimal()

# Sử dụng bộ dữ liệu midwest
data("midwest")
lm_model <- lm(popadults ~ poppovertyknown, data = midwest)
summary(lm_model)
## 
## Call:
## lm(formula = popadults ~ poppovertyknown, data = midwest)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -37266   -372    340    730  36807 
## 
## Coefficients:
##                   Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)     -2.172e+02  1.784e+02   -1.217    0.224    
## poppovertyknown  6.534e-01  5.801e-04 1126.339   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3552 on 435 degrees of freedom
## Multiple R-squared:  0.9997, Adjusted R-squared:  0.9997 
## F-statistic: 1.269e+06 on 1 and 435 DF,  p-value: < 2.2e-16
# Vẽ biểu đồ phân tán
ggplot(midwest, aes(x = poppovertyknown, y = popadults)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, color = "black") +
  labs(title = "Biểu đồ phân tán giữa popadults và poppovertyknown",
       x = "Poverty Known Population",
       y = "Adult Population") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

Kiểm định sự tự tương quan

Sử dụng bộ dữ liệu midwest

data("midwest")
# Tính hệ số tương quan Pearson giữa popadults và poppovertyknown
correlation <- cor(midwest$popadults, midwest$poppovertyknown)
print(correlation)
## [1] 0.9998286