Tạo dữ liệu

data <- data.frame(
  Loai_BT = c("BTNC19", "BTNC19", "BTNC12.5", "BTNC12.5", "BTNC9.5", "BTNC9.5"),
  HL_XM = c(0, 2, 0, 2, 0, 3),
  HL_Nhua = c(0, 4.34, 0, 4.89, 0, 5.49),
  T10 = c(628.56, 752.47, 590.97, 634.22, 592.46, 662.06),
  T20 = c(538.91, 544.60, 454.10, 431.73, 525.75, 520.04),
  T30 = c(358.23, 415.46, 320.89, 370.47, 319.61, 386.20),
  T50 = c(252.89, 287.69, 210.02, 282.66, 213.07, 223.37),
  T60 = c(214.19, 221.57, 202.36, 222.72, 188.90, 206.75)
)

Kiểm tra bảng dữ liệu

print(data)
##    Loai_BT HL_XM HL_Nhua    T10    T20    T30    T50    T60
## 1   BTNC19     0    0.00 628.56 538.91 358.23 252.89 214.19
## 2   BTNC19     2    4.34 752.47 544.60 415.46 287.69 221.57
## 3 BTNC12.5     0    0.00 590.97 454.10 320.89 210.02 202.36
## 4 BTNC12.5     2    4.89 634.22 431.73 370.47 282.66 222.72
## 5  BTNC9.5     0    0.00 592.46 525.75 319.61 213.07 188.90
## 6  BTNC9.5     3    5.49 662.06 520.04 386.20 223.37 206.75

Vì mỗi cột nhiệt độ (10°C, 20°C, …) là một biến, ta cần gom lại thành hai cột:

library(tidyr)

data_long <- pivot_longer(
  data,
  cols = starts_with("T"),
  names_to = "Nhiet_do",
  values_to = "Cuong_do"
)

Loại bỏ ký tự T, đổi thành số

data_long$Nhiet_do <- as.numeric(gsub("T", "", data_long$Nhiet_do))

Xem dữ liệu sau khi chuyển

print(data_long)
## # A tibble: 30 × 5
##    Loai_BT HL_XM HL_Nhua Nhiet_do Cuong_do
##    <chr>   <dbl>   <dbl>    <dbl>    <dbl>
##  1 BTNC19      0    0          10     629.
##  2 BTNC19      0    0          20     539.
##  3 BTNC19      0    0          30     358.
##  4 BTNC19      0    0          50     253.
##  5 BTNC19      0    0          60     214.
##  6 BTNC19      2    4.34       10     752.
##  7 BTNC19      2    4.34       20     545.
##  8 BTNC19      2    4.34       30     415.
##  9 BTNC19      2    4.34       50     288.
## 10 BTNC19      2    4.34       60     222.
## # ℹ 20 more rows

Mô hình hồi quy tuyến tính đơn giản

model <- lm(Cuong_do ~ Nhiet_do, data = data_long)

Tóm tắt mô hình

summary(model)
## 
## Call:
## lm(formula = Cuong_do ~ Nhiet_do, data = data_long)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -106.69  -42.95   11.94   33.47  156.85 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  680.274     21.768   31.25  < 2e-16 ***
## Nhiet_do      -8.466      0.562  -15.06 5.88e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 57.1 on 28 degrees of freedom
## Multiple R-squared:  0.8901, Adjusted R-squared:  0.8862 
## F-statistic: 226.9 on 1 and 28 DF,  p-value: 5.879e-15

Phân tích riêng từng loại bê tông

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data_long %>%
  group_by(Loai_BT) %>%
  do(model = summary(lm(Cuong_do ~ Nhiet_do, data = .))) %>%
  summarise(
    Loai_BT,
    R2 = model$r.squared,
    P_value = model$coefficients[2, 4]
  )
## # A tibble: 3 × 3
##   Loai_BT     R2    P_value
##   <chr>    <dbl>      <dbl>
## 1 BTNC12.5 0.889 0.0000435 
## 2 BTNC19   0.913 0.0000166 
## 3 BTNC9.5  0.927 0.00000810

Vẽ biểu đồ hồi quy để kiểm tra trực quan

library(ggplot2)

ggplot(data_long, aes(x = Nhiet_do, y = Cuong_do, color = Loai_BT)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = TRUE) +
  theme_minimal() +
  labs(title = "Mối quan hệ giữa nhiệt độ và cường độ bê tông",
       x = "Nhiệt độ (°C)", y = "Cường độ (MPa hoặc tương tự)")
## `geom_smooth()` using formula = 'y ~ x'