Examples for 'classInt::classIntervals'


Choose univariate class intervals

Aliases: classIntervals print.classIntervals plot.classIntervals nPartitions classIntervals2shingle

Keywords: spatial

### ** Examples

if (!require("spData", quietly=TRUE)) {
  message("spData package needed for examples")
  run <- FALSE
} else {
  run <- TRUE
}
spData package needed for examples
if (run) {
data(jenks71, package="spData")
pal1 <- c("wheat1", "red3")
opar <- par(mfrow=c(2,3))
plot(classIntervals(jenks71$jenks71, n=5, style="fixed",
 fixedBreaks=c(15.57, 25, 50, 75, 100, 155.30)), pal=pal1, main="Fixed")
plot(classIntervals(jenks71$jenks71, n=5, style="sd"), pal=pal1, main="Pretty standard deviations")
plot(classIntervals(jenks71$jenks71, n=5, style="equal"), pal=pal1, main="Equal intervals")
plot(classIntervals(jenks71$jenks71, n=5, style="quantile"), pal=pal1, main="Quantile")
set.seed(1)
plot(classIntervals(jenks71$jenks71, n=5, style="kmeans"), pal=pal1, main="K-means")
plot(classIntervals(jenks71$jenks71, n=5, style="hclust", method="complete"),
 pal=pal1, main="Complete cluster")
}
if (run) {
plot(classIntervals(jenks71$jenks71, n=5, style="hclust", method="single"),
 pal=pal1, main="Single cluster")
set.seed(1)
plot(classIntervals(jenks71$jenks71, n=5, style="bclust", verbose=FALSE),
 pal=pal1, main="Bagged cluster")
plot(classIntervals(jenks71$jenks71, n=5, style="fisher"), pal=pal1,
 main="Fisher's method")
plot(classIntervals(jenks71$jenks71, n=5, style="jenks"), pal=pal1,
 main="Jenks' method")
 plot(classIntervals(jenks71$jenks71, style="dpih"), pal=pal1,
 main="dpih method")
 plot(classIntervals(jenks71$jenks71, style="headtails", thr = 1), pal=pal1,
 main="Head Tails method")
par(opar)
}
if (run) {
print(classIntervals(jenks71$jenks71, n=5, style="fixed",
 fixedBreaks=c(15.57, 25, 50, 75, 100, 155.30)))
}
if (run) {
print(classIntervals(jenks71$jenks71, n=5, style="sd"))
}
if (run) {
print(classIntervals(jenks71$jenks71, n=5, style="equal"))
}
if (run) {
print(classIntervals(jenks71$jenks71, n=5, style="quantile"))
}
if (run) {
set.seed(1)
print(classIntervals(jenks71$jenks71, n=5, style="kmeans"))
}
if (run) {
set.seed(1)
print(classIntervals(jenks71$jenks71, n=5, style="kmeans", intervalClosure="right"))
}
if (run) {
set.seed(1)
print(classIntervals(jenks71$jenks71, n=5, style="kmeans", dataPrecision=0))
}
if (run) {
set.seed(1)
print(classIntervals(jenks71$jenks71, n=5, style="kmeans"), cutlabels=FALSE)
}
if (run) {
print(classIntervals(jenks71$jenks71, n=5, style="hclust", method="complete"))
}
if (run) {
print(classIntervals(jenks71$jenks71, n=5, style="hclust", method="single"))
}
if (run) {
set.seed(1)
print(classIntervals(jenks71$jenks71, n=5, style="bclust", verbose=FALSE))
}
if (run) {
print(classIntervals(jenks71$jenks71, n=5, style="bclust",
 hclust.method="complete", verbose=FALSE))
}
if (run) {
print(classIntervals(jenks71$jenks71, n=5, style="fisher"))
}
if (run) {
print(classIntervals(jenks71$jenks71, n=5, style="jenks"))
}
if (run) {
print(classIntervals(jenks71$jenks71, style="dpih"))
}
if (run) {
print(classIntervals(jenks71$jenks71, style="dpih", range.x=c(0, 160)))
}
if (run) {
  print(classIntervals(jenks71$jenks71, style="headtails"))
}
if (run) {
  print(classIntervals(jenks71$jenks71, style="headtails", thr = .45))
}
x <- c(0, 0, 0, 1, 2, 50)
print(classIntervals(x, n=3, style="fisher"))
style: fisher
  one of 3 possible partitions of this variable into 3 classes
 [0,0.5) [0.5,26)  [26,50] 
       3        2        1 
print(classIntervals(x, n=3, style="jenks"))
style: jenks
  one of 3 possible partitions of this variable into 3 classes
 [0,0]  (0,2] (2,50] 
     3      2      1 
# Argument 'unique' will collapse the label of classes containing a
# single value. This is particularly useful for 'censored' variables
# that contain for example many zeros.

data_censored<-c(rep(0,10), rnorm(100, mean=20,sd=1),rep(26,10))
plot(density(data_censored))
plot of chunk example-classInt-classIntervals-1
cl2 <- classIntervals(data_censored, n=5, style="jenks", dataPrecision=2)
print(cl2, unique=FALSE)
style: jenks
  one of 4,082,925 possible partitions of this variable into 5 classes
        [0,0]     (0,18.77] (18.77,20.05]  (20.05,22.4]     (22.4,26] 
           10            15            41            44            10 
print(cl2, unique=TRUE)
style: jenks
  one of 4,082,925 possible partitions of this variable into 5 classes
Class found with one single (possibly repeated) value: changed label
            0     (0,18.77] (18.77,20.05]  (20.05,22.4]            26 
           10            15            41            44            10 
## Not run: 
##D set.seed(1)
##D n <- 1e+05
##D x <- runif(n)
##D classIntervals(x, n=5, style="sd")
##D classIntervals(x, n=5, style="pretty")
##D classIntervals(x, n=5, style="equal")
##D classIntervals(x, n=5, style="quantile")
##D # the class intervals found vary a little because of sampling
##D classIntervals(x, n=5, style="kmeans")
##D classIntervals(x, n=5, style="fisher")
##D classIntervals(x, n=5, style="fisher")
##D classIntervals(x, n=5, style="fisher")
## End(Not run)
have_units <- FALSE
if (require(units, quietly=TRUE)) have_units <- TRUE
udunits database from /usr/share/xml/udunits/udunits2.xml
if (have_units) {
set.seed(1)
x_units <- set_units(sample(seq(1, 100, 0.25), 100), km/h)
## Not run: 
##D classIntervals(x_units, n=5, style="sd")
## End(Not run)
}
if (have_units) {
classIntervals(x_units, n=5, style="pretty")
}
style: pretty
  one of 3,764,376 possible partitions of this [km/h] variable into 5 classes
  [0,20)  [20,40)  [40,60)  [60,80) [80,100] 
      18       21       19       19       23 
if (have_units) {
## Not run: 
##D classIntervals(x_units, n=5, style="equal")
## End(Not run)
}
NULL
if (have_units) {
classIntervals(x_units, n=5, style="quantile")
}
style: quantile
  one of 3,764,376 possible partitions of this [km/h] variable into 5 classes
   [4,21.95) [21.95,41.5) [41.5,64.05) [64.05,82.8)  [82.8,99.5] 
          20           20           20           20           20 
if (have_units) {
## Not run: 
##D classIntervals(x_units, n=5, style="kmeans")
## End(Not run)
}
NULL
if (have_units) {
classIntervals(x_units, n=5, style="fisher")
}
style: fisher
  one of 3,764,376 possible partitions of this [km/h] variable into 5 classes
     [4,19.375) [19.375,39.375)   [39.375,60.5)   [60.5,79.625)   [79.625,99.5] 
             18              21              19              19              23 
if (have_units) {
classIntervals(x_units, style="headtails")
}
style: headtails
  one of 99 possible partitions of this [km/h] variable into 2 classes
   [4,51.5225) [51.5225,99.5] 
            49             51 
## Not run: 
##D st <- Sys.time()
##D x_POSIXt <- sample(st+((0:500)*3600), 100)
##D fx <- st+((0:5)*3600)*100
##D classIntervals(x_POSIXt, style="fixed", fixedBreaks=fx)
##D classIntervals(x_POSIXt, n=5, style="sd")
##D classIntervals(x_POSIXt, n=5, style="pretty")
##D classIntervals(x_POSIXt, n=5, style="equal")
##D classIntervals(x_POSIXt, n=5, style="quantile")
##D classIntervals(x_POSIXt, n=5, style="kmeans")
##D classIntervals(x_POSIXt, n=5, style="fisher")
##D classIntervals(x_POSIXt, style="headtails")
## End(Not run)
# see vignette for further details
## Not run: 
##D # Head Tails method is suitable for right-sided heavy-tailed distributions
##D set.seed(1234)
##D # Heavy tails-----
##D # Pareto distributions a=7 b=14
##D paretodist <- 7 / (1 - runif(100)) ^ (1 / 14)
##D # Lognorm
##D lognormdist <- rlnorm(100)
##D # Weibull
##D weibulldist <- rweibull(100, 1, scale = 5)
##D 
##D pal1 <- c("wheat1", "red3")
##D opar <- par(mfrow = c(1, 3))
##D plot(classIntervals(paretodist, style = "headtails"),
##D      pal = pal1,
##D      main = "HeadTails: Pareto Dist.")
##D plot(classIntervals(lognormdist, style = "headtails"),
##D      pal = pal1,
##D      main = "HeadTails: LogNormal Dist.")
##D plot(classIntervals(weibulldist, style = "headtails"),
##D      pal = pal1,
##D      main = "HeadTails: Weibull Dist.")
##D plot(classIntervals(paretodist, n = 5, style = "fisher"),
##D      pal = pal1,
##D      main = "Fisher: Pareto Dist.")
##D plot(classIntervals(lognormdist, n = 7, style = "fisher"),
##D      pal = pal1,
##D      main = "Fisher: LogNormal Dist.")
##D plot(classIntervals(weibulldist, n= 4, style = "fisher"),
##D      pal = pal1,
##D      main = "Fisher: Weibull Dist.")
##D par(opar)
##D 
##D 
##D #Non heavy tails, thr should be increased-----
##D 
##D #Normal dist
##D normdist <- rnorm(100)
##D #Left-tailed truncated Normal distr
##D leftnorm <- rep(normdist[normdist < mean(normdist)], 2)
##D # Uniform distribution
##D unifdist <- runif(100)
##D opar <- par(mfrow = c(2, 3))
##D plot(classIntervals(normdist, style = "headtails"),
##D      pal = pal1,
##D      main = "Normal Dist.")
##D plot(classIntervals(leftnorm, style = "headtails"),
##D      pal = pal1,
##D      main = "Truncated Normal Dist.")
##D plot(classIntervals(unifdist, style = "headtails"),
##D      pal = pal1,
##D      main = "Uniform Dist.")
##D # thr should be increased for non heavy-tailed distributions
##D plot(
##D   classIntervals(normdist, style = "headtails", thr = .6),
##D   pal = pal1,
##D   main = "Normal Dist. thr = .6"
##D )
##D plot(
##D   classIntervals(leftnorm, style = "headtails", thr = .6),
##D   pal = pal1,
##D   main = "Truncated Normal Distribution thr = .6"
##D )
##D plot(
##D   classIntervals(unifdist, style = "headtails", thr = .6),
##D   pal = pal1,
##D   main = "Uniform Distribution thr = .6"
##D )
##D par(opar)
## End(Not run)

[Package classInt version 0.4-7 Index]