Instalación de paqueterÃa
if(!require(pacman))
install.packages("pacman")
## Loading required package: pacman
Llamar a pacman
library("pacman")
Llamar paqueteria necesaria
p_load("vroom",
"dplyr",
"ggplot2",
"ggrepel",
"tidyverse",
"scales")
Llamar a la base de datos
Datos_PCR <- vroom("https://raw.githubusercontent.com/ManuelLaraMVZ/Metabolomica_2026_1/refs/heads/main/Cts")
## Rows: 20 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: 20 × 5
## Well Grupo Practica Fluor Cq
## <chr> <chr> <chr> <chr> <dbl>
## 1 50 Curva Absoluta SYBR 5.15
## 2 10 Curva Absoluta SYBR 10.8
## 3 5 Curva Absoluta SYBR 13.9
## 4 1 Curva Absoluta SYBR 15.7
## 5 0.5 Curva Absoluta SYBR 16.5
## 6 0.1 Curva Absoluta SYBR 18.3
## 7 Muestra G1 Absoluta SYBR 11.5
## 8 Muestra G2 Absoluta SYBR 13.1
## 9 A01 G1 Relativa SYBR 28.6
## 10 A02 G1 Relativa SYBR 15.0
## 11 A03 G1 Relativa SYBR 14.0
## 12 A04 G1 Relativa SYBR 50
## 13 A05 G1 Relativa SYBR 15.5
## 14 A06 G1 Relativa SYBR 12.6
## 15 A01 G2 Relativa SYBR 12.7
## 16 A02 G2 Relativa SYBR 16.3
## 17 A03 G2 Relativa SYBR 13.1
## 18 Referencia Referencia Relativa SYBR 18.4
## 19 Profesor G1 Relativa SYBR 20.0
## 20 Profesor G2 Relativa SYBR 20.0
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: 6 × 3
## Curva LogConc Cq
## <dbl> <dbl> <dbl>
## 1 50 1.70 5.15
## 2 10 1 10.8
## 3 5 0.699 13.9
## 4 1 0 15.7
## 5 0.5 -0.301 16.5
## 6 0.1 -1 18.3
Gráfica de datos
Grafica <- ggplot(Datos_curva,
aes(x = LogConc,
y = Cq))+
geom_point(color = "#900C3F", size =4)+
theme_classic()
Grafica
Realizar el ajuste lineal
modelo_lineal <- lm (data = Datos_curva, Cq ~ LogConc)
modelo_lineal
##
## Call:
## lm(formula = Cq ~ LogConc, data = Datos_curva)
##
## Coefficients:
## (Intercept) LogConc
## 14.991 -4.655
intersección= 14.991 pendiente= -4.655
Obtención de los coeficientes
coeficientes <- coef(modelo_lineal)
coeficientes
## (Intercept) LogConc
## 14.990632 -4.654892
Pendiente
m <- round (coeficientes [2], 2)
m
## LogConc
## -4.65
Intersección
y0 <- round (coeficientes [1], 2)
y0
## (Intercept)
## 14.99
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 -1.0000000 19.64552
## 2 -0.9727377 19.51862
## 3 -0.9454754 19.39172
## 4 -0.9182130 19.26481
## 5 -0.8909507 19.13791
## 6 -0.8636884 19.01101
Obtener valor de R2
resumen_modelo <- summary(modelo_lineal)
resumen_modelo
##
## Call:
## lm(formula = Cq ~ LogConc, data = Datos_curva)
##
## Residuals:
## 1 2 3 4 5 6
## -1.93651 0.43656 2.12380 0.69647 0.06971 -1.39002
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 14.9906 0.7233 20.725 3.2e-05 ***
## LogConc -4.6549 0.7579 -6.142 0.00356 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.649 on 4 degrees of freedom
## Multiple R-squared: 0.9041, Adjusted R-squared: 0.8802
## F-statistic: 37.73 on 1 and 4 DF, p-value: 0.003563
Obtener solo el valor de R2
R2 <- round(resumen_modelo$r.squared, 4)
R2
## [1] 0.9041
Queremos la 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 = -4.65 x + 14.99
## R^2 = 0.9041
Gráfica del ajuste y de la ecuación
Grafica_ajuste <- Grafica+
geom_smooth(data = prediccion,
aes(x = LogConc,
y = Cq),
color = "#FFC300",
size = 1.5)+
labs(title = "Curva estándar de RT-PCR",
subtitle = "Equipo: KAM",
caption = "Diseñó: Aitana Barrientos",
x = "Log10 (Concentración) [ng/µL]",
y = "Cycle threshold (Ct)")+
theme_classic(base_size = 15)+
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 = "#581845",
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 per session.
## 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 == "G1",
Well == "Muestra") %>%
select("Well", "Cq")
Dato_muestra
## # A tibble: 1 × 2
## Well Cq
## <chr> <dbl>
## 1 Muestra 11.5
Obtener el valor de Ct
Ct_muestra <- round(Dato_muestra$Cq, 2)
Ct_muestra
## [1] 11.5
Obtener el valor de x (logConc)
LogConc_calculado <- (Ct_muestra-y0)/m
LogConc_calculado
## (Intercept)
## 0.7505376
Obtener el valor real
Valor_real <- 10^LogConc_calculado
cat("La concentración de la muestra es:", round(Valor_real, 2), "ng/µL")
## La concentración de la muestra es: 5.63 ng/µL
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) 5.63 0.75 11.5
Gráfica de la muestra
Grafica_muestra <- Grafica_ajuste+
geom_point(data = Datos_muestra,
aes(x = LogConc,
y = Cq),
color = "black",
size = 6,
shape = 20)+
geom_segment(aes(x = min(Datos_curva$LogConc), xend = LogConc_calculado,
y = Ct_muestra, yend = Ct_muestra),
linetype = "dotted",
color = "#2f2d2c",
size = 1.5)+
geom_segment(aes(x = LogConc_calculado, xend = LogConc_calculado,
y = min(Datos_curva$Cq), yend = Ct_muestra),
linetype = "dotted",
color = "#2f2d2c",
size = 1.5)+
annotate("text",
x = LogConc_calculado,
y = Ct_muestra -0.6,
label = paste0("[ Muestra ] =", round(Valor_real, 2), " ng/µL"),
color = "black",
fontface = "bold",
hjust = -0.007,
vjust = -1.7)
Grafica_muestra
## Warning: Use of `Datos_curva$LogConc` is discouraged.
## ℹ Use `LogConc` instead.
## Warning in geom_segment(aes(x = min(Datos_curva$LogConc), xend = LogConc_calculado, : All aesthetics have length 1, but the data has 6 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## Warning: Use of `Datos_curva$Cq` is discouraged.
## ℹ Use `Cq` instead.
## Warning in geom_segment(aes(x = LogConc_calculado, xend = LogConc_calculado, : All aesthetics have length 1, but the data has 6 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'