#### TIỀN XỬ LÝ DỮ LIỆU:

# 1. ĐỌC DỮ LIỆU & XEM TRƯỚC

# Đọc dữ liệu từ file CSV
printer_data <- read.csv("D:/HCMUT/HK252/BTL XSTK/data.csv")    

# Xem 10 dòng đầu tiên
head(printer_data,10)
##    layer_height wall_thickness infill_density infill_pattern nozzle_temperature
## 1          0.02              8             90           grid                220
## 2          0.02              7             90      honeycomb                225
## 3          0.02              1             80           grid                230
## 4          0.02              4             70      honeycomb                240
## 5          0.02              6             90           grid                250
## 6          0.02             10             40      honeycomb                200
## 7          0.02              5             10           grid                205
## 8          0.02             10             10      honeycomb                210
## 9          0.02              9             70           grid                215
## 10         0.02              8             40      honeycomb                220
##    bed_temperature print_speed material fan_speed roughness tension_strenght
## 1               60          40      abs         0        25               18
## 2               65          40      abs        25        32               16
## 3               70          40      abs        50        40                8
## 4               75          40      abs        75        68               10
## 5               80          40      abs       100        92                5
## 6               60          40      pla         0        60               24
## 7               65          40      pla        25        55               12
## 8               70          40      pla        50        21               14
## 9               75          40      pla        75        24               27
## 10              80          40      pla       100        30               25
##    elongation
## 1         1.2
## 2         1.4
## 3         0.8
## 4         0.5
## 5         0.7
## 6         1.1
## 7         1.3
## 8         1.5
## 9         1.4
## 10        1.7
# 2. CHỌN BIẾN CẦN PHÂN TÍCH

# Lấy các biến liên quan đến độ nhám (roughness)
new_DF <- printer_data[,c("layer_height","wall_thickness","infill_density",
                          "infill_pattern","nozzle_temperature","bed_temperature","print_speed",
                          "material","fan_speed","roughness")]

head(new_DF,10)
##    layer_height wall_thickness infill_density infill_pattern nozzle_temperature
## 1          0.02              8             90           grid                220
## 2          0.02              7             90      honeycomb                225
## 3          0.02              1             80           grid                230
## 4          0.02              4             70      honeycomb                240
## 5          0.02              6             90           grid                250
## 6          0.02             10             40      honeycomb                200
## 7          0.02              5             10           grid                205
## 8          0.02             10             10      honeycomb                210
## 9          0.02              9             70           grid                215
## 10         0.02              8             40      honeycomb                220
##    bed_temperature print_speed material fan_speed roughness
## 1               60          40      abs         0        25
## 2               65          40      abs        25        32
## 3               70          40      abs        50        40
## 4               75          40      abs        75        68
## 5               80          40      abs       100        92
## 6               60          40      pla         0        60
## 7               65          40      pla        25        55
## 8               70          40      pla        50        21
## 9               75          40      pla        75        24
## 10              80          40      pla       100        30
# 3. KIỂM TRA DỮ LIỆU KHUYẾT

# Kiểm tra giá trị thiếu (NA)
colSums(is.na(new_DF))
##       layer_height     wall_thickness     infill_density     infill_pattern 
##                  0                  0                  0                  0 
## nozzle_temperature    bed_temperature        print_speed           material 
##                  0                  0                  0                  0 
##          fan_speed          roughness 
##                  0                  0
# 4. KIỂM TRA NGOẠI LAI (OUTLIERS)

# Chọn các biến liên tục
continous_data <- new_DF[,c("layer_height","wall_thickness","infill_density",
                            "nozzle_temperature","bed_temperature","print_speed",
                            "fan_speed","roughness")]

# Dùng IQR để phát hiện ngoại lai
sapply(continous_data, function(x) {
  out <- x < quantile(x,0.25) - 1.5*IQR(x) |
    x > quantile(x,0.75) + 1.5*IQR(x)
  
  c(count = sum(out),        # số lượng outlier
    rate = round(mean(out)*100,2)) # tỷ lệ %
})
##       layer_height wall_thickness infill_density nozzle_temperature
## count            0              0              0                  0
## rate             0              0              0                  0
##       bed_temperature print_speed fan_speed roughness
## count               0          10         0         0
## rate                0          20         0         0
# Kiểm tra dữ liệu print_speed
table(new_DF$print_speed)
## 
##  40  60 120 
##  20  20  10
# 5. KIỂM TRA DỮ LIỆU TRÙNG LẶP

# Kiểm tra bản ghi trùng lặp
sum(duplicated(new_DF))
## [1] 0
# 6. KHAI BÁO BIẾN PHÂN LOẠI
# Chuyển biến phân loại sang factor
new_DF$infill_pattern <- as.factor(new_DF$infill_pattern)
new_DF$material <- as.factor(new_DF$material)


#### THỐNG KÊ MÔ TẢ

# 1. TÍNH TOÁN THỐNG KÊ MÔ TẢ

# Tính toán hống kê mô tả cho dữ liệu
summary(new_DF)
##   layer_height   wall_thickness  infill_density   infill_pattern
##  Min.   :0.020   Min.   : 1.00   Min.   :10.0   grid     :25    
##  1st Qu.:0.060   1st Qu.: 3.00   1st Qu.:40.0   honeycomb:25    
##  Median :0.100   Median : 5.00   Median :50.0                   
##  Mean   :0.106   Mean   : 5.22   Mean   :53.4                   
##  3rd Qu.:0.150   3rd Qu.: 7.00   3rd Qu.:80.0                   
##  Max.   :0.200   Max.   :10.00   Max.   :90.0                   
##  nozzle_temperature bed_temperature  print_speed  material   fan_speed  
##  Min.   :200.0      Min.   :60      Min.   : 40   abs:25   Min.   :  0  
##  1st Qu.:210.0      1st Qu.:65      1st Qu.: 40   pla:25   1st Qu.: 25  
##  Median :220.0      Median :70      Median : 60            Median : 50  
##  Mean   :221.5      Mean   :70      Mean   : 64            Mean   : 50  
##  3rd Qu.:230.0      3rd Qu.:75      3rd Qu.: 60            3rd Qu.: 75  
##  Max.   :250.0      Max.   :80      Max.   :120            Max.   :100  
##    roughness    
##  Min.   : 21.0  
##  1st Qu.: 92.0  
##  Median :165.5  
##  Mean   :170.6  
##  3rd Qu.:239.2  
##  Max.   :368.0
# 2. TRỰC QUAN HÓA BẰNG CÁC ĐỒ THỊ
library(ggplot2)
library(reshape2)

# Histogram roughness
ggplot(new_DF, aes(x = roughness)) +
  geom_histogram(fill = "pink", bins = 8, color = "black") +
  labs(title = "Histogram of Roughness",
       x = "roughness", y = "Frequency") +
  theme_minimal()

# Boxplot: roughness theo infill_pattern
ggplot(new_DF, aes(x = infill_pattern, y = roughness, fill = infill_pattern)) +
  geom_boxplot() +
  labs(title = "Roughness vs Infill Pattern",
       x = "Infill Pattern", y = "Roughness") +
  theme_minimal()

# Boxplot: roughness theo material
ggplot(new_DF, aes(x = material, y = roughness, fill = material)) +
  geom_boxplot() +
  labs(title = "Roughness vs Material",
       x = "Material", y = "Roughness") +
  theme_minimal()

# Hàm vẽ scatter + regression line
plot_scatter <- function(x_var){
  ggplot(new_DF, aes_string(x = x_var, y = "roughness")) +
    geom_point(color = "blue", alpha = 0.6) +
    geom_smooth(method = "lm", se = FALSE, color = "red") +
    labs(title = paste(x_var, "& Roughness"),
         x = x_var, y = "Roughness") +
    theme_minimal()
}

# Vẽ scatter cho từng biến
plot_scatter("layer_height")
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'

plot_scatter("wall_thickness")
## `geom_smooth()` using formula = 'y ~ x'

plot_scatter("infill_density")
## `geom_smooth()` using formula = 'y ~ x'

plot_scatter("nozzle_temperature")
## `geom_smooth()` using formula = 'y ~ x'

plot_scatter("bed_temperature")
## `geom_smooth()` using formula = 'y ~ x'

plot_scatter("print_speed")
## `geom_smooth()` using formula = 'y ~ x'

plot_scatter("fan_speed")
## `geom_smooth()` using formula = 'y ~ x'

# Vẽ ma trận tương quan

cor_matrix <- cor(continous_data)
cor_melt <- melt(cor_matrix)

ggplot(cor_melt, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile() +
  geom_text(aes(label = round(value, 2)), size = 3) +
  scale_fill_gradient2(low = "red", high = "blue", mid = "white", midpoint = 0) +
  labs(title = "Correlation Matrix") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

#### THỐNG KÊ SUY DIỄN:

# 1. KIỂM ĐỊNH TRUNG BÌNH 2 MẪU: (ABS vs PLA)

# Tách dữ liệu theo vật liệu
abs_data <- subset(new_DF, material=="abs")
pla_data <- subset(new_DF, material=="pla")

# QQ-plot kiểm tra phân phối chuẩn
par(mfrow=c(1,2))
qqnorm(abs_data$roughness,main="QQ-Plot - ABS")
qqline(abs_data$roughness,col="red")

qqnorm(pla_data$roughness,main="QQ-Plot - PLA")
qqline(pla_data$roughness,col="red")

# Shapiro test
by(new_DF$roughness,new_DF$material,shapiro.test)
## new_DF$material: abs
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.94235, p-value = 0.1677
## 
## ------------------------------------------------------------ 
## new_DF$material: pla
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.97437, p-value = 0.7561
# Kiểm định phương sai
var.test(roughness~material,data=new_DF)
## 
##  F test to compare two variances
## 
## data:  roughness by material
## F = 1.8454, num df = 24, denom df = 24, p-value = 0.1405
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.8132207 4.1877771
## sample estimates:
## ratio of variances 
##           1.845423
# T-test
t.test(roughness~material,var.equal=TRUE,data=new_DF)
## 
##  Two Sample t-test
## 
## data:  roughness by material
## t = 1.6613, df = 48, p-value = 0.1032
## alternative hypothesis: true difference in means between group abs and group pla is not equal to 0
## 95 percent confidence interval:
##   -9.615162 101.055162
## sample estimates:
## mean in group abs mean in group pla 
##            193.44            147.72
# 2. ANOVA 2 YẾU TỐ

# Tạo nhóm nhiệt độ nozzle
new_DF$nozzle_temperature_group <- cut(new_DF$nozzle_temperature,
                                       breaks = c(-Inf,210,225, Inf),
                                       labels = c("Low", "Medium", "High"))

# Chuyển sang factor
new_DF$layer_height <- as.factor(new_DF$layer_height)
new_DF$nozzle_temperature_group <- as.factor(new_DF$nozzle_temperature_group)

# Bảng chéo
table(new_DF$layer_height, new_DF$nozzle_temperature_group)
##       
##        Low Medium High
##   0.02   3      4    3
##   0.06   3      4    3
##   0.1    3      4    3
##   0.15   3      4    3
##   0.2    3      4    3
# Mô hình ANOVA 2 yếu tố
model_anova <- aov(roughness ~ layer_height*nozzle_temperature_group, data = new_DF)
summary(model_anova)
##                                       Df Sum Sq Mean Sq F value   Pr(>F)    
## layer_height                           4 326957   81739  93.549  < 2e-16 ***
## nozzle_temperature_group               2 100223   50112  57.352 9.01e-12 ***
## layer_height:nozzle_temperature_group  8  22819    2852   3.264  0.00697 ** 
## Residuals                             35  30582     874                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Tukey test
TukeyHSD(model_anova)$layer_height
##            diff       lwr       upr        p adj
## 0.06-0.02  78.3  40.29344 116.30656 9.295319e-06
## 0.1-0.02  150.5 112.49344 188.50656 2.413514e-12
## 0.15-0.02 162.2 124.19344 200.20656 1.739719e-13
## 0.2-0.02  238.4 200.39344 276.40656 0.000000e+00
## 0.1-0.06   72.2  34.19344 110.20656 3.730628e-05
## 0.15-0.06  83.9  45.89344 121.90656 2.599693e-06
## 0.2-0.06  160.1 122.09344 198.10656 3.145262e-13
## 0.15-0.1   11.7 -26.30656  49.70656 9.004931e-01
## 0.2-0.1    87.9  49.89344 125.90656 1.050935e-06
## 0.2-0.15   76.2  38.19344 114.20656 1.500107e-05
TukeyHSD(model_anova)$nozzle_temperature_group
##                  diff       lwr        upr        p adj
## Medium-Low  -32.20000 -56.90887  -7.491134 8.238541e-03
## High-Low     74.86667  48.45178 101.281555 1.370368e-07
## High-Medium 107.06667  82.35780 131.775533 5.212164e-12
# Interaction plot
par(mfrow=c(1,1))
interaction.plot(x.factor = new_DF$layer_height,
                 trace.factor = new_DF$nozzle_temperature_group,
                 response = new_DF$roughness,
                 col = c("red", "blue"),
                 lwd = 2)

# Kiểm tra các giả định của ANOVA
par(mfrow=c(1,2))
res <- residuals(model_anova)
hist(res)
qqnorm(res)
qqline(res, col="red")

# Normality
shapiro.test(res)
## 
##  Shapiro-Wilk normality test
## 
## data:  res
## W = 0.9824, p-value = 0.6566
# Homogeneity
library(car)
## Loading required package: carData
leveneTest(roughness ~ layer_height*nozzle_temperature_group, data = new_DF)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group 14   0.752 0.7097
##       35
# 3. HỒI QUY TUYẾN TÍNH
# Chuyển lại numeric
new_DF$layer_height <- as.numeric(as.character(new_DF$layer_height))
new_DF$nozzle_temperature_group <- NULL

# Model đầy đủ
model_1 <- lm(roughness~layer_height+nozzle_temperature+wall_thickness+infill_density+
                infill_pattern+bed_temperature+print_speed+material,data=new_DF) 
summary(model_1)
## 
## Call:
## lm(formula = roughness ~ layer_height + nozzle_temperature + 
##     wall_thickness + infill_density + infill_pattern + bed_temperature + 
##     print_speed + material, data = new_DF)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -72.746 -24.332  -1.641  20.304  96.552 
## 
## Coefficients:
##                           Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             -2.371e+03  3.716e+02  -6.379 1.25e-07 ***
## layer_height             1.269e+03  8.765e+01  14.483  < 2e-16 ***
## nozzle_temperature       1.506e+01  2.529e+00   5.953 5.05e-07 ***
## wall_thickness           2.334e+00  2.189e+00   1.066  0.29259    
## infill_density          -4.231e-02  2.341e-01  -0.181  0.85742    
## infill_patternhoneycomb -1.255e-01  1.128e+01  -0.011  0.99117    
## bed_temperature         -1.613e+01  3.251e+00  -4.962 1.27e-05 ***
## print_speed              6.496e-01  2.060e-01   3.153  0.00302 ** 
## materialpla              2.985e+02  5.836e+01   5.114 7.78e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 38.24 on 41 degrees of freedom
## Multiple R-squared:  0.8752, Adjusted R-squared:  0.8509 
## F-statistic: 35.95 on 8 and 41 DF,  p-value: 3.834e-16
# Model rút gọn
model_2 <- lm(roughness~layer_height+nozzle_temperature+
                bed_temperature+print_speed+material,data=new_DF) 
summary(model_2)
## 
## Call:
## lm(formula = roughness ~ layer_height + nozzle_temperature + 
##     bed_temperature + print_speed + material, data = new_DF)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -74.084 -26.500  -1.662  22.585  92.356 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        -2310.7356   353.2009  -6.542 5.38e-08 ***
## layer_height        1246.5353    83.1780  14.986  < 2e-16 ***
## nozzle_temperature    14.7774     2.3979   6.163 1.95e-07 ***
## bed_temperature      -15.8078     3.0895  -5.117 6.55e-06 ***
## print_speed            0.5538     0.1804   3.070  0.00366 ** 
## materialpla          294.1610    56.1586   5.238 4.38e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 37.44 on 44 degrees of freedom
## Multiple R-squared:  0.8717, Adjusted R-squared:  0.8571 
## F-statistic: 59.78 on 5 and 44 DF,  p-value: < 2.2e-16
# Kiểm tra đa cộng tuyến
library(car)
vif(model_2)
##       layer_height nozzle_temperature    bed_temperature        print_speed 
##            1.00309           44.15385           17.02564            1.00309 
##           material 
##           28.12821
# Model sau khi xử lý đa cộng tuyến
model_3 <- lm(roughness~layer_height+nozzle_temperature+print_speed,data=new_DF) 
summary(model_3)
## 
## Call:
## lm(formula = roughness ~ layer_height + nozzle_temperature + 
##     print_speed, data = new_DF)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -77.37 -31.87  -8.78  34.56 107.49 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        -512.9882   101.6645  -5.046 7.53e-06 ***
## layer_height       1246.5353   103.7800  12.011 8.78e-16 ***
## nozzle_temperature    2.3295     0.4502   5.174 4.89e-06 ***
## print_speed           0.5538     0.2251   2.461   0.0177 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 46.71 on 46 degrees of freedom
## Multiple R-squared:  0.7912, Adjusted R-squared:  0.7775 
## F-statistic: 58.09 on 3 and 46 DF,  p-value: 1.115e-15
vif(model_3)
##       layer_height nozzle_temperature        print_speed 
##            1.00309            1.00000            1.00309
# Kiểm tra giả định
par(mfrow=c(2,2))
plot(model_3)

# Durbin-Watson test
library(lmtest) 
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
dwtest(model_3)
## 
##  Durbin-Watson test
## 
## data:  model_3
## DW = 0.74163, p-value = 3.386e-08
## alternative hypothesis: true autocorrelation is greater than 0
#### ĐỀ XUẤT MỞ RỘNG:


# 1 RESPONSE SURFACE METHOD (RSM)

library(rsm)

# Chuẩn hóa biến
m1 <- mean(new_DF$layer_height); s1 <- sd(new_DF$layer_height)
m2 <- mean(new_DF$nozzle_temperature); s2 <- sd(new_DF$nozzle_temperature)
m3 <- mean(new_DF$print_speed); s3 <- sd(new_DF$print_speed)

# Code dữ liệu
coded_dat <- coded.data(new_DF,
                        x1 ~ (layer_height - m1)/s1,
                        x2 ~ (nozzle_temperature - m2)/s2,
                        x3 ~ (print_speed - m3)/s3)

# Mô hình RSM
model_rsm <- rsm(roughness ~ FO(x1, x2, x3) +
                   TWI(x1, x2, x3) +
                   PQ(x1, x2),
                 data = coded_dat)

summary(model_rsm)
## 
## Call:
## rsm(formula = roughness ~ FO(x1, x2, x3) + TWI(x1, x2, x3) + 
##     PQ(x1, x2), data = coded_dat)
## 
##             Estimate Std. Error t value  Pr(>|t|)    
## (Intercept) 137.5916     9.3565 14.7055 < 2.2e-16 ***
## x1           55.0801    10.3714  5.3108 4.107e-06 ***
## x2           21.4289     4.1315  5.1867 6.139e-06 ***
## x3           14.1869     6.9602  2.0383   0.04801 *  
## x1:x2         5.3712     3.8686  1.3884   0.17251    
## x1:x3       -37.1380    14.5307 -2.5558   0.01440 *  
## x2:x3        17.5586     3.8681  4.5394 4.867e-05 ***
## x1^2          1.5581     8.1071  0.1922   0.84854    
## x2^2         30.0410     3.5903  8.3672 2.086e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Multiple R-squared:  0.9389, Adjusted R-squared:  0.927 
## F-statistic: 78.73 on 8 and 41 DF,  p-value: < 2.2e-16
## 
## Analysis of Variance Table
## 
## Response: roughness
##                 Df Sum Sq Mean Sq  F value    Pr(>F)
## FO(x1, x2, x3)   3 380218  126739 176.9096 < 2.2e-16
## TWI(x1, x2, x3)  3  20807    6936   9.6812 5.897e-05
## PQ(x1, x2)       2  50182   25091  35.0235 1.346e-09
## Residuals       41  29373     716                   
## Lack of fit     36  29315     814  70.1981 7.737e-05
## Pure error       5     58      12                   
## 
## Stationary point of response surface:
##         x1         x2         x3 
##  0.0224423 -0.7605069  1.3750121 
## 
## Stationary point in original units:
##       layer_height nozzle_temperature        print_speed 
##          0.1074453        210.2292881        104.8241106 
## 
## Eigenanalysis:
## eigen() decomposition
## $values
## [1]  32.65982  18.12250 -19.18317
## 
## $vectors
##          [,1]       [,2]       [,3]
## x1 -0.1059075 -0.7351160  0.6696178
## x2  0.9435473 -0.2868364 -0.1656607
## x3  0.3138506  0.6142714  0.7239948
# Hướng tối ưu
st <- steepest(model_rsm)
## Path of steepest ascent from ridge analysis:
# Tìm giá trị độ nhám tối ưu

results <- data.frame()

for(i in 1:nrow(st)){
  # chuyển về giá trị thực
  layer_real <- st$x1[i]*s1 + m1
  temp_real  <- st$x2[i]*s2 + m2
  speed_real <- st$x3[i]*s3 + m3
  
  # dự đoán
  pred <- predict(model_3,
                  newdata = data.frame(
                    layer_height = layer_real,
                    nozzle_temperature = temp_real,
                    print_speed = speed_real
                  ))
  
  results <- rbind(results,
                   data.frame(dist = st$dist[i],
                              layer = layer_real,
                              temp = temp_real,
                              speed = speed_real,
                              roughness = pred))
}

# Kết quả tối ưu
results
##     dist     layer     temp    speed roughness
## 1    0.0 0.1060000 221.5000 64.00000  170.5800
## 11   0.5 0.1318231 225.9016 64.95015  213.5494
## 12   1.0 0.1470851 232.8969 65.27677  249.0504
## 13   1.5 0.1535892 240.7665 67.50369  276.7238
## 14   2.0 0.1555211 248.6361 70.91831  299.3555
## 15   2.5 0.1553923 256.3426 74.86738  319.3348
## 16   3.0 0.1541688 263.9010 79.08369  337.7522
## 17   3.5 0.1523012 271.3260 83.44846  355.1384
## 18   4.0 0.1500474 278.6769 87.90230  371.9197
## 19   4.5 0.1475359 285.9685 92.38584  388.2582
## 110  5.0 0.1448956 293.2009 96.92876  404.3310
results[which.min(results$roughness), ]
##   dist layer  temp speed roughness
## 1    0 0.106 221.5    64    170.58
# 2. MÔ HÌNH HỒI QUY DỰ BÁO CHO ĐỘ BỀN VÀ ĐỘ GIÃN (STRENGTH & ELONGATION)

# Tension Strength
model_tension_strenght <- lm(tension_strenght~layer_height+nozzle_temperature+wall_thickness+infill_density+
                               infill_pattern+bed_temperature+print_speed+material,data=printer_data)

best_model_tension_strenght <- step(model_tension_strenght)
## Start:  AIC=179.99
## tension_strenght ~ layer_height + nozzle_temperature + wall_thickness + 
##     infill_density + infill_pattern + bed_temperature + print_speed + 
##     material
## 
##                      Df Sum of Sq    RSS    AIC
## - print_speed         1      8.38 1284.9 178.32
## - infill_pattern      1     15.01 1291.5 178.58
## <none>                            1276.5 179.99
## - material            1    128.58 1405.1 182.79
## - bed_temperature     1    139.90 1416.4 183.19
## - nozzle_temperature  1    250.56 1527.1 186.95
## - wall_thickness      1    348.52 1625.0 190.06
## - layer_height        1    588.52 1865.0 196.95
## - infill_density      1    708.24 1984.7 200.06
## 
## Step:  AIC=178.32
## tension_strenght ~ layer_height + nozzle_temperature + wall_thickness + 
##     infill_density + infill_pattern + bed_temperature + material
## 
##                      Df Sum of Sq    RSS    AIC
## - infill_pattern      1     16.38 1301.3 176.95
## <none>                            1284.9 178.32
## - material            1    127.94 1412.8 181.07
## - bed_temperature     1    138.99 1423.9 181.46
## - nozzle_temperature  1    249.02 1533.9 185.18
## - wall_thickness      1    491.77 1776.6 192.52
## - layer_height        1    625.74 1910.6 196.16
## - infill_density      1    712.62 1997.5 198.38
## 
## Step:  AIC=176.95
## tension_strenght ~ layer_height + nozzle_temperature + wall_thickness + 
##     infill_density + bed_temperature + material
## 
##                      Df Sum of Sq    RSS    AIC
## <none>                            1301.3 176.95
## - material            1    125.70 1427.0 179.56
## - bed_temperature     1    134.29 1435.5 179.86
## - nozzle_temperature  1    242.85 1544.1 183.51
## - wall_thickness      1    476.21 1777.5 190.55
## - layer_height        1    620.46 1921.7 194.45
## - infill_density      1    752.02 2053.3 197.76
summary(best_model_tension_strenght)
## 
## Call:
## lm(formula = tension_strenght ~ layer_height + nozzle_temperature + 
##     wall_thickness + infill_density + bed_temperature + material, 
##     data = printer_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.4717 -4.1695  0.0914  3.8586 13.9581 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        167.02463   53.19880   3.140 0.003055 ** 
## layer_height        56.36695   12.44845   4.528 4.67e-05 ***
## nozzle_temperature  -1.02890    0.36321  -2.833 0.006996 ** 
## wall_thickness       1.11169    0.28024   3.967 0.000271 ***
## infill_density       0.16643    0.03339   4.985 1.06e-05 ***
## bed_temperature      0.98346    0.46685   2.107 0.041026 *  
## materialpla        -17.10365    8.39202  -2.038 0.047722 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.501 on 43 degrees of freedom
## Multiple R-squared:  0.6667, Adjusted R-squared:  0.6201 
## F-statistic: 14.33 on 6 and 43 DF,  p-value: 6.783e-09
# Model rút gọn
best_model2 <- lm(tension_strenght~layer_height+nozzle_temperature+wall_thickness+infill_density,
                  data=printer_data)
summary(best_model2)
## 
## Call:
## lm(formula = tension_strenght ~ layer_height + nozzle_temperature + 
##     wall_thickness + infill_density, data = printer_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.5700 -3.8045 -0.5332  3.0743 14.1982 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        61.73141   12.68790   4.865 1.44e-05 ***
## layer_height       56.81320   12.77956   4.446 5.67e-05 ***
## nozzle_temperature -0.27893    0.05672  -4.918 1.21e-05 ***
## wall_thickness      1.16029    0.28621   4.054 0.000197 ***
## infill_density      0.15082    0.03308   4.559 3.93e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.649 on 45 degrees of freedom
## Multiple R-squared:  0.6322, Adjusted R-squared:  0.5995 
## F-statistic: 19.34 on 4 and 45 DF,  p-value: 2.566e-09
# Elongation
model_elongation <- lm(elongation~layer_height+nozzle_temperature+wall_thickness+infill_density+
                         infill_pattern+bed_temperature+print_speed+material,data=printer_data)

best_model_elongation <- step(model_elongation)
## Start:  AIC=-69.64
## elongation ~ layer_height + nozzle_temperature + wall_thickness + 
##     infill_density + infill_pattern + bed_temperature + print_speed + 
##     material
## 
##                      Df Sum of Sq     RSS     AIC
## - infill_pattern      1    0.0446  8.7088 -71.384
## - wall_thickness      1    0.2900  8.9542 -69.995
## <none>                             8.6643 -69.641
## - print_speed         1    0.3924  9.0567 -69.426
## - material            1    1.3661 10.0304 -64.320
## - bed_temperature     1    1.5036 10.1678 -63.640
## - infill_density      1    2.6119 11.2762 -58.467
## - nozzle_temperature  1    2.7487 11.4130 -57.864
## - layer_height        1    7.7548 16.4191 -39.679
## 
## Step:  AIC=-71.38
## elongation ~ layer_height + nozzle_temperature + wall_thickness + 
##     infill_density + bed_temperature + print_speed + material
## 
##                      Df Sum of Sq     RSS     AIC
## - wall_thickness      1    0.2624  8.9713 -71.900
## <none>                             8.7088 -71.384
## - print_speed         1    0.4092  9.1180 -71.089
## - material            1    1.3548 10.0636 -66.155
## - bed_temperature     1    1.4804 10.1893 -65.534
## - nozzle_temperature  1    2.7193 11.4281 -59.797
## - infill_density      1    2.7397 11.4485 -59.708
## - layer_height        1    7.7218 16.4306 -41.644
## 
## Step:  AIC=-71.9
## elongation ~ layer_height + nozzle_temperature + infill_density + 
##     bed_temperature + print_speed + material
## 
##                      Df Sum of Sq     RSS     AIC
## <none>                             8.9713 -71.900
## - print_speed         1    0.9175  9.8887 -69.031
## - material            1    1.4352 10.4065 -66.480
## - bed_temperature     1    1.5981 10.5694 -65.703
## - nozzle_temperature  1    2.9072 11.8785 -59.865
## - infill_density      1    3.0158 11.9871 -59.410
## - layer_height        1    7.4871 16.4584 -43.559
summary(best_model_elongation)
## 
## Call:
## lm(formula = elongation ~ layer_height + nozzle_temperature + 
##     infill_density + bed_temperature + print_speed + material, 
##     data = printer_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.12952 -0.32192  0.05021  0.30777  0.83809 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        19.023103   4.384019   4.339 8.51e-05 ***
## layer_height        6.079479   1.014849   5.991 3.77e-07 ***
## nozzle_temperature -0.112096   0.030029  -3.733 0.000551 ***
## infill_density      0.010456   0.002750   3.802 0.000448 ***
## bed_temperature     0.106920   0.038632   2.768 0.008295 ** 
## print_speed        -0.004639   0.002212  -2.097 0.041909 *  
## materialpla        -1.824195   0.695511  -2.623 0.012016 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4568 on 43 degrees of freedom
## Multiple R-squared:  0.7053, Adjusted R-squared:  0.6642 
## F-statistic: 17.15 on 6 and 43 DF,  p-value: 5.335e-10
# Model rút gọn
best_model3 <- lm(elongation~layer_height+nozzle_temperature+infill_density+print_speed,
                  data=printer_data)
summary(best_model3)
## 
## Call:
## lm(formula = elongation ~ layer_height + nozzle_temperature + 
##     infill_density + print_speed, data = printer_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.92535 -0.37802 -0.00021  0.34897  1.00524 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         7.871575   1.058679   7.435 2.32e-09 ***
## layer_height        6.078323   1.076810   5.645 1.05e-06 ***
## nozzle_temperature -0.031633   0.004812  -6.574 4.38e-08 ***
## infill_density      0.008771   0.002824   3.105  0.00328 ** 
## print_speed        -0.004775   0.002346  -2.035  0.04778 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4847 on 45 degrees of freedom
## Multiple R-squared:  0.6528, Adjusted R-squared:  0.6219 
## F-statistic: 21.15 on 4 and 45 DF,  p-value: 7.235e-10