An error on Ripley's K function on spatstat - r

I would like some help with the Kest function on spatstat. I'm trying to calculate de Ripley's K for my simulate data. When I try to run the function the R show me some error, that says:
Error: 7 segments do not lie entirely inside the window.
But when I plot the map, all the points shows perfectly inside the observation window.
I am using the following codes:
library(spatstat)
library(sp)
library(maptools)
pemt.area <- readShapePoly("Fragment/Fragment_SIRGAS.shp")
pemt.area <- as(pemt.area, "owin")
plot(pemt.area)
tree.simu <- read.table("simulation.txt", h=T)
tree.simu
tree.simu.ppp<- ppp(x=tree.simu$X,y=tree.simu$Y,window=pemt.area)
plot(tree.simu.ppp)
ripley <- Kest(tree.simu.ppp, correction="isotropic", domain=pemt.area, var.approx=TRUE, ratio=TRUE)
plot(ripley, main = "K function")
I tryed to look up on the vignette but I had no success on fixing this problem.
The following link contains the data files. If you need something else, please let me know: https://drive.google.com/folderview?id=0B_cWshGkkAzjbHBnemNuMzh1Q1k&usp=sharing
Thank you, I really appreciate your help!

This is not related to Kest or ppp.
Notice that the error message is talking about segments, not points.
This is an error message from the spatstat function psp which creates a spatial pattern of line segments. The error indicates that the polygon data are not conforming to the requirements. It is most likely to be happening during the call to readShapePoly or as.owin. To track it down, you could type traceback() immediately after the error report, and this will show where the error is occurring.
First it would be wise to update all your packages.

I cannot reproduce your problem. Here is the K graph I generated.
In my case: OSX, R 3.1.0 . All I can suggest is to make sure your packages are up to date and to double-check that the files you're using are identical to the ones in your link.

Related

How to block bootstrap in R?

I'm trying to run a block bootstrapping function on some time series data (monthly interest rates for ~15 years).
My data is in a csv file with no header, all comprising one column and going down by row.
I installed the package bootstrap because tsboot wouldn't work for me.
Here is my code:
testFile = read.csv("\\Users\\unori/sample_data.csv")
theta <- function(x){mean(x)}
results = bootstrap(testFile,100,theta)
It tells me there are at least 50 errors. All of them say "In mean.default(x) : argument is not numeric or logical: returning NA"
What to do? It runs when I use the example in the documentation. I think it must be how my data is stored/imported?
Thanks in advance.
Try to supply a working, minimal example that reproduces your problem! Check here to see how to make a minimal reproducible example.
The error messages tells you that the thing you want to calculate the mean of, is not a number! So R will just return NA.
Suggestions for debugging:
Does the object 'testFile' exist?
What is the output of
str(testFile)
This works for me:
library(bootstrap)
testFile <- cars[,1]
theta <- function(x){mean(x)}
results = bootstrap(testFile,100,theta)

Extract function error message: fun not found

I'm still quite new to R, so I am still learning a lot everyday. I made a list of raster files which I had cropped using the extent of a polygon. The polygon is divided in zones and from each zone I would like to know the mean value from each raster in the list. The extract function seemed the best function to do this, however when running it, it shows an error message. The code I've come up with so far is below.
> z_shapecropRastKE.list <- list()
> for (i in 1:length(shapecropRastKE.list)){
+ z_shapecropRastKE.list[[i]] <- extract(shapecropRastKE.list[[i]], shapeLVZ.list[[1]], fun="SUM", df=TRUE)
+ }
Error in fun(res[[i]], na.rm = na.rm) : could not find function "fun"
The shapeLVZ.list is just a list of two polygons corresponding to the two areas of interest. The first one is needed for this calculation.
I've already checked the needed packages and my R version is also the newest. If you want me to provide you guys with more background or more code just let me know, this is my first time posting something :)
Thanks in advance!
try fun= sum
without the double quotations

plotfit.fd is upset about my names. Cannot plot and cannot understand error

Shot in the dark but I cannot get a plot function to work in the CollocInfer package. I am trying to execute the following code:
plotfit.fd(temps, times, DEfd1,nfine=N,
xlab="Time (hours)", ylab="Temperature level (metres)",title="")
I have my code working perfectly up until this point. When I execute I get the following error:
Error in checkDim3(x, y, xdim[id], ydim[id], dNi, subset, xName = xNmi, :
Can NOT subset subscript 1 of DEfd1$coef because some dimnames(subscript 1 of temps)[[xdim=2]] are not found in dimnames(y)[[ydim=2]]; the first one is temps
I've been trying to track down what this means for hours. I ran some pacjkage supplied code and tried to figure it out and couldn't. The documentation for the checkDim3 function is here: http://www.psych.mcgill.ca/misc/fda/downloads/FDAfuns/R/R/checkDims3.R
I looked at the data and the values are consistent, i.e. I solved the ODE I am trying to model and the parameter estimates are correct. Somewhere I made an error in the bookkeeping? Any help appreciated. Or a workaround.
Ha, lesson learned:
dimnames(temps) <- NULL
works. This particular plotting function has tests for the dimnames of inputs.

Printing in R - PCA rotation components

I did a PCA in R and I am trying to print the rotation components. I was pretty much trying to understand a snippet I found online and I would really appreciate if somebody could help me with it. Please see below the snippet I found online:
require(stats)
prcomp(top2, scale=TRUE)
summary(prcomp(top2, scale=TRUE))
for (i in 1:15) {
top4[[i]] <- sort(survey.prcomp$rotation[,i], decreasing=TRUE)[1:4]}
top4
I am trying to print top 15 principal components and I get the "top4 object not found error". I am pretty new to R and would appreciate it if somebody could please explain it.
The snippet can be found at https://www.casact.org/pubs/forum/10spforum/Francis_Flynn.pdf
Thanks a lot!
The snippet you found does not work because there is no declared "survey.prcomp" object. "top4" is missing as well. I assume the authors missed this line:
survey.prcomp <- prcomp(top2, scale=TRUE)
And also this one:
top4 <- list()
Then, if your aim is to get first 15 rotation vectors, you can do so with survey.prcomp$rotation[,1:15]
The snippet you pasted does something different. It returns, for each of 15 main principal components, top 4 variables that have the most influence on the loadings (rotations).
In the snippet you pasted a series of variables are accessed but never assigned hence the error.
top2, survey.prcomp and top4 are never assigned, in the document you have attached the author seem have omitted those lines.

Issues with eigen.analysis in popbio

I've been trying to calculate demographic rates using the 'popbio' package in R. I was trying to get the sensitivity of the eigenvalues but keep getting the error "Error in Mod(z$values) : non-numeric argument to function". So, I tried to run the example from the 'popbio' package and got the same error. From the example in the package manual:
A<-matrix(c(0,0,2,.3,0,0,0,.6,0), nrow=3,byrow=TRUE) #matrix from example
ev <- eigen.analysis(A) # calculation of eigenvalues
Error in Mod(z$values) : non-numeric argument to function #error I get for example and my data
I know that the function needs a matrix. Since object "A" is a matrix, I'm confused on why I'm getting the error. Any help on why I'm getting this error would be greatly appreciated! I'm pretty new to using R for this stuff, so apologies if this is the wrong place for this question.
Cheers,
Kevin
If you run into problems with a function in an add-on package, I would run through each line of code and then ask a more general question. For example,
Type eigen.analysis and hit return to see the code below (and line 4 is the only call to Modulus used for damping ratio)
ev <- eigen(A)
lmax <- which.max(Re(ev$values))
lambda <- Re(ev$values[lmax])
dr <- rle(round(Mod(ev$values), 5))$values
...
If you get an error running
A <- matrix(c(0,0,2,.3,0,0,0,.6,0), nrow=3,byrow=TRUE)
ev <- eigen(A)
Mod(ev$values)
then ask that question on Stack Overflow instead.
Just a guess, but I'm not sure why you see Error in Mod(z$values) instead of Mod(ev$values) unless you have some updated eigen.analysis script.

Resources