簡體   English   中英

導致對象消失的函數

[英]Functions Causing Objects to Disappear

我遇到過這種非常奇怪的情況。 基本上,我正在嘗試將累積分布函數擬合到數據的G函數。 這樣做之后,我想繪制模型和原始數據,並將其輸出為PDF。 我將允許代碼進行解釋(只需復制並粘貼):

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()

上面的代碼工作完美。

現在說到奇怪的部分。

當我將所有命令封裝在mydata <- swedishpines作為函數,並導致mydata作為該函數的輸入時,它將不再起作用。 下面的代碼像最后一段代碼一樣執行,但不會。

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)

發生以下錯誤:

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

我很困惑。 我已經為此工作了很長時間,但無法解決這個問題。 PDF已輸出,但已損壞,無法打開。 我不知道為什么new_r “消失”,但是這樣做會導致所有繪圖操作停止。 顯然, new_r是函數ModelFit局部ModelFit ,但似乎也似乎是函數的某些區域的局部變量。

任何幫助將不勝感激。

您在其中做很多隱式的事情……我建議您更明確地編寫事情。

具體來說, mydata.Gest$r <- new_r然后在繪圖公式plot(..., cbind(rs, theo) ~ r, ...)中用r替換new_r 這對我行得通。 不確定為什么它不能在函數外部而不是內部運行,而是依靠plotmydata.Gest的本地范圍之外查找mydata.Gestnew_r是有風險的。

同樣,使用=將內容分配給數據幀中的列,而不是<-

從干凈的會話中:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM