Integral

y1 = function(x){(x^2)/2} # f(x)
y2 = function(x){2*x}# g(x)
curve(y1, from = 0, to=4, ylim=c(0,12))
curve(y2, from = 0, to=4, add = T, col='red')

x=seq(0,4, l=100)
polygon(rep(x,2),c(y1(x),y2(x)), density = 10, col = 'darkcyan')
text(2,3, 'A')
text(1,4, 'f(x)', pos=3)
text(2,3, 'g(x)', pos=3)

x0 = 2.5
segments(x0,y1(x0),x0,y2(x0), col = 'red')
x0 = 2.6
segments(x0,y1(x0),x0,y2(x0), col = 'red')
text(2.55, y2(2.55), 'dx', pos =3)

\[dA = (y_2-y_1)*dx\] \[A = \int dA = \int (y_2-y_1)\ dx = \int ((2x)-(\frac{x^2}{2}))\ dx\] \[A = \int_{0}^{4} ((2x)-(\frac{x^2}{2}))\ dx = x^2-\frac{x^3}{6}|_{0}^{4} = \frac{16}{3}\]

y1 = function(x){(x^2)/2} # f(x)
y2 = function(x){2*x}# g(x)
curve(y1, from = 0, to=4, ylim=c(0,9))
curve(y2, from = 0, to=4, add = T, col='red')

abline(h = c(0,8), v = c(0,4))

n = 100000
xn = runif(n, 0, 4)
yn = runif(n, 0, 8)
points(xn,yn, pch = 16, cex = 0.1, col=gray(0.5))
curve(y1, from = 0, to=4, add=T, col='yellow')
curve(y2, from = 0, to=4, add = T, col='yellow')

men_y2 = sum(yn<=y2(seq(0,4,l=n)))
men_y1 = sum(yn<=y1(seq(0,4,l=n)))
men_y1;men_y2
## [1] 33175
## [1] 49968
# Puntos dentro de A es la diferencia
puntos_A = men_y2 - men_y1
puntos_A
## [1] 16793

\[A_32 \rightarrow 100000 Puntos\\ A_x \rightarrow 16708 Puntos\]

areas = c(Ae = puntos_A*32/n, Ar = 16/3)
areas
##       Ae       Ar 
## 5.373760 5.333333

Area bajo la curva del progreso escalonado de una enfermedad - AUDPS

library(agricolae)
dates<-c(14,21,28) # days
# example 1: evaluation - vector
evaluation<-c(40,80,90)
audps(evaluation,dates)
## evaluation 
##       1470
audps(evaluation,dates,"relative")
## evaluation 
##        0.7
x<-seq(10.5,31.5,7)
y<-c(40,80,90,90)
plot(x,y,"s",ylim=c(0,100),xlim=c(10,32),axes=FALSE,col="red" ,ylab="",xlab="")
title(cex.main=0.8,main="Absolute or Relative AUDPS\nTotal area=(31.5-10.5)*100=2100",
ylab="evaluation",xlab="dates" )
points(x,y,type="h")
z<-c(14,21,28)
points(z,y[-3],col="blue",lty=2,pch=19)
points(z,y[-3],col="blue",lty=2,pch=19)
axis(1,x,pos=0)
axis(2,c(0,40,80,90,100),las=2)
text(dates,evaluation+5,dates,col="blue")
text(14,20,"A = (17.5-10.5)*40",cex=0.8)
text(21,40,"B = (24.5-17.5)*80",cex=0.8)
text(28,60,"C = (31.5-24.5)*90",cex=0.8)
text(14,95,"audps = A+B+C = 1470")
text(14,90,"relative = audps/area = 0.7")

# It calculates audpc absolute
absolute<-audps(evaluation,dates,type="absolute")
print(absolute)
## evaluation 
##       1470
rm(evaluation, dates, absolute)
library(agricolae)
dates<-c(14,21,28) # days
# example 1: evaluation - vector
evaluation<-c(40,80,90)
audps(evaluation,dates)
## evaluation 
##       1470
audps(evaluation,dates,"relative")
## evaluation 
##        0.7
mi_audps<- function(t,y){
  audps(y,t)
}
tiempos<-seq(7,70,7)
daño<-sort(runif(n=length(tiempos), min=5,80))
mi_audps(tiempos,daño)
## evaluation 
##   3216.481
#################################################
options(digits = 6)
mi_trat_audps<- function(yt){
  salida<-c()
  t<-unlist(yt[,1])
  print(t)
  for (i in seq(2,ncol(yt))) {
    salida[i]<-mi_audps(t = t, unlist(yt[,i]))
  }
  salida=salida[-1]
  return(salida)
}

library(readxl)
Datos <- read_excel("Datos.xlsx", sheet = "Hoja1")
df_da<-Datos
df_da
## # A tibble: 10 x 4
##    tiempo  D_t1  D_t2  D_t3
##     <dbl> <dbl> <dbl> <dbl>
##  1      7  6.20  16.6  6.55
##  2     14 11.5   39.8  7.25
##  3     21 16.4   49.8 13.2 
##  4     28 18.2   51.3 46.7 
##  5     35 36.6   52.8 49.7 
##  6     42 48.8   62.4 62.5 
##  7     49 56.8   64.6 67.5 
##  8     56 63.8   66.0 68.3 
##  9     63 72.2   71.5 69.0 
## 10     70 74.4   75.7 76.5
mi_trat_audps(df_da)
##  tiempo1  tiempo2  tiempo3  tiempo4  tiempo5  tiempo6  tiempo7  tiempo8 
##        7       14       21       28       35       42       49       56 
##  tiempo9 tiempo10 
##       63       70
## [1] 2833.41 3854.11 3269.99
audps_trat<-mi_trat_audps(df_da)
##  tiempo1  tiempo2  tiempo3  tiempo4  tiempo5  tiempo6  tiempo7  tiempo8 
##        7       14       21       28       35       42       49       56 
##  tiempo9 tiempo10 
##       63       70
df_text<-data.frame(label=round(audps_trat,2), trat=1:3)
dfg<-data.frame(t= rep(df_da$tiempo,3),
                y= c(df_da$D_t1,df_da$D_t2,df_da$D_t3),
                trat= rep(1:3, each=nrow(df_da)))
library(ggplot2)
ggplot(data=dfg, aes(x=t,y=y))+
  geom_line()+
  geom_text(data = df_text,
            mapping = aes(x=20,y=60,label=label))+
  facet_wrap(~trat)

Gráfico con ggpubr

library(ggpubr)

 p <- ggboxplot(dfg, x = 'trat', y = 'y',
                color = 'trat', palette =c("#00AFBB", "#E7B800", "#FC4E07"),
                add = "jitter", main='Diagrama de caja',xlab = 'Tratamientos',
                ylab = 'Daño (%)')
 p

Gráfico con plotly

library(plotly)
## 
## 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
g <- ggplot(dfg, aes(x=t,y=y))+
  geom_line()+
  geom_text(data = df_text,
            mapping = aes(x=20,y=60,label=label))+
  facet_wrap(~trat)
ggplotly(g)
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
diass<-seq(7,70,7)
dt1<-c(dfg$y[1:10])
dt2<-c(dfg$y[11:20])
dt3<-c(dfg$y[21:30])
dfgt<-data.frame(diass,dt1,dt2,dt3)

fig <- plot_ly(dfgt, x = ~diass, y = ~dt1, name = 'Tratamiento 1', type = 'scatter', mode = 'lines') 
fig <- fig %>% add_trace(y = ~dt2, name = 'Tratamiento 2', mode = 'lines+markers') 
fig <- fig %>% add_trace(y = ~dt3, name = 'Tratamiento 3', mode = 'lines+markers')
fig <- fig %>% layout(title = "Porcentaje de daño vs Tiempo",
         xaxis = list(title = "Dias"),
         yaxis = list (title = "Daño (%)"))
fig
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.