File: LSCV.risk.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 (25 lines) | stat: -rw-r--r-- 1,314 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
25
LSCV.risk.single <- function(h,cases,controls,res,edge,hazey){
  if(h<=0) return(NA)
  
  temp.case <- density.ppp(cases,sigma=h,edge=edge,dimyx=res,positive=TRUE)
  temp.con <- density.ppp(controls,sigma=h,edge=edge,dimyx=res,positive=TRUE)
  tcase.int <- spatstat.univar::integral(temp.case)
  tcon.int <- spatstat.univar::integral(temp.con)
  temp.case <- temp.case/tcase.int
  temp.con <- temp.con/tcon.int
  
  if(any(is.infinite(as.matrix(temp.case/temp.con)))) return(NA)  ## pre-fail for infinite rr cells - both HAZE and KELDIG
  
  temp.case.pts <- density.ppp(cases,sigma=h,edge=edge,dimyx=res,at="points",leaveoneout=TRUE,positive=TRUE)/tcase.int
  temp.con.pts <- density.ppp(controls,sigma=h,edge=edge,dimyx=res,at="points",leaveoneout=TRUE,positive=TRUE)/tcon.int
  
  caseatcon <- safelookup(temp.case,controls,warn=FALSE)
  conatcase <- safelookup(temp.con,cases,warn=FALSE)

  if(any(temp.case.pts<=0)||any(temp.con.pts<=0)||any(caseatcon<=0)||any(conatcase<=0)) return(NA) ## tiny bandwidth protector
  
  if(!hazey) result <- 2*mean(log(caseatcon/temp.con.pts)/temp.con.pts) - 2*mean(log(temp.case.pts/conatcase)/temp.case.pts) - spatstat.univar::integral((log(temp.case)-log(temp.con))^2)
  else result <- mean((caseatcon/temp.con.pts)^2)-2*mean(temp.case.pts/conatcase)
  
  return(result)
}