[R-sig-Geo] Breakpoint analysis with two variables
Robert J. Hijmans
r.hijmans at gmail.com
Thu Jan 23 03:07:50 CET 2014
Nate,
With questions like this, please provide a fully reproducible example
with data created by the script or from example data from a package
(see the examples in the help files and the list archives for how to
that).
The error occurs because fun1 returns either NA or an object of class
'lm' (instead of a number). See ?calc for how to use lm with calc.
Robert
On Wed, Jan 22, 2014 at 12:48 PM, nate_m <Nathaniel.L.Mikle-1 at ou.edu> wrote:
> Hi all,
>
> I am a beginner, so bear with me please. My goal is to perform breakpoint
> analysis (package 'segmented') on each cell of two raster stacks (11 files
> thick each). Some cells throughout both bricks are NA, so I have to skip
> these as well. My goal is to output breakpoints with confidence intervals
> based on the two variables for each cell. Here is my code and the error, I
> think I am doing something incorrectly with data types;
>
> library(raster)
> #each stack is 11 rasters thick
> y1 <- stack(y2)
> x1 <- stack(x2)
>
> #testing on small region
> y <- crop(y1, extent(-103,-102,37,38))
> x <- crop(x1, extent(-103,-102,37,38))
>
> fun1 <- function(x) {
> m <- NA
> try(m <- lm(x ~ y), silent=T)
> m
> }
>
> library(segmented)
>
> fun2 <- function(x) {
> m <- NA
> try(m <- segmented(x, ~y, psi=150), silent=T)
> m
> }
>
> fun3 <- function(x) {
> m <- NA
> try(m <- confint(x), silent=T)
> m
> }
>
> e1 <- calc(y, fun1)
> os <- calc(e1, fun2)
> results <- calc(os, fun3)
>
> This is the error I receive after running the first function with e1;
>
> Error in model.frame.default(formula = x ~ y, drop.unused.levels = TRUE) :
> invalid type (S4) for variable 'y'
>
> If anybody has a thought as to what I am doing wrong, it would be of great
> help! Additionally, I would like to set "psi" from the segmented package
> with values from a raster- is this possible?
>
> Thanks in advance,
> Nate
>
>
>
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Breakpoint-analysis-with-two-variables-tp7585607.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list