#Javier Becerra
#Problema 1 parte a
# Diez analisis repetidos de mercurio
# 23.3 22.5 21.9 21.5 19.9 21.3 21.7 23.8 22.6 24.7

x=c(23.3,22.5,21.9,21.5,19.9,21.3,21.7,23.8,22.6,24.7)
media=mean(x)
std=sd(x)
n=10

cat("media=",media,"desv.est=",std,"\n")
## media= 22.32 desv.est= 1.37663
RSD=std/media*100
cat("RSD=",RSD,"%\n")
## RSD= 6.167699 %
alfa=0.01
EE=qt(1-alfa/2,n-1)*std/sqrt(n)
cat("EE=",EE,"\n")
## EE= 1.414747
LO=media-EE
LU=media+EE
cat("[",LO,",",LU,"]\n")
## [ 20.90525 , 23.73475 ]
t.test(x,conf.level=0.99)
## 
##  One Sample t-test
## 
## data:  x
## t = 51.272, df = 9, p-value = 2.051e-12
## alternative hypothesis: true mean is not equal to 0
## 99 percent confidence interval:
##  20.90525 23.73475
## sample estimates:
## mean of x 
##     22.32
#Problema 1 parte b
# Seis analisis repetidos de mercurio
# 13.8 14.0 13.2 11.9 12.0 12.1

x=c(13.8,14.0,13.2,11.9,12.0,12.1)
media=mean(x)
std=sd(x)
n=6

cat("media=",media,"desv.est=",std,"\n")
## media= 12.83333 desv.est= 0.9521905
RSD=std/media*100
cat("RSD=",RSD,"%\n")
## RSD= 7.419666 %
alfa=0.01
EE=qt(1-alfa/2,n-1)*std/sqrt(n)
cat("EE=",EE,"\n")
## EE= 1.567415
LO=media-EE
LU=media+EE
cat("[",LO,",",LU,"]\n")
## [ 11.26592 , 14.40075 ]
t.test(x,conf.level=0.99)
## 
##  One Sample t-test
## 
## data:  x
## t = 33.013, df = 5, p-value = 4.793e-07
## alternative hypothesis: true mean is not equal to 0
## 99 percent confidence interval:
##  11.26592 14.40075
## sample estimates:
## mean of x 
##  12.83333
#Problema  2
# Siete medidas del pH de una solucion reguladora
# 5.12 5.20 5.15 5.17 5.16 5.19 5.15

x=c(5.12,5.20,5.15,5.17,5.16,5.19,5.15)
media=mean(x)
std=sd(x)
n=7

cat("media=",media,"desv.est=",std,"\n")
## media= 5.162857 desv.est= 0.02690371
# LIMITE DE CONFIANZA DEL 95%
alfa=0.05
EE=qt(1-alfa/2,n-1)*std/sqrt(n)
cat("EE 95%=",EE,"\n")
## EE 95%= 0.02488178
LO=media-EE
LU=media+EE
cat("IC 95%: [",LO,",",LU,"]\n")
## IC 95%: [ 5.137975 , 5.187739 ]
t.test(x,conf.level=0.95)
## 
##  One Sample t-test
## 
## data:  x
## t = 507.72, df = 6, p-value = 3.94e-15
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  5.137975 5.187739
## sample estimates:
## mean of x 
##  5.162857
# LIMITE DE CONFIANZA DEL 99%
alfa=0.01
EE=qt(1-alfa/2,n-1)*std/sqrt(n)
cat("EE 99%=",EE,"\n")
## EE 99%= 0.03769952
LO=media-EE
LU=media+EE
cat("IC 99%: [",LO,",",LU,"]\n")
## IC 99%: [ 5.125158 , 5.200557 ]
t.test(x,conf.level=0.99)
## 
##  One Sample t-test
## 
## data:  x
## t = 507.72, df = 6, p-value = 3.94e-15
## alternative hypothesis: true mean is not equal to 0
## 99 percent confidence interval:
##  5.125158 5.200557
## sample estimates:
## mean of x 
##  5.162857
#Problema 3
# CURVA DE CALIBRACION DEL HIERRO

# Concentracion de Fe (mg/L)
x=c(0,1,2,3,4,5,6)

# Absorbancia
y=c(0.004,0.201,0.398,0.602,0.792,0.998,1.189)

# Regresion lineal
modelo=lm(y~x)
summary(modelo)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##          1          2          3          4          5          6          7 
##  0.0001786 -0.0007857 -0.0017500  0.0042857 -0.0036786  0.0043571 -0.0026071 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.0038214  0.0023876   1.601     0.17    
## x           0.1979643  0.0006622 298.945 7.95e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.003504 on 5 degrees of freedom
## Multiple R-squared:  0.9999, Adjusted R-squared:  0.9999 
## F-statistic: 8.937e+04 on 1 and 5 DF,  p-value: 7.949e-12
# Pendiente e intercepto
b0=coef(modelo)[1]
b1=coef(modelo)[2]

cat("Intercepto=",b0,"\n")
## Intercepto= 0.003821429
cat("Pendiente=",b1,"\n")
## Pendiente= 0.1979643
# Coeficiente de determinacion
R2=summary(modelo)$r.squared
cat("R2=",R2,"\n")
## R2= 0.9999441
# Ecuacion de la recta
cat("y=",b0,"+",b1,"*x\n")
## y= 0.003821429 + 0.1979643 *x
# Grafica de calibracion
plot(x,y,
     main="Curva de calibracion del hierro",
     xlab="Concentracion de Fe (mg/L)",
     ylab="Absorbancia",
     pch=19)

abline(modelo,col="blue")

# PROBLEMA 4: CURVA DE CALIBRACION DEL HIERRO

# Datos de calibracion
x=c(0,1,2,3,4,5,6)
y=c(0.004,0.201,0.398,0.602,0.792,0.998,1.189)

n=length(x)

# a) RECTA DE REGRESION
modelo=lm(y~x)
b0=coef(modelo)[1]
b1=coef(modelo)[2]

cat("ORDENADA AL ORIGEN=",b0,"\n")
## ORDENADA AL ORIGEN= 0.003821429
cat("PENDIENTE=",b1,"\n")
## PENDIENTE= 0.1979643
cat("Ecuacion: y=",b0,"+",b1,"*x\n")
## Ecuacion: y= 0.003821429 + 0.1979643 *x
# b) DESVIACION ESTANDAR DE LOS RESIDUOS
syx=sqrt(sum(resid(modelo)^2)/(n-2))
cat("sy/x=",syx,"\n")
## sy/x= 0.003504079
# DESVIACION ESTANDAR DE LA PENDIENTE
Sxx=sum((x-mean(x))^2)
sb1=syx/sqrt(Sxx)
cat("Desv.est. pendiente=",sb1,"\n")
## Desv.est. pendiente= 0.0006622087
# DESVIACION ESTANDAR DE LA ORDENADA
sb0=syx*sqrt(1/n+mean(x)^2/Sxx)
cat("Desv.est. ordenada=",sb0,"\n")
## Desv.est. ordenada= 0.002387628
# c) INTERVALOS DE CONFIANZA AL 95%
alfa=0.05
tc=qt(1-alfa/2,n-2)

LO_b1=b1-tc*sb1
LU_b1=b1+tc*sb1
cat("IC pendiente: [",LO_b1,",",LU_b1,"]\n")
## IC pendiente: [ 0.196262 , 0.1996665 ]
LO_b0=b0-tc*sb0
LU_b0=b0+tc*sb0
cat("IC ordenada: [",LO_b0,",",LU_b0,"]\n")
## IC ordenada: [ -0.002316163 , 0.009959021 ]
# Comprobar si la ordenada difiere de cero
tcalc=b0/sb0
cat("t calculado ordenada=",tcalc,"\n")
## t calculado ordenada= 1.600513
cat("t critico=",tc,"\n")
## t critico= 2.570582
if(abs(tcalc)>tc){
  cat("La ordenada difiere significativamente de cero\n")
}else{
  cat("La ordenada NO difiere significativamente de cero\n")
}
## La ordenada NO difiere significativamente de cero
# d) COEFICIENTE DE CORRELACION Y R CUADRADO
R=cor(x,y)
R2=summary(modelo)$r.squared

cat("Coeficiente de correlacion R=",R,"\n")
## Coeficiente de correlacion R= 0.999972
cat("Coeficiente de determinacion R2=",R2,"\n")
## Coeficiente de determinacion R2= 0.9999441
# e) LIMITE DE DETECCION Y CUANTIFICACION
LOD=3*syx/b1
LOQ=10*syx/b1

cat("LOD=",LOD,"mg/L\n")
## LOD= 0.05310169 mg/L
cat("LOQ=",LOQ,"mg/L\n")
## LOQ= 0.1770056 mg/L
# f) CONCENTRACION DE HIERRO EN LA MUESTRA
absorbancia=c(0.512,0.518,0.509)
ym=mean(absorbancia)
m=length(absorbancia)

concentracion=(ym-b0)/b1
cat("Absorbancia media=",ym,"\n")
## Absorbancia media= 0.513
cat("Concentracion de Fe=",concentracion,"mg/L\n")
## Concentracion de Fe= 2.572073 mg/L
# INTERVALO DE CONFIANZA DEL 95% DE LA CONCENTRACION
sx=(syx/b1)*sqrt(1/m+1/n+(ym-mean(y))^2/(b1^2*Sxx))

LO_x=concentracion-tc*sx
LU_x=concentracion+tc*sx

cat("IC 95% concentracion: [",LO_x,",",LU_x,"] mg/L\n")
## IC 95% concentracion: [ 2.54046 , 2.603686 ] mg/L
# g) GRAFICA DE CALIBRACION
plot(x,y,
     main="Curva de calibracion del hierro",
     xlab="Concentracion de Fe (mg/L)",
     ylab="Absorbancia a 510 nm",
     pch=19)

abline(modelo,col="violet",lwd=2)

# PROBLEMA 5: PRUEBA T DE STUDENT PARA DATOS PAREADOS

# Datos de los dos metodos
DCPIP=c(42.3,35.1,51.8,28.6,46.2,39.7,33.4,55.0)
HPLC=c(41.5,34.0,50.9,28.4,44.8,38.9,33.1,53.7)

# a) Se utiliza una prueba pareada porque se analizaron
# los mismos ocho jugos con ambos metodos.

# b) Hipotesis
# H0: media de las diferencias = 0
# H1: media de las diferencias != 0

# c) Calculo de las diferencias
d=DCPIP-HPLC
media=mean(d)
std=sd(d)
n=length(d)

cat("Diferencias=",d,"\n")
## Diferencias= 0.8 1.1 0.9 0.2 1.4 0.8 0.3 1.3
cat("Media=",media,"desv.est=",std,"\n")
## Media= 0.85 desv.est= 0.4309458
# d) ESTADISTICO T CALCULADO
tcalc=media*sqrt(n)/std
cat("t calculado=",tcalc,"\n")
## t calculado= 5.578806
# Valor critico al 95%
alfa=0.05
tc95=qt(1-alfa/2,n-1)
cat("t critico 95%=",tc95,"\n")
## t critico 95%= 2.364624
# Valor critico al 99%
alfa=0.01
tc99=qt(1-alfa/2,n-1)
cat("t critico 99%=",tc99,"\n")
## t critico 99%= 3.499483
# e) INTERVALO DE CONFIANZA DEL 95%
alfa=0.05
EE=qt(1-alfa/2,n-1)*std/sqrt(n)

LO=media-EE
LU=media+EE

cat("EE=",EE,"\n")
## EE= 0.3602797
cat("IC 95%: [",LO,",",LU,"]\n")
## IC 95%: [ 0.4897203 , 1.21028 ]
# Prueba t pareada al 95%
cat("\nPRUEBA T PAREADA AL 95%\n")
## 
## PRUEBA T PAREADA AL 95%
print(t.test(DCPIP,HPLC,paired=TRUE,conf.level=0.95))
## 
##  Paired t-test
## 
## data:  DCPIP and HPLC
## t = 5.5788, df = 7, p-value = 0.0008343
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  0.4897203 1.2102797
## sample estimates:
## mean difference 
##            0.85
# Prueba t pareada al 99%
cat("\nPRUEBA T PAREADA AL 99%\n")
## 
## PRUEBA T PAREADA AL 99%
print(t.test(DCPIP,HPLC,paired=TRUE,conf.level=0.99))
## 
##  Paired t-test
## 
## data:  DCPIP and HPLC
## t = 5.5788, df = 7, p-value = 0.0008343
## alternative hypothesis: true mean difference is not equal to 0
## 99 percent confidence interval:
##  0.3168105 1.3831895
## sample estimates:
## mean difference 
##            0.85
# f) Conclusion
#Sí, existe una diferencia estadísticamente significativa entre los métodos DCPIP y HPLC, ya que el valor de \(t\) calculado (5.5788) es mayor que el valor crítico al 99 % (3.4995). Por lo tanto, se rechaza la hipótesis nula.

#Una posible causa química es la presencia de otras sustancias reductoras en los jugos que reaccionan con el DCPIP, aumentando el consumo del reactivo y causando una sobreestimación de la concentración de vitamina C. El HPLC permite separar el ácido ascórbico de otros componentes, por lo que puede ser más selectivo.