This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
# 1. Load thư viện và dữ liệu
library(MASS)
data("Boston")
# 2. Xây dựng mô hình hồi quy tuyến tính đơn biến
# medv: biến phụ thuộc, rm: biến độc lập
model1 <- lm(medv ~ rm, data = Boston)
# Xem tóm tắt mô hình
summary(model1)
##
## Call:
## lm(formula = medv ~ rm, data = Boston)
##
## Residuals:
## Min 1Q Median 3Q Max
## -23.346 -2.547 0.090 2.986 39.433
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -34.671 2.650 -13.08 <2e-16 ***
## rm 9.102 0.419 21.72 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.616 on 504 degrees of freedom
## Multiple R-squared: 0.4835, Adjusted R-squared: 0.4825
## F-statistic: 471.8 on 1 and 504 DF, p-value: < 2.2e-16
# 3. Kiểm tra giả định phân phối chuẩn của residuals
# Cách 1: Sử dụng biểu đồ Q-Q Plot
plot(model1, which = 2)
# Cách 2: Vẽ Histogram của residuals
residuals_data <- residuals(model1)
hist(residuals_data, breaks = 20, main = "Histogram of Residuals",
xlab = "Residuals", col = "lightblue")
# Cách 3: Kiểm định Shapiro-Wilk (nếu n < 5000)
shapiro.test(residuals_data)
##
## Shapiro-Wilk normality test
##
## data: residuals_data
## W = 0.92046, p-value = 1.088e-15
# CÂU 2:
# 1. Load thư viện ISLR chứa tập dữ liệu Default
# Nếu chưa có, hãy chạy: install.packages("ISLR")
library(ISLR)
data("Default")
# 2. Xây dựng mô hình hồi quy Logistic
# Dự báo default dựa trên tất cả các biến khác (student, balance, income)
model2 <- glm(default ~ student + balance + income,
data = Default,
family = binomial)
# 3. Tính hệ số hồi quy và ý nghĩa thống kê
summary(model2)
##
## Call:
## glm(formula = default ~ student + balance + income, family = binomial,
## data = Default)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.087e+01 4.923e-01 -22.080 < 2e-16 ***
## studentYes -6.468e-01 2.363e-01 -2.738 0.00619 **
## balance 5.737e-03 2.319e-04 24.738 < 2e-16 ***
## income 3.033e-06 8.203e-06 0.370 0.71152
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 2920.6 on 9999 degrees of freedom
## Residual deviance: 1571.5 on 9996 degrees of freedom
## AIC: 1579.5
##
## Number of Fisher Scoring iterations: 8