File: LSCV.density.spatial.single.R

package info (click to toggle)
r-cran-sparr 2.3-16-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 884 kB
  • sloc: makefile: 2
file content (24 lines) | stat: -rw-r--r-- 815 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
LSCV.density.spatial.single <- function(h,pp,res,edge,za){
  if(h<=0) return(NA)
  temp.dens <- density(pp,h,edge=edge,dimyx=res,positive=FALSE,diggle=FALSE)
  temp.int <- spatstat.univar::integral(temp.dens)
  temp.dens.pts <- density(pp,sigma=h,edge=edge,dimyx=res,at="points",positive=FALSE,leaveoneout=TRUE,diggle=FALSE)/temp.int
  temp.dens <- temp.dens/temp.int
  t2int <- spatstat.univar::integral(temp.dens^2)
  
  ## tiny bandwidth protector action
  if(za==-1){
    if(any(temp.dens<=0)) return(Inf)
  }
  
  if(any(temp.dens.pts<=0)){ #was: return(t2int)
    if(za==2){
      temp.dens.pts[temp.dens.pts<=0] <- min(temp.dens.pts[temp.dens.pts>0])
    } else if(za==1){
      temp.dens.pts <- posifybivden(temp.dens.pts)
    } else {
      return(Inf)
    }
  }  
  return(t2int-2*mean(temp.dens.pts))
}