data(mtcars)

model_lm <- lm(mpg ~ hp + wt, data = mtcars)

summary(model_lm)
## 
## Call:
## lm(formula = mpg ~ hp + wt, data = mtcars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.941 -1.600 -0.182  1.050  5.854 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 37.22727    1.59879  23.285  < 2e-16 ***
## hp          -0.03177    0.00903  -3.519  0.00145 ** 
## wt          -3.87783    0.63273  -6.129 1.12e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.593 on 29 degrees of freedom
## Multiple R-squared:  0.8268, Adjusted R-squared:  0.8148 
## F-statistic: 69.21 on 2 and 29 DF,  p-value: 9.109e-12
r_squared <- summary(model_lm)$r.squared
cat("R bình phương:", r_squared, "\n")
## R bình phương: 0.8267855
predicted_mpg <- predict(model_lm, newdata = mtcars)

rmse <- sqrt(mean((mtcars$mpg - predicted_mpg)^2))
cat("RMSE:", rmse, "\n")
## RMSE: 2.468854
if (!require(arm)) install.packages("arm", dependencies = TRUE)
## Loading required package: arm
## Warning: package 'arm' was built under R version 4.4.3
## Loading required package: MASS
## Loading required package: Matrix
## Loading required package: lme4
## Warning: package 'lme4' was built under R version 4.4.3
## 
## arm (Version 1.14-4, built: 2024-4-1)
## Working directory is D:/Python
library(arm)

data(iris)

iris_binary <- iris[iris$Species %in% c("setosa", "versicolor"), ]

iris_binary$Species <- as.numeric(iris_binary$Species == "versicolor")

model_bayes <- bayesglm(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, 
                        data = iris_binary, family = binomial)

predicted_probs <- predict(model_bayes, type = "response")

predicted_classes <- ifelse(predicted_probs > 0.5, 1, 0)

conf_matrix <- table(Predicted = predicted_classes, Actual = iris_binary$Species)
print(conf_matrix)
##          Actual
## Predicted  0  1
##         0 50  0
##         1  0 50
accuracy <- sum(diag(conf_matrix)) / sum(conf_matrix)
cat("Độ chính xác:", accuracy, "\n")
## Độ chính xác: 1
library(ggplot2)

ggplot(iris_binary, aes(x = Petal.Length, y = Petal.Width, color = as.factor(Species))) +
  geom_point(size = 3) +
  theme_minimal() +
  labs(title = "Kiểm tra phân tách hoàn toàn giữa Setosa và Versicolor")