I would like to run a mark correlation function on a simple 3d dataset, but it seems like you cannot assign marks with a pp3... Am I missing something or are there other means?
The function pp3 doesn’t accept a marks argument, but you can assign marks to the resulting object:
x <- runif(100)
y <- runif(100)
z <- runif(100)
X <- pp3(x, y, z, c(0, 1), c(0,1), c(0,1))
marks(X) <- rnorm(100)
This uses the method marks<-.ppx. Methods for class ppx apply to class pp3.
However, mark correlation functions are not yet implemented for pp3 objects. The help for markcorr says that the point pattern must be two-dimensional.
Related
What is the difference between a A terra SpatRaster generated from terra and a Formal class raster layer generated by raster, if both are derived from the same raster file, let's say a tif?
They are instances of different classes defined in different R packages. They are not directly related in any way. And it does not matter whether they were constructed by reading the same file or not.
Likewise, you can represent the number 10 in many different ways (by different classes).
x <- 10
y <- list(10)
z <- data.frame(d=10)
a <- matrix(10)
You can use class to find out what class an object belongs to
class(x)
#[1] "numeric"
class(y)
#[1] "list"
class(z)
#[1] "data.frame"
class(a)
#[1] "matrix" "array"
And you can often coerce an object from one class to another.
b <- as.data.frame(a)
class(a)
#[1] "matrix" "array"
class(b)
#[1] "data.frame"
But there are things you can do with a matrix that you cannot do with a data.frame and vice versa.
Note that we also have generic functions. That is, functions that are implemented for objects of different classes. So you can do
nrow(a)
#[1] 1
nrow(z)
#[1] 1
And even
nrow(x)
#NULL
You can also use nrow with a SpatRaster and RasterLayer.
The meaning of the value returned by a generic function may depend on the object type that you are using. For example, compare the use of length for two objects of different classes holding the same numbers.
length(1:3)
#[1] 3
length(data.frame(v=1:3))
#[1] 1
Finally, with these generic functions, the actual implementation of the function that is used depends on the class of the object. Therefore, with SpatRaster x the below function calls are identical (the both call the nrow method implemented in "terra".
library(terra)
x <- rast(nrow=1, ncol=1, vals=10)
terra::nrow(x)
#[1] 1
raster::nrow(x)
#[1] 1
You do not provide a motivation for your question. In your previous question you are using the "raster" and "terra" packages at the same time. That is possible but it can easily lead to confusion because they are so similar (as illustrated by that question). See the top and the bottom of the page returned by ?terra::terra for some of the major differences.
Once you start using "terra" it is best stick with that, but if you need Raster* objects, for use with a package that only supports that class, you can coerce a SpatRaster to a RasterLayer or RasterStack like this
library(raster)
r <- raster(x)
s <- stack(x)
b <- brick(r)
# or let the software determine whether you get a Layer, Stack or Brick
y <- as(x, "Raster")
Inspect what classes they belong to
class(x)
#[1] "SpatRaster"
#attr(,"package")
#[1] "terra"
class(y)
#[1] "RasterLayer"
#attr(,"package")
#[1] "raster"
And you can take a Raster object to terra like this
rr <- rast(s)
rr
#class : SpatRaster
#dimensions : 1, 1, 1 (nrow, ncol, nlyr)
#resolution : 360, 180 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +no_defs
#source(s) : memory
#name : lyr.1
#min value : 10
#max value : 10
This question is very close to what has been asked here. The answer is great if we want to generate random marks to an already existing point pattern - we draw from a multivariate normal distribution and associate with each point.
However, I need to generate marks that follows the marks given in the lansing dataset that comes with spatstat for my own point pattern. In other words, I have a point pattern without marks and I want to simulate marks with a definite pattern (for example, to illustrate the concept of segregation for my own data). How do I make such marks? I understand the number of points could be different between lansing and my data set but I am allowed to reduce the window or create more points. Thanks!
Here is another version of segregation in four different rectangular
regions.
library(spatstat)
p <- c(.6,.2,.1,.1)
prob <- rbind(p,
p[c(4,1:3)],
p[c(3:4,1:2)],
p[c(2:4,1)])
X <- unmark(spruces)
labels <- factor(LETTERS[1:4])
subwins <- quadrats(X, 2, 2)
Xsplit <- split(X, subwins)
rslt <- NULL
for(i in seq_along(Xsplit)){
Y <- Xsplit[[i]]
marks(Y) <- sample(labels, size = npoints(Y),
replace = TRUE, prob = prob[i,])
rslt <- superimpose(rslt, Y)
}
plot(rslt, main = "", cols = 1:4)
plot(subwins, add = TRUE)
Segregation refers to the fact that one species predominates in a
specific part of the observation window. An extreme example would be to
segregate completely based on e.g. the x-coordinate. This would generate strips
of points of different types:
library(spatstat)
X <- lansing
Y <- cut(X, X$x, breaks = 6, labels = LETTERS[1:6])
plot(Y, cols = 1:6)
Without knowing more details about the desired type of segregation it is
hard to suggest something more useful.
I am reading text from spatstat textbook, and trying to learn model fit using ppm.
I created a model with carteisan coordinates as the covariates. And then I wanted to see the effect of only one covariate on the model,
model1 = ppm(chicago_ppp ~ x+y)
plot(effectfun(model1, covname = "x"))
but I get the error
Error in effectfun(model1, covname = "x") : A value for the covariate “y” must be provided (as an argument to effect fun)
The same happens if I use covname "y" it asks for "x"
Can someone please show me what is my mistake. Thank you.
UPDATE: When I use only one covariate, and I use effectfun with that one covariate, there is no error. When I use two covariates and I want to check effectfun of one covariate, I get this error in the question.
To be able to calculate the estimated intensity for different values of
x you need to fix a value for y like this:
library(spatstat)
model <- ppm(cells ~ x + y)
plot(effectfun(model, covname = "x", y = 0.1))
plot(effectfun(model, covname = "x", y = 0.9))
In the theano derivatives tutorial here:
http://deeplearning.net/software/theano/tutorial/gradients.html#tutcomputinggrads
the example of Lop works without an explicit value of the W matrix in the dot product. And, in fact, the partial derivatives in this case do remove the values of the components of W so they are not needed.
But, attempting a similar thing with the Rop throws an error:
theano.gof.fg.MissingInputError: ("An input of the graph, used to compute dot(Elemwise{second,no_inplace}.0, ), was not provided and not given a value.
How is this different?
Theano will try to optimize the computation graph, but it does not always work.
In the Lop example, Theano can detect that we don't actually need that W, but when changed to the Rop it just can't.
The Lop example:
W = T.dmatrix('W')
v = T.dvector('v')
x = T.dvector('x')
y = T.dot(x, W)
VJ = T.Lop(y, W, v)
f = theano.function([v, x], VJ)
f([2, 2], [0, 1])
If I just change y = T.dot(x, W) to y = T.dot(x, W**1), Theano will fail to do the optimization and throw the same error message at me say that I did not provide enough parameters.
Actually in the Rop example, if we change the values given to W, it does not affect the result at all, because Theano failed to optimize that.
p.s. I find the Theano documents very unclear sometimes.
I am trying to avoid looping by using an documented apply function, but have not been able to find any examples to suit my purpose. I have two vectors, x which is (1 x p) and y which is (1 x q) and would like to feed the Cartesian product of their parameters into a function, here is a parsimonious example:
require(kernlab)
x = c("cranapple", "pear", "orange-aid", "mango", "kiwi",
"strawberry-kiwi", "fruit-punch", "pomegranate")
y = c("apple", "cranberry", "orange", "peach")
sk <- stringdot(type="boundrange", length = l, normalized=TRUE)
sk_map = function(x, y){return(sk(x, y))}
I realize I could use an apply function over one dimension and loop for the other, but I feel like there has to be a way to do it in one step... any ideas?
Is this what you had in mind:
sk <- stringdot(type="boundrange", length = 2, normalized=TRUE)
# Create data frame with every combination of x and y
dat = expand.grid(x=x,y=y)
# Apply sk by row
sk_map = apply(dat, 1, function(dat_row) sk(dat_row[1],dat_row[2]))
You can use the outer function for this if your function is vectorized, and you can use the Vectorize function to create a vectorized function if it is not.
outer(x,y,FUN=sk)
or
outer(x,y, FUN=Vectorize(sk))