[R] Problem in R code

Crown Flame crown11||@me @end|ng |rom gm@||@com
Wed Nov 8 11:33:15 CET 2023


Good afternoon,
I have been working on my thesis project on the topic "Urban Heat Island
Pattern in India". To achieve the results I am applying a* two-dimensional
Gaussian fit* on an LST raster of 1 km spatial resolution but I am facing
two errors in the following code.

library(raster)
LST <- raster("D:/Celsius_Day/MOD_01.tif")
gaussian2d <- function(x, y, mu_x, mu_y, sigma_x, sigma_y, amp)
{
  exp(-((x - mu_x)^2/(2*sigma_x^2) + (y - mu_y)^2/(2*sigma_y^2)))*amp
}

#define a function for the sum of squared errors between the data and the
Gaussian
sse <- function(p)
{ mu_x <- p
mu_y <- p
sigma_x <- p
sigma_y <- p
amp <- p[5]
fitted <- gaussian2d(x, y, mu_x, mu_y, sigma_x, sigma_y, amp)
sum((z - fitted)^2)
}

#loop over 8 cities
cities <-
c("Delhi","Jaipur","Kolkata","Mumbai","Pune","Hyderabad","Bangalore","Chennai")
lon <-
c(77.219934,75.793261,88.365394,72.900361,73.875199,78.47476,77.602114,80.192181)
lat <-
c(28.589256,26.892024,22.619754,19.110629,18.50269,17.422973,12.974087,13.044415)
results <- data.frame() #A data frame to store the results
for(i in 1:8)
{
  LST_city <- extract(LST, c(lon[i],lat[i]), fun = mean, buffer = 10000,
na.rm = TRUE)   #error
}

# Fit a 2D Gaussian surface to the LST data
x <- coordinates(LST)[,1]
y <- coordinates(LST)[,2]
z <- values(LST)
mu_x0 <- mean(x)
mu_y0 <- mean(y)
sigma_x0 <- sd(x)/2
sigma_y0 <- sd(y)/2
amp0 <- max(z)
opt <- optim(c(mu_x0, mu_y0, sigma_x0, sigma_y0, amp0), sse)     #error 2

#Calculate the footprint of SUHI effect (FP) by the Gaussian surface
FP <- which(gaussian2d(x, y, opt$par, opt$par, opt$par, opt$par,
opt$par[5]) >= threshold)

#store the results for each city in the data frame
results <- rbind(results, data.frame(city=cities[i], FP=mean(FP)))

#print the results
results


The two errors are in the row which are defining the variables "LST_city"
and "opt".
The first error is:
Error in .cellValues(x, y, ...) : unused arguments (fun =
new("standardGeneric", .Data = function (x, ...) standardGeneric("mean"),
generic = "mean", package = "base", group = list(), valueClass =
character(0), signature = "x", default = new("derivedDefaultMethod", .Data
= function (x, ...) UseMethod("mean"), target = new("signature", .Data =
"ANY", names = "x", package = "methods"), defined = new("signature", .Data
= "ANY", names = "x", package = "methods"), generic = "mean"), skeleton =
(new("derivedDefaultMethod", .Data = function (x, ...) UseMethod("mean"),
target = new("signature", .Data = "ANY", names = "x", package = "methods"),
defined = new("signature", .Data = "ANY", names = "x", package =
"methods"), generic = "mean"))(x, ...)), buffer = 20000, na.rm = TRUE)


The second error is:

Error in optim(c(mu_x0, mu_y0, sigma_x0, sigma_y0, amp0), sse) :
  non-finite value supplied by optim



What could be the possible reason behind these errors and most
importantly how can I get rid of these errors?



Thank you


Regards

DD

	[[alternative HTML version deleted]]



More information about the R-help mailing list