library(purrr)

e3 <- function(x)x^3
e3(6)
## [1] 216
res = map(1:3, e3)
mode(res)
## [1] "list"
vec = c(unlist(res))
mode(vec)
## [1] "numeric"
set.seed(123)
pob <- rnorm(100, 3, 0.7)
t.m <- function(n){
  mue = sample(pob, n, replace = F)
  prub.t = t.test(mue, mu = 3,
                  alternative = 'g',
                  conf.level = 0.95)$p.value
  desi = ifelse(prub.t < 0.05, "Rechaza Ho", "No rechazo Ho")
  return(desi)
}
t.m(10)
## [1] "No rechazo Ho"
muestras = map(rep(10,15), t.m)
table(unlist(muestras))
## 
## No rechazo Ho    Rechaza Ho 
##            13             2
mean(pob)
## [1] 3.063284
set.seed(123)
df.mo = data.frame(MO = rnorm(60,3,0.7),
                   prof = gl(2,30,60, labels = c("10","20")))
t.m2 <- function(n){
  por.prof = split(df.mo$MO, df.mo$prof)
  mue1 = sample(por.prof$`10`, n, replace = F)
  mue2 = sample(por.prof$`20`, n, replace = F)
  p2 = t.test(mue1,mue2, mu = 0, alternative = 't',
              paired = F, var.equal = T, conf.level = 0.95)$p.value
  desi = ifelse(p2 < 0.05, "Rechazo Ho", "No rechazo Ho")
  return(desi)
}
mues = map(rep(10,100), t.m2)
table(unlist(mues))
## 
## No rechazo Ho    Rechazo Ho 
##            97             3
set.seed(123)
pH = rnorm(50, 5.5, 0.5)
muestra.ph = sample(pH, 10)
mean(muestra.ph)
## [1] 5.392885
sd(muestra.ph)
## [1] 0.3725621
t.m3 <- function(n){
  mue = sample(pH,n)
  media = mean(mue)
  desv = sd(mue)
  return(list(media = media,
              desv = desv))
}
remues = map(rep(10,10), t.m3)
resum = matrix(unlist(remues),2)
ee = sd(resum[1,])
0.48/sqrt(10)
## [1] 0.1517893
#####
library(spatstat)
## Loading required package: spatstat.data
## Loading required package: nlme
## Loading required package: rpart
## 
## spatstat 1.59-0       (nickname: 'J'ai omis les oeufs de caille') 
## For an introduction to spatstat, type 'beginner'
X = runifpoint(20)
Y = runifpoint(10)
z = superimpose(X,Y)
plot(z, W=square(1))

plot(superimpose(Hooray = X, Boo = Y))

data("swedishpines")
X = affine(swedishpines, mat = diag(c(1/10, 1/10)))
unitname(X) = c("metre", "metres")
X
## Planar point pattern: 71 points
## window: rectangle = [0, 9.6] x [0, 10] metres
data("swedishpines")
X = rescale(swedishpines,10)
X
## Planar point pattern: 71 points
## window: rectangle = [0, 9.6] x [0, 10] metres
###
(data("nztrees"))
## [1] "nztrees"
plot(nztrees)

summary(nztrees)
## Planar point pattern:  86 points
## Average intensity 0.005916753 points per square foot
## 
## Coordinates are integers
## i.e. rounded to the nearest foot
## 
## Window: rectangle = [0, 153] x [0, 95] feet
## Window area = 14535 square feet
## Unit of length: 1 foot
class(nztrees)
## [1] "ppp"
contour(density(nztrees,10))

hist(nztrees$x, nclass=25)

hist(nztrees$y, nclass=25)

tw = owin(c(0,148), c(0,95))
plot(nztrees)

plot(density(nztrees, 3))
plot(nztrees, add = T, col='white', pch = 16)

trim.rectangle(tw,xmargin = c(0,5), ymargin = 0)
## window: rectangle = [0, 143] x [0, 95] units
library(readxl)
palma <- read_excel("C:/CARLOS/palma.xlsx")
plot(palma$linea,palma$palma,
     col = (palma$estatus+2),
     pch = 16, cex = 4)

summary(palma)
##      linea       palma         estatus   
##  Min.   :1   Min.   : 1.0   Min.   :0.0  
##  1st Qu.:1   1st Qu.: 3.0   1st Qu.:0.0  
##  Median :2   Median : 5.5   Median :1.0  
##  Mean   :2   Mean   : 5.5   Mean   :0.7  
##  3rd Qu.:3   3rd Qu.: 8.0   3rd Qu.:1.0  
##  Max.   :3   Max.   :10.0   Max.   :1.0
#prevalencia enfermas/total
prev = sum(palma$estatus==1)/ length(palma$estatus)
#intensidad palma enferma/ unidad de area
inte = (sum(palma$estatus==1) / ((9*8)*(7*10)))*10000

#convertir data.frame en patron de puntos PPP
ppp.pal = ppp(palma$linea, palma$palma,
              c(0,9), c(0, 11),
              marks = factor(palma$estatus))
plot(ppp.pal)

separ = split(palma, palma$estatus)

ppp.separ0 = ppp(separ$`0`$linea, separ$`0`$palma,
                 c(0,9), c(0,11))
ppp.separ1 = ppp(separ$`1`$linea, separ$`1`$palma,
                 c(0,9), c(0,11))
plot(ppp.separ0)

plot(ppp.separ1)

ppp.s0 = split(ppp.pal)$`0`
ppp.s1 = split(ppp.pal)$`1`
plot(density(ppp.s1))