[R] Problem in R code

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Thu Nov 9 12:59:46 CET 2023


В Wed, 8 Nov 2023 16:03:15 +0530
Crown Flame <crown11flame using gmail.com> пишет:

> for(i in 1:8)
> {
>   LST_city <- extract(LST, c(lon[i],lat[i]), fun = mean, buffer =
> 10000, na.rm = TRUE)   #error
> }

Three things you might need to change:

1. You are trying to assign the output of extract() to the same
variable LST_city in all 8 iterations of the loop. You will probably
need to make it a list and assign to the list elements, e.g.
LST_city[[i]] <- ...

It will also help to learn about lapply() and other functions that
encapsulate loops, although that is more of a matter of taste.

2. You are giving a single vector c(lon[i], lat[i]) to extract() as the
y=... argument. According to help(extract), this is interpreted as
_numbers_ of cells inside x, not coordinates. You should probably
construct a spPolygons() object and use that as the `y` argument to
extract().

3. The description of the 'raster' package says that it's been
superseded by the 'terra' package. You don't have to rewrite all your
code now, but it may be beneficial to check whether 'terra' can
accomplish what you want.

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

This must be self-explanatory: your `sse` function returns something
that is not a real number. Try options(error = recover) to see which
arguments it is given when it fails in such a manner. See the free book
"The R Inferno" for more R debugging tips
<https://www.burns-stat.com/documents/books/the-r-inferno/>. Consider
using the 'optimx' package which contains optimisation algorithms that
might work better for you.

-- 
Best regards,
Ivan



More information about the R-help mailing list