PHẦN I. PHÂN TÍCH HỒI QUY

1. Xây dựng mô hình BBD 15 thí nghiệm

# mô hình BBD 15 thí nghiệm
library (rsm)
## Warning: package 'rsm' was built under R version 4.3.2
bbd(~ nong_do + thoi_gian + Ti_le_dm_dl, n0=3,randomize = F,
    coding = list(nong_do~(nong_do_etoh-70)/20, thoi_gian~(thoi_gian_chiet-60)/30,
    Ti_le_dm_dl~(Ti_le-6)/2))
##    run.order std.order nong_do_etoh thoi_gian_chiet Ti_le
## 1          1         1           50              30     6
## 2          2         2           90              30     6
## 3          3         3           50              90     6
## 4          4         4           90              90     6
## 5          5         5           50              60     4
## 6          6         6           90              60     4
## 7          7         7           50              60     8
## 8          8         8           90              60     8
## 9          9         9           70              30     4
## 10        10        10           70              90     4
## 11        11        11           70              30     8
## 12        12        12           70              90     8
## 13        13        13           70              60     6
## 14        14        14           70              60     6
## 15        15        15           70              60     6
## 
## Data are stored in coded form using these coding formulas ...
## nong_do ~ (nong_do_etoh - 70)/20
## thoi_gian ~ (thoi_gian_chiet - 60)/30
## Ti_le_dm_dl ~ (Ti_le - 6)/2

2. Phân tich dữ liệu hồi quy

# Phân tích dữ liệu biến phụ thuộc hàm lượng irisfloretin (5)
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.2
## Warning: package 'purrr' was built under R version 4.3.2
## Warning: package 'dplyr' was built under R version 4.3.2
## Warning: package 'stringr' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
df = read.csv("E:/1-TUANANHPHARM/UPDATE/in HUP/ĐÀO TẠO sinh viên/dữ liệu KLTN/2023, Thủy Tiên/OE_Iris2.csv", header=TRUE)
# Đọc dữ liệu
summary(df)
##      nongdo      thoigian       tile      hl_iris           hl_cao     
##  Min.   :50   Min.   :30   Min.   :4   Min.   :0.5210   Min.   :16.74  
##  1st Qu.:60   1st Qu.:45   1st Qu.:5   1st Qu.:0.5400   1st Qu.:18.15  
##  Median :70   Median :60   Median :6   Median :0.5670   Median :19.32  
##  Mean   :70   Mean   :60   Mean   :6   Mean   :0.5583   Mean   :18.93  
##  3rd Qu.:80   3rd Qu.:75   3rd Qu.:7   3rd Qu.:0.5740   3rd Qu.:19.70  
##  Max.   :90   Max.   :90   Max.   :8   Max.   :0.5880   Max.   :20.66
str(df)
## 'data.frame':    15 obs. of  5 variables:
##  $ nongdo  : int  50 90 50 90 50 90 50 90 70 70 ...
##  $ thoigian: int  30 30 90 90 60 60 60 60 30 90 ...
##  $ tile    : int  6 6 6 6 4 4 8 8 4 4 ...
##  $ hl_iris : num  0.524 0.579 0.537 0.582 0.543 0.588 0.521 0.572 0.571 0.576 ...
##  $ hl_cao  : num  18.2 18.6 18.7 19.3 16.7 ...
# Mã hóa x1, x2, x3
mahoa = coded.data(df,x1~(nongdo-70)/20,x2~(thoigian-60)/30,x3~(tile-6)/2)
hq_iris <- rsm(hl_iris ~ SO(x1,x2,x3), data = mahoa)
summary(hq_iris)
## 
## Call:
## rsm(formula = hl_iris ~ SO(x1, x2, x3), data = mahoa)
## 
##               Estimate Std. Error  t value  Pr(>|t|)    
## (Intercept)  0.5673333  0.0027679 204.9710 5.245e-11 ***
## x1           0.0245000  0.0016950  14.4545 2.859e-05 ***
## x2           0.0046250  0.0016950   2.7287 0.0413459 *  
## x3          -0.0131250  0.0016950  -7.7435 0.0005741 ***
## x1:x2       -0.0025000  0.0023970  -1.0430 0.3447502    
## x1:x3        0.0015000  0.0023970   0.6258 0.5589170    
## x2:x3        0.0027500  0.0023970   1.1472 0.3031949    
## x1^2        -0.0062917  0.0024949  -2.5218 0.0530589 .  
## x2^2        -0.0055417  0.0024949  -2.2212 0.0770144 .  
## x3^2        -0.0050417  0.0024949  -2.0208 0.0992729 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Multiple R-squared:  0.9832, Adjusted R-squared:  0.9529 
## F-statistic:  32.5 on 9 and 5 DF,  p-value: 0.0006539
## 
## Analysis of Variance Table
## 
## Response: hl_iris
##                 Df    Sum Sq    Mean Sq F value    Pr(>F)
## FO(x1, x2, x3)   3 0.0063512 0.00211708 92.1139 8.523e-05
## TWI(x1, x2, x3)  3 0.0000643 0.00002142  0.9318   0.49033
## PQ(x1, x2, x3)   3 0.0003069 0.00010231  4.4513   0.07077
## Residuals        5 0.0001149 0.00002298                  
## Lack of fit      3 0.0001102 0.00003675 15.7500   0.06029
## Pure error       2 0.0000047 0.00000233                  
## 
## Stationary point of response surface:
##         x1         x2         x3 
##  1.8711384 -0.2774444 -1.0989684 
## 
## Stationary point in original units:
##     nongdo   thoigian       tile 
## 107.422768  51.676667   3.802063 
## 
## Eigenanalysis:
## eigen() decomposition
## $values
## [1] -0.003864193 -0.005061060 -0.007949747
## 
## $vectors
##          [,1]       [,2]       [,3]
## x1 -0.1314990  0.7413059  0.6581593
## x2  0.6851999 -0.4118235  0.6007516
## x3  0.7163861  0.5299689 -0.4537884
hq_cao <- rsm(hl_cao ~ SO(x1,x2,x3), data = mahoa)
summary(hq_cao)
## 
## Call:
## rsm(formula = hl_cao ~ SO(x1, x2, x3), data = mahoa)
## 
##              Estimate Std. Error  t value  Pr(>|t|)    
## (Intercept) 19.680333   0.062979 312.4895 6.369e-12 ***
## x1           0.228750   0.038567   5.9313 0.0019437 ** 
## x2           0.220000   0.038567   5.7044 0.0023117 ** 
## x3           1.366250   0.038567  35.4256 3.373e-07 ***
## x1:x2        0.086500   0.054542   1.5859 0.1736097    
## x1:x3        0.029500   0.054542   0.5409 0.6118118    
## x2:x3       -0.047000   0.054542  -0.8617 0.4282204    
## x1^2        -0.948167   0.056769 -16.7023 1.406e-05 ***
## x2^2        -0.040167   0.056769  -0.7075 0.5108314    
## x3^2        -0.420667   0.056769  -7.4102 0.0007046 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Multiple R-squared:  0.997,  Adjusted R-squared:  0.9915 
## F-statistic: 182.7 on 9 and 5 DF,  p-value: 9.358e-06
## 
## Analysis of Variance Table
## 
## Response: hl_cao
##                 Df  Sum Sq Mean Sq  F value    Pr(>F)
## FO(x1, x2, x3)   3 15.7389  5.2463 440.8984 1.771e-06
## TWI(x1, x2, x3)  3  0.0422  0.0141   1.1834    0.4043
## PQ(x1, x2, x3)   3  3.7810  1.2603 105.9172 6.052e-05
## Residuals        5  0.0595  0.0119                   
## Lack of fit      3  0.0559  0.0186  10.3858    0.0891
## Pure error       2  0.0036  0.0018                   
## 
## Stationary point of response surface:
##        x1        x2        x3 
## 0.2405025 2.1115443 1.5143846 
## 
## Stationary point in original units:
##     nongdo   thoigian       tile 
##  74.810050 123.346329   9.028769 
## 
## Eigenanalysis:
## eigen() decomposition
## $values
## [1] -0.03675935 -0.42154412 -0.95069653
## 
## $vectors
##           [,1]       [,2]        [,3]
## x1 -0.04636056 0.03269182  0.99838967
## x2 -0.99716558 0.05777542 -0.04819555
## x3  0.05925798 0.99779419 -0.02992066

PHẦN II. TÍNH GIÁ TRỊ TỐI ƯU

Giá trị tối ưu Iris (%)

# Tạo mô hình hồi quy hàm lượng iris (%)

library(rsm)
model1 <- rsm(hl_iris ~ FO(nongdo, thoigian, tile) + TWI(nongdo, thoigian, tile) + SO(nongdo, thoigian, tile), data=df)
summary(model1)
## Near-stationary-ridge situation detected -- stationary point altered
##  Change 'threshold' if this is not what you intend
## 
## Call:
## rsm(formula = hl_iris ~ FO(nongdo, thoigian, tile) + TWI(nongdo, 
##     thoigian, tile) + SO(nongdo, thoigian, tile), data = df)
## 
##                    Estimate  Std. Error t value  Pr(>|t|)    
## (Intercept)      3.8184e-01  5.2283e-02  7.3034 0.0007536 ***
## nongdo           3.4521e-03  9.7798e-04  3.5298 0.0167431 *  
## thoigian         9.0972e-04  4.9952e-04  1.8212 0.1282110    
## tile             3.1875e-03  8.9489e-03  0.3562 0.7362351    
## nongdo:thoigian -4.1667e-06  3.9951e-06 -1.0430 0.3447502    
## nongdo:tile      3.7500e-05  5.9926e-05  0.6258 0.5589170    
## thoigian:tile    4.5833e-05  3.9951e-05  1.1472 0.3031949    
## nongdo^2        -1.5729e-05  6.2373e-06 -2.5218 0.0530589 .  
## thoigian^2      -6.1574e-06  2.7721e-06 -2.2212 0.0770144 .  
## tile^2          -1.2604e-03  6.2373e-04 -2.0208 0.0992729 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Multiple R-squared:  0.9832, Adjusted R-squared:  0.9529 
## F-statistic:  32.5 on 9 and 5 DF,  p-value: 0.0006539
## 
## Analysis of Variance Table
## 
## Response: hl_iris
##                             Df    Sum Sq    Mean Sq F value    Pr(>F)
## FO(nongdo, thoigian, tile)   3 0.0063513 0.00211708 92.1139 8.523e-05
## TWI(nongdo, thoigian, tile)  3 0.0000643 0.00002142  0.9318   0.49033
## PQ(nongdo, thoigian, tile)   3 0.0003069 0.00010231  4.4513   0.07077
## Residuals                    5 0.0001149 0.00002298                  
## Lack of fit                  3 0.0001102 0.00003675 15.7500   0.06029
## Pure error                   2 0.0000047 0.00000233                  
## 
## Stationary point of response surface:
##      nongdo    thoigian        tile 
## -0.01864375 -0.02259811  1.23582141 
## 
## Eigenanalysis:
## eigen() decomposition
## $values
## [1]  0.000000000  0.000000000 -0.001261119
## 
## $vectors
##                 [,1]       [,2]        [,3]
## nongdo   -0.17128173 0.98510665 -0.01508188
## thoigian  0.98510126 0.17100092 -0.01828077
## tile      0.01542949 0.01798834  0.99971914
# TÍNH TOÁN TỐI ƯU

optim_function <- function(params) {
  nongdo <- params[1]
  thoigian <- params[2]
  tile <- params[3]
  
  # Tính toán giá trị dự đoán của hl_iris dựa trên mô hình hồi quy
  hl_iris_pred <- predict(model1, newdata = data.frame(nongdo = nongdo, thoigian = thoigian, tile = tile))
  
  # Do optim() tìm giá trị tối thiểu, trả về giá trị âm của hl_iris_pred để tối đa hóa nó
  return(-hl_iris_pred)
}

  # Giả sử giá trị khởi tạo
initial_values <- c(mean(df$nongdo), mean(df$thoigian), mean(df$tile))

  # Thực hiện tối ưu hóa
optim_result <- optim(initial_values, optim_function, method = "L-BFGS-B", 
                      lower = c(min(df$nongdo), min(df$thoigian), min(df$tile)), 
                      upper = c(max(df$nongdo), max(df$thoigian), max(df$tile)))

  # Kết quả tối ưu
optimal_values <- optim_result$par
  # Tính giá trị hl_iris tối ưu
optimal_hl_iris <- predict(model1, newdata = data.frame(nongdo = optimal_values[1],
                                                        thoigian = optimal_values[2],
                                                        tile = optimal_values[3]))

  # In giá trị tối ưu và giá trị hl_iris tương ứng
print(paste("Giá trị tối ưu: nongdo =", optimal_values[1],
            "thoigian =", optimal_values[2], 
            "tile =", optimal_values[3]))
## [1] "Giá trị tối ưu: nongdo = 90 thoigian = 58.3082706766756 tile = 4"
print(paste("Giá trị hl_iris tối ưu:", optimal_hl_iris))
## [1] "Giá trị hl_iris tối ưu: 0.592142622180451"

Giá trị tối ưu hàm lượng cao (%)

# Tạo mô hình hồi quy hàm lượng cao (%)
library(rsm)
model2 <- rsm(hl_cao ~ FO(nongdo, thoigian, tile) + TWI(nongdo, thoigian, tile) + SO(nongdo, thoigian, tile), data=df)

# Phân tích phương sai
summary(model2)
## Near-stationary-ridge situation detected -- stationary point altered
##  Change 'threshold' if this is not what you intend
## 
## Call:
## rsm(formula = hl_cao ~ FO(nongdo, thoigian, tile) + TWI(nongdo, 
##     thoigian, tile) + SO(nongdo, thoigian, tile), data = df)
## 
##                    Estimate  Std. Error  t value  Pr(>|t|)    
## (Intercept)     -5.8750e-01  1.1896e+00  -0.4939 0.6423372    
## nongdo           3.3022e-01  2.2253e-02  14.8397 2.514e-05 ***
## thoigian         7.2972e-03  1.1366e-02   0.6420 0.5491273    
## tile             1.9405e+00  2.0362e-01   9.5300 0.0002152 ***
## nongdo:thoigian  1.4417e-04  9.0903e-05   1.5859 0.1736097    
## nongdo:tile      7.3750e-04  1.3635e-03   0.5409 0.6118118    
## thoigian:tile   -7.8333e-04  9.0903e-04  -0.8617 0.4282204    
## nongdo^2        -2.3704e-03  1.4192e-04 -16.7023 1.406e-05 ***
## thoigian^2      -4.4630e-05  6.3076e-05  -0.7075 0.5108314    
## tile^2          -1.0517e-01  1.4192e-02  -7.4102 0.0007046 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Multiple R-squared:  0.997,  Adjusted R-squared:  0.9915 
## F-statistic: 182.7 on 9 and 5 DF,  p-value: 9.358e-06
## 
## Analysis of Variance Table
## 
## Response: hl_cao
##                             Df  Sum Sq Mean Sq  F value    Pr(>F)
## FO(nongdo, thoigian, tile)   3 15.7389  5.2463 440.8984 1.771e-06
## TWI(nongdo, thoigian, tile)  3  0.0422  0.0141   1.1834    0.4043
## PQ(nongdo, thoigian, tile)   3  3.7810  1.2603 105.9172 6.052e-05
## Residuals                    5  0.0595  0.0119                   
## Lack of fit                  3  0.0559  0.0186  10.3858    0.0891
## Pure error                   2  0.0036  0.0018                   
## 
## Stationary point of response surface:
##    nongdo  thoigian      tile 
## 70.999301 -2.122083  9.482860 
## 
## Eigenanalysis:
## eigen() decomposition
## $values
## [1]  0.00000000 -0.00237124 -0.10516945
## 
## $vectors
##                  [,1]         [,2]         [,3]
## nongdo   -0.030357981  0.999532644  0.003589662
## thoigian -0.999532544 -0.030344589 -0.003728141
## tile      0.003617472  0.003701163 -0.999986608
# TÍNH TOÁN TỐI ƯU

optim_function <- function(params) {
  nongdo <- params[1]
  thoigian <- params[2]
  tile <- params[3]
  
  # Tính toán giá trị dự đoán của hl_cao dựa trên mô hình hồi quy
  hl_cao_pred <- predict(model2, newdata = data.frame(nongdo = nongdo, thoigian = thoigian, tile = tile))
  
  # Do optim() tìm giá trị tối thiểu, trả về giá trị âm của hl_cao_pred để tối đa hóa nó
  return(-hl_cao_pred)
  }

# Giả sử giá trị khởi tạo
initial_values <- c(mean(df$nongdo), mean(df$thoigian), mean(df$tile))

# Thực hiện tối ưu hóa
optim_result <- optim(initial_values, optim_function, method = "L-BFGS-B", 
                      lower = c(min(df$nongdo), min(df$thoigian), min(df$tile)), 
                      upper = c(max(df$nongdo), max(df$thoigian), max(df$tile)))

# Kết quả tối ưu
optimal_values <- optim_result$par
# Tính giá trị hl_cao tối ưu
optimal_hl_cao <- predict(model2, newdata = data.frame(nongdo = optimal_values[1],
                                                       thoigian = optimal_values[2],
                                                       tile = optimal_values[3]))

# In giá trị tối ưu và giá trị hl_cao tương ứng
print(paste("Giá trị tối ưu: nongdo =", optimal_values[1],
            "thoigian =", optimal_values[2], 
            "tile =", optimal_values[3]))
## [1] "Giá trị tối ưu: nongdo = 73.6359641406465 thoigian = 90 tile = 8"
print(paste("Giá trị hl_cao tối ưu:", optimal_hl_cao))
## [1] "Giá trị hl_cao tối ưu: 20.790087465943"

PHẦN III. VẼ BIỂU ĐỒ

Vẽ biểu đồ iris

library(dplyr) 
library(ggplot2)
model1 <- rsm(hl_iris ~ FO(thoigian, tile, nongdo) + TWI(thoigian, tile, nongdo) + SO(thoigian, tile, nongdo), data=df)
summary(model1)
## Near-stationary-ridge situation detected -- stationary point altered
##  Change 'threshold' if this is not what you intend
## 
## Call:
## rsm(formula = hl_iris ~ FO(thoigian, tile, nongdo) + TWI(thoigian, 
##     tile, nongdo) + SO(thoigian, tile, nongdo), data = df)
## 
##                    Estimate  Std. Error t value  Pr(>|t|)    
## (Intercept)      3.8184e-01  5.2283e-02  7.3034 0.0007536 ***
## thoigian         9.0972e-04  4.9952e-04  1.8212 0.1282110    
## tile             3.1875e-03  8.9489e-03  0.3562 0.7362351    
## nongdo           3.4521e-03  9.7798e-04  3.5298 0.0167431 *  
## thoigian:tile    4.5833e-05  3.9951e-05  1.1472 0.3031949    
## thoigian:nongdo -4.1667e-06  3.9951e-06 -1.0430 0.3447502    
## tile:nongdo      3.7500e-05  5.9926e-05  0.6258 0.5589170    
## thoigian^2      -6.1574e-06  2.7721e-06 -2.2212 0.0770144 .  
## tile^2          -1.2604e-03  6.2373e-04 -2.0208 0.0992729 .  
## nongdo^2        -1.5729e-05  6.2373e-06 -2.5218 0.0530589 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Multiple R-squared:  0.9832, Adjusted R-squared:  0.9529 
## F-statistic:  32.5 on 9 and 5 DF,  p-value: 0.0006539
## 
## Analysis of Variance Table
## 
## Response: hl_iris
##                             Df    Sum Sq    Mean Sq F value    Pr(>F)
## FO(thoigian, tile, nongdo)   3 0.0063512 0.00211708 92.1139 8.523e-05
## TWI(thoigian, tile, nongdo)  3 0.0000643 0.00002142  0.9318   0.49033
## PQ(thoigian, tile, nongdo)   3 0.0003069 0.00010231  4.4513   0.07077
## Residuals                    5 0.0001149 0.00002298                  
## Lack of fit                  3 0.0001102 0.00003675 15.7500   0.06029
## Pure error                   2 0.0000047 0.00000233                  
## 
## Stationary point of response surface:
##    thoigian        tile      nongdo 
## -0.02259811  1.23582141 -0.01864375 
## 
## Eigenanalysis:
## eigen() decomposition
## $values
## [1]  0.000000000  0.000000000 -0.001261119
## 
## $vectors
##                 [,1]       [,2]        [,3]
## thoigian  0.98510126 0.17100092  0.01828077
## tile      0.01542949 0.01798834 -0.99971914
## nongdo   -0.17128173 0.98510665  0.01508188
#1. Biểu đồ fix nongdo cồn

#- Giả định cố định nongdo ở một giá trị nhất định
fixed_nongdo <- mean(df$nongdo) 
#-- Tạo dữ liệu dự đoán
new_data1 <- expand.grid(thoigian=seq(min(df$thoigian), max(df$thoigian), length.out=100),
                        tile=seq(min(df$tile), max(df$tile), length.out=100),
                        nongdo=fixed_nongdo)
new_data1$iris_pred <- predict(model1, newdata=new_data1) 
#-- Tạo biểu đồ nhiệt với geom_tile() và thêm các vạch với geom_contour() -> OK 
ggplot(new_data1, aes(x=thoigian, y=tile, fill=iris_pred)) +
  geom_tile() +
  geom_contour(aes(z=iris_pred), color="white") + # Thêm các vạch mức độ
  scale_fill_viridis_c() +
  labs(title="Hàm lượng irisflorentin theo Thời Gian và Tỉ Lệ DM/DL",
       x="Thời Gian", y="Tỉ Lệ DM/DL", fill="Hàm lượng irisflorentin (%)") +
  theme_minimal()
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

#-- Tạo biểu đồ 3D từng cặp dữ liệu
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plot_ly(data = new_data1, x = ~thoigian, y = ~tile, z = ~iris_pred, 
        type = "scatter3d", mode = "markers",
        marker = list(size = 2, color = ~iris_pred, colorscale = 'Viridis', opacity = 0.8)) %>%
  layout(scene = list(xaxis = list(title = 'Thời Gian'),
                      yaxis = list(title = 'Tỉ Lệ DM/DL'),
                      zaxis = list(title = 'Hàm lượng irisflorentin (%)'),
                      aspectmode = 'cube'))  # Cân đối tỉ lệ cho ba trục
#2. Biểu đồ fix thoigian chiết

#- Giả định cố định nongdo ở một giá trị nhất định
fixed_thoigian <- mean(df$thoigian) 
#-- Tạo dữ liệu dự đoán
new_data2 <- expand.grid(nongdo=seq(min(df$nongdo), max(df$nongdo), length.out=100),
                        tile=seq(min(df$tile), max(df$tile), length.out=100),
                        thoigian=fixed_thoigian)
new_data2$iris_pred <- predict(model1, newdata=new_data2) 
#-- Tạo biểu đồ nhiệt với geom_tile() và thêm các vạch với geom_contour() -> OK 
ggplot(new_data2, aes(x=nongdo, y=tile, fill=iris_pred)) +
  geom_tile() +
  geom_contour(aes(z=iris_pred), color="white") + # Thêm các vạch mức độ
  scale_fill_viridis_c() +
  labs(title="Hàm lượng irisflorentin theo Tỉ lệ DM/DL và Nồng độ",
       x="Nồng độ ethanol", y="Tỉ lệ DM/DL", fill="Hàm lượng irisflorentin (%)") +
  theme_minimal()
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

#-- Tạo biểu đồ 3D từng cặp dữ liệu
plot_ly(data = new_data2, x = ~tile, y = ~nongdo, z = ~iris_pred, 
        type = "scatter3d", mode = "markers",
        marker = list(size = 2, color = ~iris_pred, colorscale = 'Viridis', opacity = 0.8)) %>%
  layout(scene = list(xaxis = list(title = 'Tỉ lệ DM/DL'),
                      yaxis = list(title = 'Nồng độ ethanol'),
                      zaxis = list(title = 'Hàm lượng irisflorentin (%)'),
                      aspectmode = 'cube'))  # Cân đối tỉ lệ cho ba trục
#3. Biểu đồ fix tile dung môi/ dược liệu

#- Giả định cố định nongdo ở một giá trị nhất định
fixed_tile <- mean(df$tile) 
#-- Tạo dữ liệu dự đoán
new_data3 <- expand.grid(thoigian=seq(min(df$thoigian), max(df$thoigian), length.out=100),
                        nongdo=seq(min(df$nongdo), max(df$nongdo), length.out=100),
                        tile=fixed_tile)
new_data3$iris_pred <- predict(model1, newdata=new_data3) 
#-- Tạo biểu đồ nhiệt với geom_tile() và thêm các vạch với geom_contour() -> OK 
ggplot(new_data3, aes(x=thoigian, y=nongdo, fill=iris_pred)) +
  geom_tile() +
  geom_contour(aes(z=iris_pred), color="white") + # Thêm các vạch mức độ
  scale_fill_viridis_c() +
  labs(title="Hàm lượng irisflorentin theo Tỉ lệ thời gian chiết và Nồng độ ethanol",
       x="Thời gian chiết", y="Nồng độ ethanol", fill="Hàm lượng irisflorentin (%)") +
  theme_minimal()
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

#-- Tạo biểu đồ 3D từng cặp dữ liệu
plot_ly(data = new_data3, x = ~thoigian, y = ~nongdo, z = ~iris_pred, 
        type = "scatter3d", mode = "markers",
        marker = list(size = 2, color = ~iris_pred, colorscale = 'Viridis', opacity = 0.8)) %>%
  layout(scene = list(xaxis = list(title = 'Thời gian chiết'),
                      yaxis = list(title = 'Nồng độ ethanol'),
                      zaxis = list(title = 'Hàm lượng irisflorentin (%)'),
                      aspectmode = 'cube'))  # Cân đối tỉ lệ cho ba trục

Vẽ biểu đồ hàm lượng cao

model2 <- rsm(hl_cao ~ FO(thoigian, tile, nongdo) + TWI(thoigian, tile, nongdo) + SO(thoigian, tile, nongdo), data=df)
summary(model2)
## Near-stationary-ridge situation detected -- stationary point altered
##  Change 'threshold' if this is not what you intend
## 
## Call:
## rsm(formula = hl_cao ~ FO(thoigian, tile, nongdo) + TWI(thoigian, 
##     tile, nongdo) + SO(thoigian, tile, nongdo), data = df)
## 
##                    Estimate  Std. Error  t value  Pr(>|t|)    
## (Intercept)     -5.8750e-01  1.1896e+00  -0.4939 0.6423372    
## thoigian         7.2972e-03  1.1366e-02   0.6420 0.5491273    
## tile             1.9405e+00  2.0362e-01   9.5300 0.0002152 ***
## nongdo           3.3022e-01  2.2253e-02  14.8397 2.514e-05 ***
## thoigian:tile   -7.8333e-04  9.0903e-04  -0.8617 0.4282204    
## thoigian:nongdo  1.4417e-04  9.0903e-05   1.5859 0.1736097    
## tile:nongdo      7.3750e-04  1.3635e-03   0.5409 0.6118118    
## thoigian^2      -4.4630e-05  6.3076e-05  -0.7075 0.5108314    
## tile^2          -1.0517e-01  1.4192e-02  -7.4102 0.0007046 ***
## nongdo^2        -2.3704e-03  1.4192e-04 -16.7023 1.406e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Multiple R-squared:  0.997,  Adjusted R-squared:  0.9915 
## F-statistic: 182.7 on 9 and 5 DF,  p-value: 9.358e-06
## 
## Analysis of Variance Table
## 
## Response: hl_cao
##                             Df  Sum Sq Mean Sq  F value    Pr(>F)
## FO(thoigian, tile, nongdo)   3 15.7389  5.2463 440.8984 1.771e-06
## TWI(thoigian, tile, nongdo)  3  0.0422  0.0141   1.1834    0.4043
## PQ(thoigian, tile, nongdo)   3  3.7810  1.2603 105.9172 6.052e-05
## Residuals                    5  0.0595  0.0119                   
## Lack of fit                  3  0.0559  0.0186  10.3858    0.0891
## Pure error                   2  0.0036  0.0018                   
## 
## Stationary point of response surface:
##  thoigian      tile    nongdo 
## -2.122083  9.482860 70.999301 
## 
## Eigenanalysis:
## eigen() decomposition
## $values
## [1]  0.00000000 -0.00237124 -0.10516945
## 
## $vectors
##                  [,1]         [,2]         [,3]
## thoigian  0.999532544 -0.030344589 -0.003728141
## tile     -0.003617472  0.003701163 -0.999986608
## nongdo    0.030357981  0.999532644  0.003589662
#1. Biểu đồ fix nongdo cồn

#- Giả định cố định nongdo ở một giá trị nhất định
fixed_nongdo <- mean(df$nongdo) 
#-- Tạo dữ liệu dự đoán
new_data1 <- expand.grid(thoigian=seq(min(df$thoigian), max(df$thoigian), length.out=100),
                        tile=seq(min(df$tile), max(df$tile), length.out=100),
                        nongdo=fixed_nongdo)
new_data1$cao_pred <- predict(model2, newdata=new_data1) 
#-- Tạo biểu đồ nhiệt với geom_tile() và thêm các vạch với geom_contour() -> OK 
ggplot(new_data1, aes(x=thoigian, y=tile, fill=cao_pred)) +
  geom_tile() +
  geom_contour(aes(z=cao_pred), color="white") + # Thêm các vạch mức độ
  scale_fill_viridis_c() +
  labs(title="Hàm lượng cao theo Thời Gian và Tỉ Lệ DM/DL",
       x="Thời Gian", y="Tỉ Lệ DM/DL", fill="Hàm lượng cao (%)") +
  theme_minimal()
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

#-- Tạo biểu đồ 3D từng cặp dữ liệu
library(plotly)
plot_ly(data = new_data1, x = ~thoigian, y = ~tile, z = ~cao_pred, 
        type = "scatter3d", mode = "markers",
        marker = list(size = 2, color = ~cao_pred, colorscale = 'Viridis', opacity = 0.8)) %>%
  layout(scene = list(xaxis = list(title = 'Thời Gian'),
                      yaxis = list(title = 'Tỉ Lệ DM/DL'),
                      zaxis = list(title = 'Hàm lượng cao (%)'),
                      aspectmode = 'cube'))  # Cân đối tỉ lệ cho ba trục
#2. Biểu đồ fix thoigian chiết

#- Giả định cố định nongdo ở một giá trị nhất định
fixed_thoigian <- mean(df$thoigian) 
#-- Tạo dữ liệu dự đoán
new_data2 <- expand.grid(nongdo=seq(min(df$nongdo), max(df$nongdo), length.out=100),
                        tile=seq(min(df$tile), max(df$tile), length.out=100),
                        thoigian=fixed_thoigian)
new_data2$cao_pred <- predict(model2, newdata=new_data2) 
#-- Tạo biểu đồ nhiệt với geom_tile() và thêm các vạch với geom_contour() -> OK 
ggplot(new_data2, aes(x=nongdo, y=tile, fill=cao_pred)) +
  geom_tile() +
  geom_contour(aes(z=cao_pred), color="white") + # Thêm các vạch mức độ
  scale_fill_viridis_c() +
  labs(title="Hàm lượng cao theo Tỉ lệ DM/DL và Nồng độ",
       x="Nồng độ ethanol", y="Tỉ lệ DM/DL", fill="Hàm lượng cao (%)") +
  theme_minimal()
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

#-- Tạo biểu đồ 3D từng cặp dữ liệu
plot_ly(data = new_data2, x = ~tile, y = ~nongdo, z = ~cao_pred, 
        type = "scatter3d", mode = "markers",
        marker = list(size = 2, color = ~cao_pred, colorscale = 'Viridis', opacity = 0.8)) %>%
  layout(scene = list(xaxis = list(title = 'Tỉ lệ DM/DL'),
                      yaxis = list(title = 'Nồng độ ethanol'),
                      zaxis = list(title = 'Hàm lượng cao (%)'),
                      aspectmode = 'cube'))  # Cân đối tỉ lệ cho ba trục
#3. Biểu đồ fix tile dung môi/ dược liệu

#- Giả định cố định nongdo ở một giá trị nhất định
fixed_tile <- mean(df$tile) 
#-- Tạo dữ liệu dự đoán
new_data3 <- expand.grid(thoigian=seq(min(df$thoigian), max(df$thoigian), length.out=100),
                        nongdo=seq(min(df$nongdo), max(df$nongdo), length.out=100),
                        tile=fixed_tile)
new_data3$cao_pred <- predict(model2, newdata=new_data3) 
#-- Tạo biểu đồ nhiệt với geom_tile() và thêm các vạch với geom_contour() -> OK 
ggplot(new_data3, aes(x=thoigian, y=nongdo, fill=cao_pred)) +
  geom_tile() +
  geom_contour(aes(z=cao_pred), color="white") + # Thêm các vạch mức độ
  scale_fill_viridis_c() +
  labs(title="Hàm lượng cao theo Tỉ lệ thời gian chiết và Nồng độ ethanol",
       x="Thời gian chiết", y="Nồng độ ethanol", fill="Hàm lượng cao (%)") +
  theme_minimal()
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

#-- Tạo biểu đồ 3D từng cặp dữ liệu
plot_ly(data = new_data3, x = ~thoigian, y = ~nongdo, z = ~cao_pred, 
        type = "scatter3d", mode = "markers",
        marker = list(size = 2, color = ~cao_pred, colorscale = 'Viridis', opacity = 0.8)) %>%
  layout(scene = list(xaxis = list(title = 'Thời gian chiết'),
                      yaxis = list(title = 'Nồng độ ethanol'),
                      zaxis = list(title = 'Hàm lượng cao (%)'),
                      aspectmode = 'cube'))  # Cân đối tỉ lệ cho ba trục

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.