if(!require("pacman"))
  install.packages("pacman")
## Loading required package: pacman

llamar a la paquetería

library("pacman")
p_load("vroom",
       "ggplot2",
       "dplyr",
       "ggrepel",
       "tidyverse",
       "scales")
Datos_PCR <- vroom("https://raw.githubusercontent.com/ManuelLaraMVZ/resultados_PCR_practica/refs/heads/main/Cts1.csv")
## Rows: 32 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): Well, Grupo, Practica, Fluor
## dbl (1): Cq
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Datos_PCR
## # A tibble: 32 × 5
##    Well     Grupo Practica Fluor    Cq
##    <chr>    <chr> <chr>    <chr> <dbl>
##  1 A01      G1    Relativa SYBR   30.8
##  2 B01      G1    Relativa SYBR   41  
##  3 C01      G1    Relativa SYBR   26.1
##  4 D01      G1    Relativa SYBR   41  
##  5 E01      G1    Relativa SYBR   41  
##  6 F01      G1    Relativa SYBR   22.3
##  7 Profesor G1    Relativa SYBR   30.7
##  8 Profesor G2    Relativa SYBR   30.7
##  9 B01      G2    Relativa SYBR   20.3
## 10 C01      G2    Relativa SYBR   18.7
## # ℹ 22 more rows

Filtrado de datos

Datos_curva <- Datos_PCR %>% 
  filter(Practica=="Absoluta",
         Grupo== "Curva") %>% 
  select("Well", "Cq") %>% 
  mutate(Well= as.numeric(Well),
         Curva=Well,
         LogConc= log10(Curva)) %>% 
  select(3,4, 2)
Datos_curva
## # A tibble: 7 × 3
##      Curva LogConc    Cq
##      <dbl>   <dbl> <dbl>
## 1 1000           3  12.4
## 2  100           2  15.6
## 3   10           1  18.8
## 4    1           0  22  
## 5    0.1        -1  25.2
## 6    0.01       -2  28.4
## 7    0.001      -3  34.8

Gráfica de datos

Grafica <- ggplot(Datos_curva,
                  aes(x= LogConc,
                      y=Cq))+
  geom_point(color= "#581845",size=4)+
             theme_classic()
Grafica

Realizar ajuste linear

modelo_lineal <- lm(data = Datos_curva,Cq ~ LogConc)
modelo_lineal
## 
## Call:
## lm(formula = Cq ~ LogConc, data = Datos_curva)
## 
## Coefficients:
## (Intercept)      LogConc  
##      22.457       -3.543

Obtención de los coeficientes

coeficientes <- coef(modelo_lineal)
coeficientes
## (Intercept)     LogConc 
##   22.457143   -3.542857

Pendiente

m <- round(coeficientes [2], 2)
m
## LogConc 
##   -3.54

Intersección

y0 <- round(coeficientes [1], 2)
y0
## (Intercept) 
##       22.46

Predicción modelo

prediccion <- data.frame(LogConc=seq(min(Datos_curva$LogConc),
                                     max(Datos_curva$LogConc),
                                     length.out=100))
prediccion$Cq <- predict(modelo_lineal, newdata = prediccion)
head(prediccion)
##     LogConc       Cq
## 1 -3.000000 33.08571
## 2 -2.939394 32.87100
## 3 -2.878788 32.65628
## 4 -2.818182 32.44156
## 5 -2.757576 32.22684
## 6 -2.696970 32.01212

Obtenemos el valor de R^2

resumen_modelo <- summary(modelo_lineal)
resumen_modelo
## 
## Call:
## lm(formula = Cq ~ LogConc, data = Datos_curva)
## 
## Residuals:
##       1       2       3       4       5       6       7 
##  0.5714  0.2286 -0.1143 -0.4571 -0.8000 -1.1429  1.7143 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  22.4571     0.3959   56.73 3.22e-08 ***
## LogConc      -3.5429     0.1979  -17.90 1.00e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.047 on 5 degrees of freedom
## Multiple R-squared:  0.9846, Adjusted R-squared:  0.9816 
## F-statistic: 320.3 on 1 and 5 DF,  p-value: 9.997e-06

Obtener el valor de R^2

R2 <- round(resumen_modelo$r.squared,4)
R2
## [1] 0.9846

Ecuación para publicarla en la gráfica

ecuacion_recta <- paste0("y=",m,"x+",y0,"\nR^2=",R2)
cat("y=",m,"x+",y0,"\nR^2=",R2)
## y= -3.54 x+ 22.46 
## R^2= 0.9846

Gráfica del ajuste y de la ecuación

Grafica_ajuste <- Grafica+
  geom_smooth(data = prediccion, 
              aes(x = LogConc,
                  y =Cq),
              color = "#E8daef",
                size = 1.5)+
labs(title ="Curva estándar de RT-PCR",
     subtitle = "Equipo: 2",
     caption = "Diseño: Ximena Sánchez",
     x="Log10(Concentración) [pg/uL)",
     y="Cycle threshold (CT)")+
  theme_classic(base_size = 15)+
theme(panel.grid.major = element_line(color = "grey80"),
      panel.grid.minor = element_line(color = "grey90"))+

  scale_y_continuous(labels = number_format(accuracy = 1))+
  theme(plot.title = element_text(hjust = 0.5, face = "bold"),
        axis.title.x = element_text(face = "bold"),
        axis.title.y = element_text(face = "bold"))+
  annotate("text",
           x= max(Datos_curva$LogConc)-1.5,
           y= min(Datos_curva$Cq)+13,
           label= ecuacion_recta,
           color="#17202a",
           size=5,
           fontface="bold",
           hjust=0)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Grafica_ajuste
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Predicción de la concentración de la muestra

Dato_muestra <- Datos_PCR %>% 
  filter(Practica == "Absoluta",
         Grupo == "G2",
         Well== "A03") %>% 
  select("Well","Cq")
Dato_muestra
## # A tibble: 1 × 2
##   Well     Cq
##   <chr> <dbl>
## 1 A03    15.2

Obtener el valor de Ct

Ct_muestra <- round(Dato_muestra$Cq,2)
Ct_muestra
## [1] 15.18

Obtener el valor de x (logConc)

LogConc_calculado <- (Ct_muestra-y0)/m
LogConc_calculado
## (Intercept) 
##    2.056497

Obtener el valor real

Valor_real <- 10^LogConc_calculado
cat("La concentración de la muestra es:", round(Valor_real,2), "pg/uL")
## La concentración de la muestra es: 113.89 pg/uL

Construcción de Data.frame

Datos_muestra <- data.frame(Concentracion_real =round(Valor_real,2) ,
                            LogConc = round(LogConc_calculado,2),
                            Cq= Ct_muestra)
Datos_muestra
##             Concentracion_real LogConc    Cq
## (Intercept)             113.89    2.06 15.18

Gráfica de la muestra

Grafica_muestra <- Grafica_ajuste +
  geom_point(data = Datos_muestra,
             aes(x = LogConc, y = Cq),
             color = "#FA8072", 
             size = 6,
             shape = 20) +
  
  annotate("segment",
           x = min(Datos_curva$LogConc), xend = LogConc_calculado,
           y = Ct_muestra, yend = Ct_muestra,
           linetype = "dotted",
           color = "#f06292",
           size = 1.5) +

  annotate("segment",
           x = LogConc_calculado, xend = LogConc_calculado,
           y = min(Datos_curva$Cq), yend = Ct_muestra,
           linetype = "dotted",
           color = "#f06292",
           size = 1.5) +

  annotate("text",
           x = LogConc_calculado,
           y = Ct_muestra - 0.7,
           label = paste0("[Muestra]= ", round(Valor_real, 2), " pg/uL"),
           color = "#f06292",
           fontface = "bold",
           hjust = -0.002,
           vjust = -2)

Grafica_muestra
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'