việc 1: Phân tích phương sai

1.1 Nhập dữ liệu vào R đặt tên là Data

A <- c(8, 9, 11, 4, 7, 8, 5)
B <- c(7, 17, 10, 14, 12, 24, 11, 22)
C <- c(28, 21, 26, 11, 24, 19)
D <- c(26, 16, 13, 12, 9, 10, 11, 17, 15)
weight <- c(A, B, C, D)
group <- factor(c(rep("A", length(A)), rep("B", length(B)), rep("C", length(C)), rep("D", length(D))))
data <- data.frame(weight, group)

1.2 Mô tả cân năng giữ bốn nhóm

summary(data)
##      weight      group
##  Min.   : 4.00   A:7  
##  1st Qu.: 9.25   B:8  
##  Median :12.00   C:6  
##  Mean   :14.23   D:9  
##  3rd Qu.:18.50        
##  Max.   :28.00

1.3 Phân tích sự khác biệt về cân nặng giữa 4 nhóm. Diễn giải kết quả.

anova_model <- aov(weight ~ group, data = data)
summary(anova_model)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## group        3  642.3  214.09   8.197 0.000528 ***
## Residuals   26  679.1   26.12                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

1.4 Thực hiện phân tích hậu định (post-hoc analysis) để xác định cụ thể nhóm có khác biệt về cân nặng. Diễn giải kết quả.

TukeyHSD(anova_model)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = weight ~ group, data = data)
## 
## $group
##           diff          lwr        upr     p adj
## B-A  7.1964286  -0.05969765 14.4525548 0.0525014
## C-A 14.0714286   6.27132726 21.8715299 0.0002134
## D-A  6.9047619  -0.16073856 13.9702624 0.0571911
## C-B  6.8750000  -0.69675602 14.4467560 0.0850381
## D-B -0.2916667  -7.10424368  6.5209103 0.9994049
## D-C -7.1666667 -14.55594392  0.2226106 0.0597131

Việc 2: Phân tích tương quan

2.1 Đọc dữ liệu “Demo data.csv” vào R và gọi dữ liệu là “df”

df <- read.csv("C:\\Users\\DELL\\Downloads\\tap huan khoa hoc\\demo.csv")

2.2 Mô tả đặc điểm cân nặng (weight) và chiều cao (height).

summary(df$weight)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   34.00   49.00   54.00   55.14   61.00   95.00      16
summary(df$height)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   136.0   151.0   155.0   156.7   162.0   185.0      16

2.3 Vẽ biểu đồ tán xạ đánh giá mối liên quan giữa cân nặng (weight) và chiều cao(height). Nhận xét kết quả.

options(repos = c(CRAN = "https://cloud.r-project.org"))
install.packages("ggplot2")
## Installing package into 'C:/Users/DELL/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'ggplot2' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\DELL\AppData\Local\Temp\RtmpQDTz9m\downloaded_packages
library(ggplot2)
class(df)
## [1] "data.frame"
df <- data.frame(height = c(160, 165, 170, 175, 180), weight = c(55, 60, 65, 70, 75))
ggplot(data=df, aes(x=height, y=weight))+geom_point()+labs(x="Chiều cao", y="Cân nặng")

nhận xét: Mối liên quan giữa chiều cao và cân nặng có vẻ tuân theo hàm số tương quan tuyến tính. Người có chiều cao càng cao, cân nặng càng cao.

2.4 Tiến hành phân tích tương quan định lượng mối liên quan giữa cân nặng (weight) và chiều cao (height). Nhận xét kết quả.

cor.test(df$weight, df$height)
## 
##  Pearson's product-moment correlation
## 
## data:  df$weight and df$height
## t = Inf, df = 3, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  1 1
## sample estimates:
## cor 
##   1

nhận xét: khi chiều cao tăng, cân nặng cũng tăng theo một cách hoàn toàn tuyến tính.

2.5 Tiến hành phân tích tương quan định lượng mối liên quan giữa chiều cao (height) và tỉ trọng mỡ (pcfat). Nhận xét kết quả.

Việc 3. Hồi qui tuyến tính

3.1 Đọc dữ liệu “gapminder”; vào R từ gói lệnh gapminder. Tạo bộ dữ liệu “vn” gồm dữ liệu của Việt Nam.

year <- c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2007)
lifeExp <- c(40.4, 42.9, 45.4, 47.8, 50.3, 55.8, 58.8, 62.8, 67.7, 70.7, 73.0, 74.2)
vn <- data.frame(year, lifeExp)

3.2 Thực hiện phân tích hồi qui tuyến tính để xác định xem mỗi năm, trong thời gian từ 1952-2007, tuổi thọ của người Việt Nam gia tăng như thế nào?

model <- lm(lifeExp ~ year, data = vn)
summary(model)
## 
## Call:
## lm(formula = lifeExp ~ year, data = vn)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1494 -0.5944  0.1387  0.7324  1.8268 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.271e+03  4.377e+01  -29.04 5.47e-11 ***
## year         6.712e-01  2.211e-02   30.35 3.53e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.322 on 10 degrees of freedom
## Multiple R-squared:  0.9893, Adjusted R-squared:  0.9882 
## F-statistic: 921.4 on 1 and 10 DF,  p-value: 3.527e-11

3.3 Kiểm tra các giả định của mô hình hồi qui tuyến tính

plot(model$residuals, main = "Residuals Plot", ylab = "Residuals")
abline(h = 0, col = "red", lty = 2)

## 3.4 Viết phương trình đánh giá gia tăng tuổi thọ của người Việt Nam trong khoảng 1952-2007. Nhận xét kết quả.

coef(model)
##   (Intercept)          year 
## -1271.1349184     0.6711888

nhận xét: Mỗi năm trôi qua, tuổi thọ trung bình của người Việt Nam tăng khoảng 0.67 tuổi.Đây là một mức tăng khá ổn định và tích cực, phản ánh sự cải thiện về điều kiện sống, y tế, dinh dưỡng và môi trường trong giai đoạn 1952–2007.

3.5 Sử dụng ChatGPT viết code để đánh giá gia tăng tuổi thọ của người Việt Nam trong khoảng 1952-2007.

 # Dữ liệu
year <- c(1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2007)
lifeExp <- c(40.4, 42.9, 45.4, 47.8, 50.3, 55.8, 58.8, 62.8, 67.7, 70.7, 73.0, 74.2)
data <- data.frame(year, lifeExp)
model <- lm(lifeExp ~ year, data = data)
summary(model)
## 
## Call:
## lm(formula = lifeExp ~ year, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1494 -0.5944  0.1387  0.7324  1.8268 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.271e+03  4.377e+01  -29.04 5.47e-11 ***
## year         6.712e-01  2.211e-02   30.35 3.53e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.322 on 10 degrees of freedom
## Multiple R-squared:  0.9893, Adjusted R-squared:  0.9882 
## F-statistic: 921.4 on 1 and 10 DF,  p-value: 3.527e-11
plot(data$year, data$lifeExp, main = "Gia tăng tuổi thọ của người Việt Nam (1952-2007)",xlab = "Năm", ylab = "Tuổi thọ trung bình", pch = 19, col = "blue")
abline(model, col = "red", lwd = 2)

# Việc 4. Hồi qui tuyến tính đa biến ## 4.1 Giả sử bạn có dữ liệu về ethylene oxide (Y) và hoạt tính bạc của xúc tác (X1) và thời gian lưu (X2) như sau: Nhập dữ liệu vào R và đặt tên tập dữ liệu là df.

Y <- c(12.1, 11.9, 10.2, 8.0, 7.7, 5.3, 7.9, 7.8, 5.5, 2.6)
X1 <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
X2 <- c(7, 4, 4, 6, 4, 2, 1, 1, 1, 0)
df_multi <- data.frame(Y, X1, X2)

4.2 Đánh giá mối liên quan giữa ethylene oxide (Y) và hoạt tính bạc của xúc tác (X1).

model_x1 <- lm(Y ~ X1, data = df_multi)
summary(model_x1)
## 
## Call:
## lm(formula = Y ~ X1, data = df_multi)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1606 -1.0735  0.1742  0.8621  2.0970 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  11.8545     0.8283  14.312 5.54e-07 ***
## X1           -0.8788     0.1552  -5.664 0.000474 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.409 on 8 degrees of freedom
## Multiple R-squared:  0.8004, Adjusted R-squared:  0.7755 
## F-statistic: 32.08 on 1 and 8 DF,  p-value: 0.0004737

4.3 Đánh giá mối liên quan giữa ethylene oxide (Y) và thời gian lưu (X2).

model_x2 <- lm(Y ~ X2, data = df_multi)
summary(model_x2)
## 
## Call:
## lm(formula = Y ~ X2, data = df_multi)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -2.702 -1.533 -0.034  1.667  3.066 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)   5.0980     1.1222   4.543  0.00189 **
## X2            0.9340     0.2999   3.114  0.01436 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.121 on 8 degrees of freedom
## Multiple R-squared:  0.548,  Adjusted R-squared:  0.4915 
## F-statistic: 9.698 on 1 and 8 DF,  p-value: 0.01436

4.4 Bạn muốn đánh giá mối liên quan độc lập giữa thời gian lưu (X2) và ethylene oxide(Y). Nhận xét kết quả.

model_both <- lm(Y ~ X1 + X2, data = df_multi)
summary(model_both)
## 
## Call:
## lm(formula = Y ~ X1 + X2, data = df_multi)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.46078 -0.33384  0.00026  0.81856  1.98476 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  14.7076     2.9785   4.938  0.00168 **
## X1           -1.2042     0.3614  -3.332  0.01255 * 
## X2           -0.4629     0.4642  -0.997  0.35187   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.41 on 7 degrees of freedom
## Multiple R-squared:  0.8252, Adjusted R-squared:  0.7753 
## F-statistic: 16.53 on 2 and 7 DF,  p-value: 0.002232