0

I'm trying to do Bayesian Occupancy Analysis with site covariates. My first step is making a function. I keep on getting the + in my R console indicating it thinks my code is incomplete. Having ran lines individually I am pretty certain the issue lies in the first line of code. However, I can't work out where exactly I've missed something out and hence where the problem originally lies.

data.fn <- function(R = 39, T = 14, xmin= 0, xmax= 1, alpha.psi = 0.4567,
                beta.psi = 0.0338, alpha.p = 0.4, beta.p = 0.4) {
y <- array(dim = c(R,T)) #This creates an array for counts
  #Ecological Process
  #Covariate values
X <- sort(runif(n=R, min = xmin, max = xmax))
#Expected occurence-covariate relationship
 psi <- plogis(alpha.psi + beta.psi *X) #this applies the inverse logit

 #Add Bernoulli Noise - drawing indicator of occurence (z) from bernoulli psi 
 z<- rbinom(n = R, size = 1, prob = psi)
 occ.fs <- sum(z) #"Finite Sample Occupancy"
 "Make a census"
  p.eff <- z*p
  for (i in 1:T) {
    y[,i] <- rbinom(n=R, size = 1, prob = p.eff)
  }
}

There's more code - i.e. the {} function is complete but the issue started before that was ran and I keep having issues uploading the code into Stack. The error message is simply + all down the left hand side of the R console

EDIT

Could there be something wrong with how R is sensing stuff? For instance with the following code

naive.pred <- plogis(predict(glm(apply(y, 1, max) ~ X + I (X^2),
                             family = binomial)))

I got the error message - unexpected symbol (the bracket) in the family = binomial yet each bracket is paired correctly- there's no extra unnecessary brackets?

chris1
  • 11
  • 3
  • 3
    The `+` as a prompt means: R attends further input to end a structure. It can be a missing `}` for a block or a missing `)` or a missing `"` ... In your question there is no `}` for the code block of your function. – jogo Jul 02 '19 at 12:02
  • Now that you've updated the example so the `{` is closed, what happens when you start a fresh R session and run the example code? (For me it works as expected, no errors and the function is added to the global environment) – IceCreamToucan Jul 02 '19 at 12:54
  • It still doesn't work for me? I still get the exact same error message with a fresh R session - I still get the + message but when I run individual bits of code seperately it has stopped giving me the + message – chris1 Jul 02 '19 at 14:01
  • For `naive.pred` are you looking for estimated occupancy at all `R` sites or the regression coefficents? On top of this, the `naive.pred` code functions just fine assuming that you have `X` and `y` in your global environment. – mfidino Jul 02 '19 at 14:39
  • Tbh my first aim is getting the code to work and then I'll refine these other more precise features. The code doesn't seem to work on my R studio at all but appears to work on all those commenting etc. Why could this be? – chris1 Jul 02 '19 at 14:56
  • If you run the code block [here](https://rdrr.io/snippets/) there is no error and the function is assigned properly. Are you sure you are *only* running the first code block in the question after starting a new R session (and not after running other code)? If so, the only other possibility I see is that you've put some incomplete code in your .Rprofile – IceCreamToucan Jul 02 '19 at 15:51

1 Answers1

0

While I did not see a + issue when I looked over your code, you are not simulating the observed data correctly and there was a p object inside your function that did not have an argument passed to it. You did create a logit linear predictor for psi using alpha.psi and beta.psi, however you are lacking a logit linear predictor for the probability of detecting a species given that they are there using alpha.p and beta.p. Assuming that the covariate X is used for both the latent occupancy state and the observation model the code should become.

data.fn <- function(R = 39, T = 14, xmin= 0, xmax= 1, alpha.psi = 0.4567,
                                        beta.psi = 0.0338, alpha.p = 0.4, beta.p = 0.4) {
    y <- array(dim = c(R,T)) #This creates an array for counts
    #Ecological Process
    #Covariate values
    X <- sort(runif(n=R, min = xmin, max = xmax))
    #Expected occurence-covariate relationship
    psi <- plogis(alpha.psi + beta.psi *X) #this applies the inverse logit

    #Add Bernoulli Noise - drawing indicator of occurence (z) from bernoulli psi 
    z<- rbinom(n = R, size = 1, prob = psi)
    occ.fs <- sum(z) #"Finite Sample Occupancy"
    # Linear predictor for detection,
    #  assuming the same covariate is used for detection
    p.eff <- plogis(alpha.p + beta.p * X)
    for (i in 1:T) {
        y[,i] <- rbinom(n=R, size = 1, prob = p.eff * z)
    }
    return(list(y = y, z = z, X = X, occ.fs = occ.fs))
}

This code assumes that you are passing logit scale parameters to the data, so if you are trying to simulate data such that X has a very marginal and positive influence on occupancy then you are off to the races, so to speak. If you are looking for a more pronounced effect, than you should increase the effect size. Finally, 39 site is very few for an occupancy analysis given that binary deteciton / non-detection data is quite information poor. Don't be surprised if you posterior estimates you get from analyzing the dataset do not actually return the parameters used to simulate the data.

mfidino
  • 3,030
  • 1
  • 9
  • 13