Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8327667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:11:34+00:00 2026-06-09T01:11:34+00:00

This is a very strange situation that I’ve come across. Basically, I’m trying to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:11:36+00:00Added an answer on June 9, 2026 at 1:11 am

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very strange situation. Basically I have code that uses a decryptor
A very strange situation, I've got this code that is supposed to make an
This is a very strange problem, and I just hope that I can clearly
Very strange situation. check this out : billy@prog:~/sql$ grep -R temp_exchange *.sp billy@prog:~/sql$ grep
I am running into a very strange situation with an app I have that
a noob question here. I have a very strange situation that's very puzzling to
I got a (to me, at least) very strange situation. I am trying to
I am stuck onto a very strange situation. I have a workflow that i
I have a quite strange situation. I have this very simple package: Task get
This very strange. When I resize a WinForms dialog the controls are flickering very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.