# === STEP 1: Install and Load Required Libraries ===
# Uncomment these if not already installed
# install.packages("openxlsx")
# install.packages("plotly")
# install.packages("ggplot2")

library(openxlsx)
## Warning: package 'openxlsx' was built under R version 4.3.3
library(ggplot2)
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
## 
## 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
library(magrittr)

# === STEP 2: Load Data ===
# Be sure the file path is correct (no extra spaces)
ftir <- read.xlsx("C:/Users/Administrator/Desktop/ANALYSIS with R/Final 1.xlsx", 
                  sheet = 1, 
                  colNames = TRUE, 
                  rowNames = TRUE)

# === STEP 3: Check structure ===
str(ftir)
## 'data.frame':    66 obs. of  971 variables:
##  $ Group             : chr  "Carob adulterated" "Carob adulterated" "Carob adulterated" "Carob adulterated" ...
##  $ 1.0309999999999999: num  551 554 549 548 546 ...
##  $ 1.032             : num  512 515 508 513 493 ...
##  $ 1.0329999999999999: num  500 503 494 503 498 ...
##  $ 1.034             : num  506 508 499 510 513 ...
##  $ 1.0349999999999999: num  539 542 532 545 531 ...
##  $ 1.036             : num  556 561 549 563 549 ...
##  $ 1.0369999999999999: num  572 576 565 578 576 ...
##  $ 1.038             : num  583 588 576 589 583 ...
##  $ 1.0389999999999999: num  591 596 584 597 588 ...
##  $ 1.04              : num  601 605 592 605 591 ...
##  $ 1.0409999999999999: num  607 611 597 611 595 ...
##  $ 1.042             : num  612 615 600 615 604 ...
##  $ 1.0429999999999999: num  620 621 605 622 612 ...
##  $ 1.044             : num  631 630 613 632 623 ...
##  $ 1.0449999999999999: num  670 665 643 668 640 ...
##  $ 1.046             : num  703 694 668 698 664 ...
##  $ 1.0469999999999999: num  749 736 704 741 744 ...
##  $ 1.048             : num  811 793 753 800 808 ...
##  $ 1.0489999999999999: num  894 870 820 879 892 ...
##  $ 1.05              : num  1137 1097 1021 1113 999 ...
##  $ 1.0509999999999999: num  1300 1252 1161 1272 1133 ...
##  $ 1.052             : num  1493 1436 1328 1460 1293 ...
##  $ 1.0529999999999999: num  1712 1648 1522 1675 1689 ...
##  $ 1.054             : num  1952 1883 1739 1913 1916 ...
##  $ 1.0549999999999999: num  2206 2134 1974 2165 2153 ...
##  $ 1.056             : num  2717 2648 2463 2676 2390 ...
##  $ 1.0569999999999999: num  2952 2889 2697 2913 2617 ...
##  $ 1.0580000000000001: num  3158 3105 2909 3123 2998 ...
##  $ 1.0589999999999999: num  3325 3286 3091 3295 3136 ...
##  $ 1.06              : num  3448 3425 3234 3424 3229 ...
##  $ 1.0609999999999999: num  3523 3558 3397 3520 3281 ...
##  $ 1.0620000000000001: num  3460 3513 3366 3463 3243 ...
##  $ 1.0629999999999999: num  3363 3433 3299 3371 3071 ...
##  $ 1.0640000000000001: num  3241 3325 3205 3253 2952 ...
##  $ 1.0649999999999999: num  3102 3198 3090 3117 2819 ...
##  $ 1.0660000000000001: num  2803 2911 2823 2817 2681 ...
##  $ 1.0669999999999999: num  2652 2761 2682 2664 2539 ...
##  $ 1.0680000000000001: num  2503 2610 2538 2512 2258 ...
##  $ 1.069             : num  2357 2459 2393 2361 2117 ...
##  $ 1.07              : num  2211 2306 2247 2211 1977 ...
##  $ 1.071             : num  1919 1996 1947 1910 1835 ...
##  $ 1.0720000000000001: num  1770 1837 1794 1758 1692 ...
##  $ 1.073             : num  1620 1676 1639 1605 1547 ...
##  $ 1.0740000000000001: num  1470 1516 1483 1452 1260 ...
##  $ 1.075             : num  1322 1359 1331 1303 1124 ...
##  $ 1.0760000000000001: num  1180 1209 1185 1161 996 ...
##  $ 1.077             : num  927 942 924 908 880 ...
##  $ 1.0780000000000001: num  820 832 815 803 778 ...
##  $ 1.079             : num  730 738 723 715 622 ...
##  $ 1.08              : num  657 663 648 644 567 ...
##  $ 1.081             : num  528 530 516 521 499 ...
##  $ 1.0820000000000001: num  509 511 497 503 481 ...
##  $ 1.083             : num  498 500 486 493 471 ...
##  $ 1.0840000000000001: num  493 495 480 489 465 ...
##  $ 1.085             : num  491 493 478 487 465 ...
##  $ 1.0860000000000001: num  492 493 479 488 467 ...
##  $ 1.087             : num  493 495 481 490 468 ...
##  $ 1.0880000000000001: num  493 496 481 490 468 ...
##  $ 1.089             : num  492 495 481 489 466 ...
##  $ 1.0900000000000001: num  491 494 479 487 464 ...
##  $ 1.091             : num  489 492 478 485 461 ...
##  $ 1.0920000000000001: num  483 488 472 478 458 ...
##  $ 1.093             : num  480 485 469 475 454 ...
##  $ 1.0940000000000001: num  477 482 466 471 450 ...
##  $ 1.095             : num  475 480 463 468 443 ...
##  $ 1.0960000000000001: num  472 477 460 465 439 ...
##  $ 1.097             : num  469 475 457 462 436 ...
##  $ 1.0980000000000001: num  463 469 452 457 434 ...
##  $ 1.099             : num  460 466 449 454 431 ...
##  $ 1.1000000000000001: num  457 463 446 451 426 ...
##  $ 1.101             : num  449 455 440 444 420 ...
##  $ 1.1020000000000001: num  445 452 437 441 418 ...
##  $ 1.103             : num  438 444 431 434 415 ...
##  $ 1.1040000000000001: num  435 441 429 431 413 ...
##  $ 1.105             : num  432 438 426 429 409 ...
##  $ 1.1060000000000001: num  429 435 425 427 408 ...
##  $ 1.107             : num  428 433 423 426 407 ...
##  $ 1.1080000000000001: num  425 430 423 424 407 ...
##  $ 1.109             : num  424 428 423 424 408 ...
##  $ 1.1100000000000001: num  423 427 423 424 409 ...
##  $ 1.111             : num  422 425 423 423 410 ...
##  $ 1.1120000000000001: num  420 423 423 422 409 ...
##  $ 1.113             : num  418 421 422 420 409 ...
##  $ 1.1140000000000001: num  412 414 417 414 407 ...
##  $ 1.115             : num  407 410 414 409 404 ...
##  $ 1.1160000000000001: num  402 404 409 404 394 ...
##  $ 1.117             : num  395 398 402 397 387 ...
##  $ 1.1180000000000001: num  388 391 395 390 380 ...
##  $ 1.119             : num  373 376 380 374 372 ...
##  $ 1.1200000000000001: num  366 368 372 366 364 ...
##  $ 1.121             : num  352 355 357 353 343 ...
##  $ 1.1220000000000001: num  347 349 351 347 338 ...
##  $ 1.123             : num  343 345 346 343 333 ...
##  $ 1.1240000000000001: num  339 340 339 338 330 ...
##  $ 1.125             : num  339 339 338 337 329 ...
##  $ 1.1259999999999999: num  339 339 338 337 328 ...
##  $ 1.127             : num  341 340 338 338 329 ...
##  $ 1.1279999999999999: num  343 341 340 340 331 ...
##   [list output truncated]
# === STEP 4: Separate Group column ===
# Ensure last column is group, adjust if it's in a different position
ftir$Group <- as.factor(ftir$Group)  # Convert to factor
group_labels <- ftir$Group           # Save group separately
ftir_data <- ftir[, !colnames(ftir) %in% "Group"]  # Remove Group from numeric data

# === STEP 5: Run PCA ===
pca_ftir <- prcomp(ftir_data, scale. = TRUE)
scores_pca_ftir <- as.data.frame(predict(pca_ftir)[, 1:4])  # First 4 PCs
scores_pca_ftir$Group <- group_labels  # Add group back


# === STEP 7: 3D PCA Plot ===
plot_ly(data = scores_pca_ftir,
        x = ~PC1,
        y = ~PC2,
        z = ~PC3,
        color = ~Group,
        text = rownames(scores_pca_ftir),
        marker = list(size = 6)) %>%
  layout(scene = list(
    xaxis = list(title = paste0("PC1 (", round(summary(pca_ftir)$importance[2, 1] * 100, 1), "%)")),
    yaxis = list(title = paste0("PC2 (", round(summary(pca_ftir)$importance[2, 2] * 100, 1), "%)")),
    zaxis = list(title = paste0("PC3 (", round(summary(pca_ftir)$importance[2, 3] * 100, 1), "%)"))
  ),
  title = "3D PCA Plot")
## No trace type specified:
##   Based on info supplied, a 'scatter3d' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter3d
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode