#AUDPS mejor estimacion del daño que sin el escalonado, ya que no sobreestima el valor.

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

tiempo y daño

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 
##   3363.367

daño , tiempo y tratamiento

mi_trat_audps<-function(yt){
  salida = c()
  t = unlist(yt[,1])
  for(i in seq(2, ncol(yt))){
    salida[i] = mi_audps(t = t, y = unlist(yt[,i]))
  }
  salida=salida[-1]
  return(salida)
}

library(readxl)
df_da <- read_excel("audpss.xlsx")
df_da
## # A tibble: 10 x 4
##    tiempo  D_t1  D_t2  D_t3
##     <dbl> <dbl> <dbl> <dbl>
##  1      7  11.0 34.2   70.4
##  2     14  11.5 69.2   28.6
##  3     21  12.7 11.7   55.3
##  4     28  26.3 56.3   37.6
##  5     35  43.1 13.1   51.6
##  6     42  47.9 52.2   48.7
##  7     49  52.0 36.2   71.2
##  8     56  56.7  6.66  15.8
##  9     63  56.9 44.5   16.0
## 10     70  64.4 19.8   25.1
audps_trat = mi_trat_audps(df_da)
audps_trat
## [1] 2676.923 2407.445 2941.994

GRAFICOS

df_texto = data.frame(label = round(audps_trat,2), trat = 1:3)

library(ggplot2)

dfg = data.frame(t=rep(df_da$tiempo,3), y = c(df_da$D_t1, sort(df_da$D_t2), sort(df_da$D_t3)), trat = rep(1:3, each = nrow(df_da)))
dfg
##     t         y trat
## 1   7 10.980865    1
## 2  14 11.484420    1
## 3  21 12.729575    1
## 4  28 26.263772    1
## 5  35 43.075655    1
## 6  42 47.852565    1
## 7  49 52.032075    1
## 8  56 56.715140    1
## 9  63 56.879940    1
## 10 70 64.403516    1
## 11  7  6.661733    2
## 12 14 11.678976    2
## 13 21 13.118687    2
## 14 28 19.751884    2
## 15 35 34.203925    2
## 16 42 36.202124    2
## 17 49 44.508499    2
## 18 56 52.219764    2
## 19 63 56.337474    2
## 20 70 69.237648    2
## 21  7 15.766930    3
## 22 14 15.950041    3
## 23 21 25.101016    3
## 24 28 28.603015    3
## 25 35 37.566302    3
## 26 42 48.726920    3
## 27 49 51.585742    3
## 28 56 55.344096    3
## 29 63 70.418714    3
## 30 70 71.222114    3
ggplot(data=dfg, aes(x=t,y=y))+
  geom_line()+
  geom_text(data = df_texto, mapping = aes(x = 20, y = 60, label = label))+
  facet_wrap(~trat)

library(ggpubr)

ggdotchart(dfg, x = "y", y = "t",
           color = "trat",add.params = list(color = "lightgray", size = 2), group = "trat", dot.size = 6, label = round(dfg$y,1),  
           font.label = list(color = "white", size = 9, 
                             vjust = 0.5),
           ggtheme = theme_pubr())+
  geom_hline(yintercept = 0, linetype = 2, color = "lightgray")

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
fig1 = plot_ly(
  type = "scatter",
  x = df_da$D_t1, 
  y = df_da$tiempo, 
  mode = "markers+lines") 
fig1
## 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.
fig2 = plot_ly(
  type = "scatter",
  x = sort(df_da$D_t2), 
  y = df_da$tiempo, 
  mode = "markers+lines") 
fig2
fig3 = plot_ly(
  type = "scatter",
  x = sort(df_da$D_t3), 
  y = df_da$tiempo, 
  mode = "markers+lines") 
fig3