Output of adaptive.density function in spatstat - r

I'm reading the book "Spatial Point Patterns: Methodology and Applications with R", Chapter 6, trying to replicate all the examples following the code at the companion website. I cannot replicate Figure 6.15 (a) since this is the output I get and it's way different from the Figure in the book.
library(spatstat)
#> Carico il pacchetto richiesto: spatstat.data
#> Carico il pacchetto richiesto: nlme
#> Carico il pacchetto richiesto: rpart
#>
#> spatstat 1.60-1 (nickname: 'Swinging Sixties')
#> For an introduction to spatstat, type 'beginner'
swp <- rescale(swedishpines)
aden <- adaptive.density(swp, f=0.1, nrep=30)
#> Computing 30 intensity estimates...
#>
#> PLEASE NOTE: The components "delsgs" and "summary" of the
#> object returned by deldir() are now DATA FRAMES rather than
#> matrices (as they were prior to release 0.0-18).
#> See help("deldir").
#>
#> PLEASE NOTE: The process that deldir() uses for determining
#> duplicated points has changed from that used in version
#> 0.0-9 of this package (and previously). See help("deldir").
#> 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, 26, 27, 28, 29, 30.
#> Done.
rainsat <- function(n) {
grade <- sqrt(seq(0.1, 1, length=n))
rainbow(n=n, start=1/2, s=grade)
}
par(mar = c(1, 0, 0, 2))
plot(aden, main="", ribscale=1000, col=rainsat)
plot(swp, add=TRUE, pch=3)
Created on 2019-09-06 by the reprex package (v0.3.0)
What's the problem here? What am I doing wrong? Even if I run all the code in the startup.R and figurelayout.R files (which should just change the cols of the plots making the b/w) I still cannot get the same plot.

adaptive.density involves randomisation. You will not get the same result if you repeat the same command twice (unless you reset random.seed).
A larger value of nrep will reduce the random variation.

Related

Why does the 'digits' argument in R's print change a value?

Why does the function return a value t = 13.214, but print(..., digits = 3) returns t = 10?
vals <- data.frame(a = c(4, 2, 4, 7, 3, 4, 8, 8, 3, 0, 1, 5, 4, 6, 4, 8, 7, 9, 6, 6, 3, 6, 7, 4),
b = c(5, 7, 6, 13, 12, 6, 14, 16, 4, 2, 7, 7, 4, 8, 9, 9, 11, 13, 12, 8, 3, 8, 7, 7))
stats::t.test(x = vals)
# One Sample t-test
# data: vals
# t = 13.214, df = 47, p-value < 2.2e-16
# alternative hypothesis: true mean is not equal to 0
# 95 percent confidence interval:
# 5.598761 7.609572
# sample estimates:
# mean of x
# 6.604167
print(stats::t.test(x = vals), digits = 3)
Form ?print:
digits: minimal number of significant digits, see print.default.
But that should not change 10 to 13?
package ‘stats’ version 3.5.1
R.version
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 5.1
year 2018
month 07
day 02
svn rev 74947
language R
version.string R version 3.5.1 (2018-07-02)
nickname Feather Spray
The first step in answering these questions is always to figure out which print method we're dealing with. The generic help in ?print won't necessarily be terribly relevant. t.test objects have class htest, so we want to look at print.htest.
Note that ?print.htest sends you to a slightly more specific documentation page. The documentation for digits doesn't say anything specific, but then in the Details section we see:
Both print methods traditionally have not obeyed the digits argument
properly. They now do, the htest method mostly in expressions like
max(1, digits - 2).
(This is in R 3.5.2)
For example, in the function code we see things like:
out <- c(out, paste(names(x$statistic), "=", format(signif(x$statistic,
max(1L, digits - 2L)))))
The default value for digits will typically be 7. It uses digits for printing the sample estimates and confidence intervals, but fewer digits for other quantities.

How to do a "full" union with the R package sf

I try to do a union between three polygons using sf::st_union. In the figure below showing the result from ArcGIS "Overlay, Union, All" I wish to obtain a similar result as the five different polygons in 'OUTPUT' by using the sf package in R.
library(sf)
a1 <- st_polygon(list(rbind(c(0, 10), c(45, 10), c(45, 90), c(0, 90), c(0, 10))))
a2 <- st_polygon(list(rbind(c(45, 10), c(90,10), c(90, 90), c(45, 90), c(45, 10))))
b <- st_polygon(list(rbind(c(15, 5), c(75, 5), c(75, 50), c(15, 50), c(15, 5))))
a <- st_sf(c(st_sfc(a1), st_sfc(a2)))
b <- st_sf(st_sfc(b))
a$station <- c(1, 2)
b$type <- "A"
ab_union <- st_union(a, b)
In this simple example the resulting sf object 'ab_union' will only contain two polygons, not the expected five. Can I get the wanted result with five objects as in the figure above by using functions in the sf package?
I didn't find a function that make everything in one step, but this is a way to resolve your problem:
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(tidyverse)
a1 <- st_polygon(list(rbind(c(0, 10), c(45, 10), c(45, 90), c(0, 90), c(0, 10))))
a2 <- st_polygon(list(rbind(c(45, 10), c(90,10), c(90, 90), c(45, 90), c(45, 10))))
b1 <- st_polygon(list(rbind(c(15, 5), c(75, 5), c(75, 50), c(15, 50), c(15, 5))))
a <- st_sf(station=c(1, 2), geometry=st_sfc(a1, a2))
b <- st_sf(type="A", geometry=st_sfc(b1))
st_agr(a) = "constant" #to avoid warnings, but see https://github.com/r-spatial/sf/issues/406
st_agr(b) = "constant"
#Operations
plot(st_geometry(st_union(a,b)))
op1 <- st_difference(a,st_union(b)) #notice the use of st_union()
plot(st_geometry(op1), border="red", add=TRUE)
op2 <- st_difference(b, st_union(a)) #notice the order of b and a and st_union()
plot(st_geometry(op2), border="green", add=TRUE)
op3 <- st_intersection(b, a) #notice the order of b and a
plot(st_geometry(op3), border="blue", add=TRUE)
union <- rbind(op1, op2, op3) #Error because op1 (op2) doesn't have the column "type" ("station")
#> Error in match.names(clabs, names(xi)): names do not match previous names
op11 <- dplyr::mutate(op1, type=NA)
op22 <- dplyr::mutate(op2, station=NA)
union <- rbind(op11, op22, op3)
(as.data.frame(union)) #The row names must be ordered.
#> station type geometry
#> 1 1 <NA> POLYGON ((15 10, 0 10, 0 90...
#> 2 2 <NA> POLYGON ((45 50, 45 90, 90 ...
#> 3 NA A POLYGON ((75 10, 75 5, 15 5...
#> 11 1 A POLYGON ((15 10, 15 50, 45 ...
#> 1.1 2 A POLYGON ((45 50, 75 50, 75 ...
plot(union)
#Other approach for avoid create the new columns would be:
union2 <- dplyr::bind_rows(op1, op2, op3) #But see discusion here: https://github.com/r-spatial/sf/issues/49
#> Warning in bind_rows_(x, .id): Vectorizing 'sfc_POLYGON' elements may not
#> preserve their attributes
#> Warning in bind_rows_(x, .id): Vectorizing 'sfc_POLYGON' elements may not
#> preserve their attributes
#> Warning in bind_rows_(x, .id): Vectorizing 'sfc_POLYGON' elements may not
#> preserve their attributes
Created on 2019-04-06 by the reprex package (v0.2.1)
The discussions I refer:
https://github.com/r-spatial/sf/issues/406
https://github.com/r-spatial/sf/issues/49
I also needed such a function, so here's my version for arbitrary sf objects, avoiding to explicitly add missing columns. It needs the 'plyr' library in addition (function rbind.fill):
my_union <- function(a,b) {
#
# function doing a real GIS union operation such as in QGIS or ArcGIS
#
# a - the first sf
# b - the second sf
#
st_agr(a) = "constant"
st_agr(b) = "constant"
op1 <- st_difference(a,st_union(b))
op2 <- st_difference(b, st_union(a))
op3 <- st_intersection(b, a)
union <- rbind.fill(op1, op2, op3)
return(st_as_sf(union))
}

Problems with ks.test and ties

I have a distribution, for example:
d
#[1] 4 22 15 5 9 5 11 15 21 14 14 23 6 9 17 2 7 10 4
Or, the vector d in dput format.
d <- c(4, 22, 15, 5, 9, 5, 11, 15, 21, 14, 14, 23, 6, 9, 17, 2, 7, 10, 4)
And when I apply the ks.test,:
gamma <- ks.test(d, "pgamma", shape = 3.178882, scale = 3.526563)
This gives the following warning:
Warning message:
In ks.test(d, "pgamma", shape = 3.178882, scale = 3.526563) :
ties should not be present for the Kolmogorov-Smirnov test
I tried put unique(d), but obvious my data reduce the values and I wouldn't like this happen.
And the others manners and examples online, this example happen too, but the difference is the test show some results with the warning message, not only the message without values of ks.test.
Some help?
In gamma you can find your result, warning message is not blocking
d <- c(4, 22, 15, 5, 9, 5, 11, 15, 21, 14, 14, 23, 6, 9, 17, 2, 7, 10, 4)
gamma <- ks.test(d, "pgamma", shape = 3.178882, scale = 3.526563)
Warning message: In ks.test(d, "pgamma", shape = 3.178882, scale =
3.526563) : ties should not be present for the Kolmogorov-Smirnov test
gamma
One-sample Kolmogorov-Smirnov test
data: d
D = 0.14549, p-value = 0.816
alternative hypothesis: two-sided
You find an explanation of the warning in the help page ??ks.test
The presence of ties always generates a warning, since continuous
distributions do not generate them. If the ties arose from rounding
the tests may be approximately valid, but even modest amounts of
rounding can have a significant effect on the calculated statistic.
As you can see some rounding is applied and the test is "approximately" valid.

Plot variables as slope of line between points

Due to the nature of my specification, the results of my regression coefficients provide the slope (change in yield) between two points; therefore, I would like to plot these coefficients using the slope of a line between these two points with the first point (0, -0.7620) as the intercept. Please note this is a programming question; not a statistics question.
I'm not entirely sure how to implement this in base graphics or ggplot and would appreciate any help. Here is some sample data.
Sample Data:
df <- data.frame(x = c(0, 5, 8, 10, 12, 15, 20, 25, 29), y = c(-0.762,-0.000434, 0.00158, 0.0000822, -0.00294, 0.00246, -0.000521, -0.00009287, -0.01035) )
Output:
x y
1 0 -7.620e-01
2 5 -4.340e-04
3 8 1.580e-03
4 10 8.220e-05
5 12 -2.940e-03
6 15 2.460e-03
7 20 -5.210e-04
8 25 -9.287e-05
9 29 -1.035e-02
Example:
You can use cumsum, the cumulative sum, to calculate intermediate values
df <- data.frame(x=c(0, 5, 8, 10, 12, 15, 20, 25, 29),y=cumsum(c(-0.762,-0.000434, 0.00158, 0.0000822, -0.00294, 0.00246, -0.000521, -0.00009287, -0.0103)))
plot(df$x,df$y)

How to calculate a mean value from multiple maximal values

I have a variable e.g. c(0, 8, 7, 15, 85, 12, 46, 12, 10, 15, 15)
how can I calculate a mean value out of random maximal values in R?
for example, I would like to calculate a mean value with three maximal values?
First step: You draw a sample of 3 from your data and store it in x
Second step: You calculate the mean of the sample
try
dat <- c(0,8,7,15, 85, 12, 46, 12, 10, 15,15)
x <- sample(dat,3)
x
mean(x)
possible output:
> x <- sample(dat,3)
> x
[1] 85 15 0
> mean(x)
[1] 33.33333
If you mean the three highest values, just sort your vector and subset:
> mean(sort(c(0,8,7,15, 85, 12, 46, 12, 10, 15,15), decreasing=T)[1:3])
[1] 48.66667

Resources