#Việc 1. Đọc dữ liệu "Bone data.csv" vào R và gọi dữ liệu là “df”
dbone <- read.csv("E:\\HOPT\\PTDLR\\DATA\\Bone data.csv")
head(dbone)
## id sex age weight height prior.fx fnbmd smoking fx
## 1 1 Male 73 98 175 0 1.08 1 0
## 2 2 Female 68 72 166 0 0.97 0 0
## 3 3 Male 68 87 184 0 1.01 0 0
## 4 4 Female 62 72 173 0 0.84 1 0
## 5 5 Male 61 72 173 0 0.81 1 0
## 6 6 Female 76 57 156 0 0.74 0 0
#Việc 2. Đánh giá mối liên quan giữa cân nặng (weight) và mật độ xương tại cổ xương đùi (fnbmd)
#2.1 Vẽ biểu đồ đánh giá mối liên quan giữa cân nặng và mật độ xương tại cổ xương đùi. Bạn nhận xét gì về mối liên quan này.
library(lessR)
##
## lessR 4.4.2 feedback: gerbing@pdx.edu
## --------------------------------------------------------------
## > d <- Read("") Read data file, many formats available, e.g., Excel
## d is default data frame, data= in analysis routines optional
##
## Many examples of reading, writing, and manipulating data,
## graphics, testing means and proportions, regression, factor analysis,
## customization, forecasting, and aggregation from pivot tables
## Enter: browseVignettes("lessR")
##
## View lessR updates, now including time series forecasting
## Enter: news(package="lessR")
##
## Interactive data analysis
## Enter: interact()
##
## Attaching package: 'lessR'
## The following object is masked from 'package:base':
##
## sort_by
Plot(weight, fnbmd, fit = "loess", data = dbone)
##
##
## >>> Suggestions or enter: style(suggest=FALSE)
## Plot(weight, fnbmd, enhance=TRUE) # many options
## Plot(weight, fnbmd, fill="skyblue") # interior fill color of points
## Plot(weight, fnbmd, out_cut=.10) # label top 10% from center as outliers
##
## Loess Model MSE = 0.0156
##
#Đúng là trong gói lệnh lessR, biểu đồ tán xạ giữa hai biến liên tục được vẽ bằng hàm Plot. Dưới đây là mã R chính xác để vẽ biểu đồ tán xạ giữa weight và fnbmd cùng với đường hồi quy:
Plot(x = "weight", y = "fnbmd", data = dbone, fit = "lm")
##
##
## >>> Suggestions or enter: style(suggest=FALSE)
## Plot("weight", "fnbmd", data=dbone, fit="lm", size_cut=FALSE)
## Plot("weight", "fnbmd", data=dbone, fit="lm", trans=.8, bg="off", grid="off")
## SummaryStats("weight", "fnbmd") # or ss
##
## Joint and Marginal Frequencies
## ------------------------------
##
## "weight"
## "fnbmd" weight Sum
## fnbmd 1 1
## Sum 1 1
##
## Cramer's V: NaN
##
## Chi-square Test of Independence:
## Chisq = 0.000, df = 0, p-value = 1.000
## >>> Low cell expected frequencies, chi-squared approximation may not be accurate
##
## Some Parameter values (can be manually set)
## -------------------------------------------------------
## radius: 0.22 size of largest bubble
## power: 0.55 relative bubble t's going onsizes
#Câu nhận xét: “Mối liên quan giữa cân nặng và mật độ xương cho thấy một xu hướng tích cực, tức là khi cân nặng tăng, mật độ xương có xu hướng tăng theo, điều này có thể gợi ý rằng trọng lượng cơ thể có ảnh hưởng đến mật độ xương.”
#2.2 Thực hiện phân tính tương quan đánh giá mối liên quan giữa cân nặng và mật độ xương tại cổ xương đùi. Viết 1 câu diễn giải kết quả.
Correlation(weight, fnbmd, data = dbone)
## Correlation Analysis for Variables weight and fnbmd
##
##
## >>> Pearson's product-moment correlation
##
## Number of paired values with neither missing, n = 2121
## Number of cases (rows of data) deleted: 41
##
## Sample Covariance: s = 1.269
##
## Sample Correlation: r = 0.581
##
## Hypothesis Test of 0 Correlation: t = 32.882, df = 2119, p-value = 0.000
## 95% Confidence Interval for Correlation: 0.552 to 0.609
#Tính hệ số tương quan Pearson
cor.test(dbone$weight, dbone$fnbmd, method = "pearson")
##
## Pearson's product-moment correlation
##
## data: dbone$weight and dbone$fnbmd
## t = 32.882, df = 2119, p-value < 0.00000000000000022
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.5523536 0.6087528
## sample estimates:
## cor
## 0.5812509
#Bạn thực hiện phân tích tương quan bằng gói lệnh ‘lessR’ được không
# Phân tích tương quan Pearson bằng lessR
install.packages("lessR")
library(lessR)
dbone <- read.csv("E:\\HOPT\\PTDLR\\DATA\\Bone data.csv")
Correlation(c("weight", "fnbmd"), data = dbone)
##
## The correlation matrix contains 2 variables
##
## Missing data deletion: pairwise
##
## weight fnbmd
## weight 2161 2121
## fnbmd 2121 2122
##
## Correlation Matrix
##
## weight fnbmd
## weight 1.00 0.58
## fnbmd 0.58 1.00
#Hồi qui tuyến tính
#Việc 3. Đọc dữ liệu "Obesity data.csv" vào R
df <- read.csv("E:\\HOPT\\PTDLR\\DATA\\Obesity data.csv")
head(df)
## id gender height weight bmi age bmc bmd fat lean pcfat
## 1 1 F 150 49 21.8 53 1312 0.88 17802 28600 37.3
## 2 2 M 165 52 19.1 65 1309 0.84 8381 40229 16.8
## 3 3 F 157 57 23.1 64 1230 0.84 19221 36057 34.0
## 4 4 F 156 53 21.8 56 1171 0.80 17472 33094 33.8
## 5 5 M 160 51 19.9 54 1681 0.98 7336 40621 14.8
## 6 6 F 153 47 20.1 52 1358 0.91 14904 30068 32.2
#Việc 4. So sánh tỉ trọng mỡ (pcfat) giữa nam và nữ (gender)
#4.1 Đánh giá phân bố của tỉ trọng mỡ
Histogram(pcfat, fill = "blue", xlab = "Percentage of fat (%)", ylab = "Frequency", data = df)
## >>> Suggestions
## bin_width: set the width of each bin
## bin_start: set the start of the first bin
## bin_end: set the end of the last bin
## Histogram(pcfat, density=TRUE) # smoothed curve + histogram
## Plot(pcfat) # Violin/Box/Scatterplot (VBS) plot
##
## --- pcfat ---
##
## n miss mean sd min mdn max
## 1217 0 31.604786 7.182862 9.200000 32.400000 48.400000
##
##
##
## --- Outliers --- from the box plot: 10
##
## Small Large
## ----- -----
## 9.2
## 9.7
## 9.8
## 10.3
## 10.3
## 10.7
## 11.0
## 11.4
## 11.7
## 11.9
##
##
## Bin Width: 5
## Number of Bins: 9
##
## Bin Midpnt Count Prop Cumul.c Cumul.p
## -------------------------------------------------
## 5 > 10 7.5 3 0.00 3 0.00
## 10 > 15 12.5 26 0.02 29 0.02
## 15 > 20 17.5 61 0.05 90 0.07
## 20 > 25 22.5 128 0.11 218 0.18
## 25 > 30 27.5 244 0.20 462 0.38
## 30 > 35 32.5 338 0.28 800 0.66
## 35 > 40 37.5 294 0.24 1094 0.90
## 40 > 45 42.5 107 0.09 1201 0.99
## 45 > 50 47.5 16 0.01 1217 1.00
##
#4.2 Sử dụng kiểm định t để so sánh tỉ trọng mỡ giữa nam và nữ. Viết 1 câu diễn giải kết quả.
ttest(pcfat ~ gender, data = df)
##
## Compare pcfat across gender with levels F and M
## Grouping Variable: gender
## Response Variable: pcfat
##
##
## ------ Describe ------
##
## pcfat for gender F: n.miss = 0, n = 862, mean = 34.672, sd = 5.187
## pcfat for gender M: n.miss = 0, n = 355, mean = 24.156, sd = 5.764
##
## Mean Difference of pcfat: 10.516
##
## Weighted Average Standard Deviation: 5.362
##
##
## ------ Assumptions ------
##
## Note: These hypothesis tests can perform poorly, and the
## t-test is typically robust to violations of assumptions.
## Use as heuristic guides instead of interpreting literally.
##
## Null hypothesis, for each group, is a normal distribution of pcfat.
## Group F: Sample mean assumed normal because n > 30, so no test needed.
## Group M: Sample mean assumed normal because n > 30, so no test needed.
##
## Null hypothesis is equal variances of pcfat, homogeneous.
## Variance Ratio test: F = 33.223/26.909 = 1.235, df = 354;861, p-value = 0.016
## Levene's test, Brown-Forsythe: t = -2.232, df = 1215, p-value = 0.026
##
##
## ------ Infer ------
##
## --- Assume equal population variances of pcfat for each gender
##
## t-cutoff for 95% range of variation: tcut = 1.962
## Standard Error of Mean Difference: SE = 0.338
##
## Hypothesis Test of 0 Mean Diff: t-value = 31.101, df = 1215, p-value = 0.000
##
## Margin of Error for 95% Confidence Level: 0.663
## 95% Confidence Interval for Mean Difference: 9.853 to 11.180
##
##
## --- Do not assume equal population variances of pcfat for each gender
##
## t-cutoff: tcut = 1.964
## Standard Error of Mean Difference: SE = 0.353
##
## Hypothesis Test of 0 Mean Diff: t = 29.768, df = 602.015, p-value = 0.000
##
## Margin of Error for 95% Confidence Level: 0.694
## 95% Confidence Interval for Mean Difference: 9.823 to 11.210
##
##
## ------ Effect Size ------
##
## --- Assume equal population variances of pcfat for each gender
##
## Standardized Mean Difference of pcfat, Cohen's d: 1.961
##
##
## ------ Practical Importance ------
##
## Minimum Mean Difference of practical importance: mmd
## Minimum Standardized Mean Difference of practical importance: msmd
## Neither value specified, so no analysis
##
##
## ------ Graphics Smoothing Parameter ------
##
## Density bandwidth for gender F: 1.475
## Density bandwidth for gender M: 1.867
#4.3 Xây dựng mô hình hổi qui tuyến tính để so sánh tỉ trọng mỡ giữa nam và nữ
# Kiểm tra giả định của mô hình
#- Viết phương trình mô tả liên quan giữa giới tính và tỉ trọng mỡ
#pcfat = a + b*gender
#- Diễn giải kết quả
m.1 = lm(pcfat ~ gender, data = df)
summary(m.1)
##
## Call:
## lm(formula = pcfat ~ gender, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -20.0724 -3.2724 0.1484 3.6276 14.8439
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 34.6724 0.1826 189.9 <0.0000000000000002 ***
## genderM -10.5163 0.3381 -31.1 <0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.362 on 1215 degrees of freedom
## Multiple R-squared: 0.4432, Adjusted R-squared: 0.4428
## F-statistic: 967.3 on 1 and 1215 DF, p-value: < 0.00000000000000022
#Dùng gói lessR
m.2 = reg(pcfat ~ gender, data = df)
##
## >>> gender is not numeric. Converted to indicator variables.
#Kiểm tra giả định
par(mfrow = c(2, 2))
plot(m.1)
#Dùng gói ggfortify
library(ggplot2)
library(ggfortify)
autoplot(m.1)
#Việc 5. Đánh giá mối liên quan giữa cân nặng (weight) và tỉ trọng mỡ (pcfat)
#5.1 Vẽ biểu đồ đánh giá mối liên quan giữa cân nặng và tỉ trọng mỡ
#Vẽ biểu đồ tán xạ
Plot(weight, pcfat, fit = "lm", data = df)
##
##
## >>> Suggestions or enter: style(suggest=FALSE)
## Plot(weight, pcfat, enhance=TRUE) # many options
## Plot(weight, pcfat, fill="skyblue") # interior fill color of points
## Plot(weight, pcfat, MD_cut=6) # Mahalanobis distance from center > 6 is an outlier
##
##
## >>> Pearson's product-moment correlation
##
## Number of paired values with neither missing, n = 1217
## Sample Correlation of weight and pcfat: r = 0.057
##
## Hypothesis Test of 0 Correlation: t = 1.975, df = 1215, p-value = 0.049
## 95% Confidence Interval for Correlation: 0.000 to 0.112
##
##
## Line: b0 = 29.222947 b1 = 0.043193 Linear Model MSE = 51.470803 Rsq = 0.003
##
#5.2 Xây dựng mô hình để đánh giá mối liên quan giữa cân nặng và tỉ trọng mỡ
#- Kiểm tra giả định của mô hình
#- Viết phương trình pcfat = a + b*weight
#- Diễn giải kết quả
#Sử dụng mô hình hồi qui tuyến tính
m.3 = lm(pcfat ~ weight, data = df)
summary(m.3)
##
## Call:
## lm(formula = pcfat ~ weight, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -22.3122 -4.5234 0.8902 5.2695 16.9742
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 29.22295 1.22370 23.881 <0.0000000000000002 ***
## weight 0.04319 0.02188 1.975 0.0485 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.174 on 1215 degrees of freedom
## Multiple R-squared: 0.003199, Adjusted R-squared: 0.002378
## F-statistic: 3.899 on 1 and 1215 DF, p-value: 0.04855
autoplot(m.3)
#Việc 6. Đánh giá mối liên quan độc lập giữa cân nặng (weight) và tỉ trọng mỡ (pcfat)
#6.1 Qua y văn bạn xác định các yếu tố có thể gây nhiễu (confounder) mối liên quan giữa cân nặng và tỉ trong mỡ là giới tính, tuổi, và chiều cao. Hãy xây dựng mô hình đa biến đánh giá mối liên quan độc lập giữa cân nặng và tỉ trọng mỡ sau khi hiệu chỉnh cho các yếu tố gây nhiễu.
m.4 = lm(pcfat ~ weight + age + gender + height, data = df)
summary(m.4)
##
## Call:
## lm(formula = pcfat ~ weight + age + gender + height, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -18.208 -2.543 0.019 2.582 15.706
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 48.368722 3.505431 13.798 < 0.0000000000000002 ***
## weight 0.439169 0.015594 28.163 < 0.0000000000000002 ***
## age 0.056166 0.007404 7.585 0.0000000000000658 ***
## genderM -11.483254 0.344343 -33.348 < 0.0000000000000002 ***
## height -0.257013 0.023768 -10.813 < 0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.974 on 1212 degrees of freedom
## Multiple R-squared: 0.695, Adjusted R-squared: 0.694
## F-statistic: 690.4 on 4 and 1212 DF, p-value: < 0.00000000000000022
autoplot(m.4)
#6.2 Viết phương trình
pcfat <- 48.4 + 0.44 * df$weight + 0.06 * df$age - 11.5 * df$genderM - 0.26 * df$height
head(df$pcfat)
## [1] 37.3 16.8 34.0 33.8 14.8 32.2
# Mô hình hồi quy tuyến tính đa biến
model <- lm(pcfat ~ weight + age + height + gender, data = df)
# Xem kết quả mô hình
summary(model)
##
## Call:
## lm(formula = pcfat ~ weight + age + height + gender, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -18.208 -2.543 0.019 2.582 15.706
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 48.368722 3.505431 13.798 < 0.0000000000000002 ***
## weight 0.439169 0.015594 28.163 < 0.0000000000000002 ***
## age 0.056166 0.007404 7.585 0.0000000000000658 ***
## height -0.257013 0.023768 -10.813 < 0.0000000000000002 ***
## genderM -11.483254 0.344343 -33.348 < 0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.974 on 1212 degrees of freedom
## Multiple R-squared: 0.695, Adjusted R-squared: 0.694
## F-statistic: 690.4 on 4 and 1212 DF, p-value: < 0.00000000000000022
#Việc 7. Xây dựng mô hình dự báo tỉ trọng mỡ (pcfat)
#7.1 Xây dựng mô hình tối ưu dự báo tỉ trọng mỡ bằng phương pháp BMA (Bayesian Modelling Average).
library(BMA)
## Loading required package: survival
## Loading required package: leaps
## Loading required package: robustbase
##
## Attaching package: 'robustbase'
## The following object is masked from 'package:survival':
##
## heart
## Loading required package: inline
## Loading required package: rrcov
## Scalable Robust Estimators with High Breakdown Point (version 1.7-7)
yvar = df[, c("pcfat")]
xvar = df[, c("gender", "age", "height", "weight", "bmi")]
m.bma = bicreg(xvar, yvar, strict = FALSE, OR = 20)
summary(m.bma)
##
## Call:
## bicreg(x = xvar, y = yvar, strict = FALSE, OR = 20)
##
##
## 3 models were selected
## Best 3 models (cumulative posterior probability = 1 ):
##
## p!=0 EV SD model 1 model 2 model 3
## Intercept 100.0 5.26146 4.582901 7.95773 -0.79279 8.13735
## genderM 100.0 -11.25139 0.429659 -11.44430 -11.42764 -10.80625
## age 100.0 0.05259 0.008048 0.05497 0.05473 0.04715
## height 31.4 0.01759 0.028494 . 0.05598 .
## weight 39.2 0.03102 0.042611 0.07921 . .
## bmi 100.0 1.01265 0.111625 0.89419 1.08852 1.08936
##
## nVar 4 4 3
## r2 0.697 0.696 0.695
## BIC -1423.06312 -1422.62198 -1422.49027
## post prob 0.392 0.314 0.294
imageplot.bma(m.bma)
#Xây dựng mô hình dự báo tỉ trọng mỡ (pcfat) bằng phương pháp Bayesian Modelling Average (gói lệnh ‘BMA’)
# Chọn các biến dự báo tiềm năng
X <- df[, c("weight", "age", "height", "gender")] # ma trận các biến giải thích
Y <- df$pcfat # biến kết quả
# Xây dựng mô hình BMA
bma_model <- bicreg(x = X, y = Y, strict = FALSE)
# Xem kết quả
summary(bma_model)
##
## Call:
## bicreg(x = X, y = Y, strict = FALSE)
##
##
## 1 models were selected
## Best 1 models (cumulative posterior probability = 1 ):
##
## p!=0 EV SD model 1
## Intercept 100 48.36872 3.505431 48.36872
## weight 100 0.43917 0.015594 0.43917
## age 100 0.05617 0.007404 0.05617
## height 100 -0.25701 0.023768 -0.25701
## genderM 100 -11.48325 0.344343 -11.48325
##
## nVar 4
## r2 0.695
## BIC -1416.58247
## post prob 1
#or
model_bma <- bic.glm(pcfat ~ weight + gender + age + height + bmi, data = df, glm.family = gaussian())
summary(model_bma)
##
## Call:
## bic.glm.formula(f = pcfat ~ weight + gender + age + height + bmi, data = df, glm.family = gaussian())
##
##
## 3 models were selected
## Best 3 models (cumulative posterior probability = 1 ):
##
## p!=0 EV SD model 1 model 2 model 3
## Intercept 100 5.29004 4.569142 7.95773 -0.79279 8.13735
## weight 39.1 0.03101 0.042609 0.07921 . .
## genderM 100.0 -11.24928 0.430060 -11.44430 -11.42764 -10.80625
## age 100.0 0.05257 0.008049 0.05497 0.05473 0.04715
## height 31.1 0.01741 0.028405 . 0.05598 .
## bmi 100.0 1.01269 0.111616 0.89419 1.08852 1.08936
##
## nVar 4 4 3
## BIC -7399.21047 -7398.74990 -7398.66161
## post prob 0.391 0.311 0.298
##
## 1 observations deleted due to missingness.
#7.2 Kiểm tra giả định của mô hình.
m.bma = lm(pcfat ~ gender + age + weight + bmi, data = df)
summary(m.bma)
##
## Call:
## lm(formula = pcfat ~ gender + age + weight + bmi, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -18.225 -2.557 0.033 2.608 15.646
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.957732 0.852500 9.335 < 0.0000000000000002 ***
## genderM -11.444303 0.342565 -33.408 < 0.0000000000000002 ***
## age 0.054966 0.007395 7.433 0.000000000000199 ***
## weight 0.079207 0.028620 2.768 0.00573 **
## bmi 0.894194 0.080297 11.136 < 0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.963 on 1212 degrees of freedom
## Multiple R-squared: 0.6966, Adjusted R-squared: 0.6956
## F-statistic: 695.7 on 4 and 1212 DF, p-value: < 0.00000000000000022
#install.packages("car")
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following objects are masked from 'package:lessR':
##
## bc, recode, sp
vif(m.bma)
## gender age weight bmi
## 1.878778 1.263468 5.609651 4.663425
autoplot(m.bma)
#7.3 Viết phương trình dự báo tối ưu. Viết 1 câu diễn giải kết quả.
# convert gender tu string sang numeric
df$genderM <- ifelse(df$gender == "Male", 1, 0)
pcfat = 8.0 - 11.4 * df$genderM + 0.05 * df$age + 0.08 * df$weight + 0.89 * df$bmi
head(df$pcfat)
## [1] 37.3 16.8 34.0 33.8 14.8 32.2
#7.4 Xác định tầm quan trọng của các biến số dự báo tỉ trọng mỡ. Viết 1 câu diễn giải kết quả.
library(relaimpo)
## Loading required package: MASS
## Loading required package: boot
##
## Attaching package: 'boot'
## The following object is masked from 'package:car':
##
## logit
## The following object is masked from 'package:robustbase':
##
## salinity
## The following object is masked from 'package:survival':
##
## aml
## Loading required package: survey
## Loading required package: grid
## Loading required package: Matrix
##
## Attaching package: 'survey'
## The following object is masked from 'package:graphics':
##
## dotchart
## Loading required package: mitools
## This is the global version of package relaimpo.
## If you are a non-US user, a version with the interesting additional metric pmvd is available
## from Ulrike Groempings web site at prof.beuth-hochschule.de/groemping.
df$sex = ifelse(df$gender == "F", 1, 0)
m.bma2 = lm(pcfat ~ sex + age + weight + bmi, data = df)
calc.relimp(m.bma2, type = "lmg", rela = TRUE, rank = TRUE)
## Response variable: pcfat
## Total response variance: 51.5935
## Analysis based on 1217 observations
##
## 4 Regressors:
## sex age weight bmi
## Proportion of variance explained by model: 69.66%
## Metrics are normalized to sum to 100% (rela=TRUE).
##
## Relative importance metrics:
##
## lmg
## sex 0.59317775
## age 0.06893066
## weight 0.09175463
## bmi 0.24613695
##
## Average coefficients for different model sizes:
##
## 1X 2Xs 3Xs 4Xs
## sex 10.51634414 11.71834412 11.80453842 11.44430262
## age 0.12768705 0.10445197 0.05168496 0.05496623
## weight 0.04319324 -0.05539405 -0.06907993 0.07920690
## bmi 1.03619023 1.50631405 1.54278433 0.89419395
# gói relaimpo là một công cụ phổ biến để đánh giá tầm quan trọng tương đối của các biến trong mô hình hồi quy. Gói relaimpo sử dụng phương pháp relative importance để tính toán mức độ đóng góp của mỗi biến vào mô hình hồi quy
# Lấy tên các biến định lượng dùng cho mô hình
# Lấy tên các biến định lượng dùng cho mô hình
numeric_vars <- c("pcfat", "weight", "age")
# Giữ các dòng có giá trị finite ở các biến đó
df_clean <- df[apply(df[, numeric_vars], 1, function(x) all(is.finite(x))), ]
# Bảo đảm gender là factor
df_clean$gender <- factor(df_clean$gender)
# Hồi quy tuyến tính
model_lm <- lm(pcfat ~ weight + age + gender, data = df_clean)
# Phân tích tầm quan trọng
library(relaimpo)
relaimpo_result <- calc.relimp(model_lm, type = "lmg", rela = TRUE)
# In kết quả
print(relaimpo_result)
## Response variable: pcfat
## Total response variance: 51.5935
## Analysis based on 1217 observations
##
## 3 Regressors:
## weight age gender
## Proportion of variance explained by model: 66.55%
## Metrics are normalized to sum to 100% (rela=TRUE).
##
## Relative importance metrics:
##
## lmg
## weight 0.1329652
## age 0.1076167
## gender 0.7594182
##
## Average coefficients for different model sizes:
##
## 1X 2Xs 3Xs
## weight 0.04319324 0.2081606 0.3591105
## age 0.12768705 0.1114258 0.0913430
## gender -10.51634414 -12.0443618 -13.5588989
# Kết luận