Question

This is a very strange situation that I've come across. Basically, I'm trying to fit a cumulative distribution function to the G function of my data. After doing so, I want to plot the the model and the original data, and output this as PDF. I'll allow the code to explain (simply copy and paste):

library(spatstat)

data(swedishpines)

mydata <- swedishpines

mydata.Gest <- Gest(mydata)

Gvalues <- mydata.Gest$rs

count <- (which(Gvalues == 1))[1]

new_r <- seq(1/count, length(Gvalues)/count, by = 1/count)

GvsR_dataframe <- data.frame(G <- Gvalues, R <- new_r)

themodel <- suppressWarnings(nls(G ~ pnorm(R, mean, sd), data = GvsR_dataframe, start = list(mean=0.4, sd=0.2), trace = FALSE))

pdf(file = "ModelPlot.pdf")

plot(mydata.Gest, cbind(rs, theo) ~ new_r, lty = c(1, 2), col = c("black", "red"), xlim = c(0, max(new_r)), ylim = c(0,1), main = paste("Model-fitting for G Function \n Mean = ",as.numeric(coef(themodel)[1]),"\n Standard Deviation = ",as.numeric(coef(themodel)[2]), sep=''), ylab = "G(r)", xlab = "Distance Between Particles (r)", legend = NULL)
lines(new_r, predict(themodel), lty = 2, col = "blue")
legend("bottomright", c("CSR", "Swedish Pines", "Normal Probability \n Density Function"), lty = c(2, 4, 1, 2), col = c("red", "black", "blue"), bg = 'grey', border = 'black')

graphics.off()

The above code works perfectly.

Now for the strange part.

When I encapsulate all of the commands after mydata <- swedishpines as a function, and cause mydata to be an input to this function, it doesn't work any longer. The following code should perform just as the last segment of code did, but it does not.

library(spatstat)

data(swedishpines)

mydata <- swedishpines

ModelFit <- function(mydata) {

mydata.Gest <- Gest(mydata)

Gvalues <- mydata.Gest$rs

count <- (which(Gvalues == 1))[1]

new_r <- seq(1/count, length(Gvalues)/count, by = 1/count)

GvsR_dataframe <- data.frame(G <- Gvalues, R <- new_r)

themodel <- suppressWarnings(nls(G ~ pnorm(R, mean, sd), data = GvsR_dataframe, start = list(mean=0.4, sd=0.2), trace = FALSE))

pdf(file = "ModelPlot.pdf")

plot(mydata.Gest, cbind(rs, theo) ~ new_r, lty = c(1, 2), col = c("black", "red"), xlim = c(0, max(new_r)), ylim = c(0,1), main = paste("Model-fitting for G Function \n Mean = ",as.numeric(coef(themodel)[1]),"\n Standard Deviation = ",as.numeric(coef(themodel)[2]), sep=''), ylab = "G(r)", xlab = "Distance Between Particles (r)", legend = NULL)
lines(new_r, predict(themodel), lty = 2, col = "blue")
legend("bottomright", c("CSR", "Swedish Pines", "Normal Probability \n Density Function"), lty = c(2, 4, 1, 2), col = c("red", "black", "blue"), bg = 'grey', border = 'black')

graphics.off()

}

ModelFit(mydata)

The following error occurs:

Error in eval(expr, envir, enclos) : object 'new_r' not found

I'm VERY confused. I've been working on this for a long time, and just could not come up with a solution to this problem. The PDF is outputted, but it is corrupt, and will not open. I have no idea why new_r 'disappears', but in doing so, it causes all of the plotting operations to halt. Obviously new_r is local to the function ModelFit, but it almost seems as though it's local to certain areas in the function, as well.

Any help would be greatly appreciated.

Was it helpful?

Solution

You're doing a lot of implicit stuff in there... I would suggest writing things more explicitly.

Specifically, mydata.Gest$r <- new_r then replace new_r with r in your plot formula, plot(..., cbind(rs, theo) ~ r, ...). That works for me. Not sure why it works outside of a function and not inside, but relying on plot to look outside the local scope of mydata.Gest for new_r is risky.

Also, use = to assign things to columns in a data frame rather than <-

From a clean session:

data.frame(x<-1:10, y<- 1:10)
ls()

versus

data.frame(x=1:10, y=1:10)
ls()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top