Skip to content

change the way lag variables are detected in predict #38

Open

Description

@rsbivand

Handling of formula Durbin objects does not work, get to line 233 in R/predict.sarlm.R.

Activity

  1. dpr01 commented on Mar 6, 2024

    @dpr01

    Hi, I have this issue when using predict on a Durbin model with only some lagged variables. Assuming the problem isn't easy to fix, would there be any other way to generate forecasts with a model like this? Thanks.

  2. rsbivand commented on Mar 7, 2024

    @rsbivand
    MemberAuthor

    Could you provide a reproducible example that matches your case?

  3. dpr01 commented on Mar 7, 2024

    @dpr01

    So after looking at it more carefully, I believe my error was actually being caused by a mismatch of structure between my original dataset and the predicting dataset. The message I had was:

    "Error in predict.Sarlm(mod, dfp, listw = weights) :
    unknown mismatch. please report this bug
    In addition: Warning message:
    In colnames(Xs.not.lagged) != colnames(Xo) :
    longer object length is not a multiple of shorter object length"

    I assumed the first error (which in the code you link to this issue) was the main problem but fixing the second warning appears to have made everything work. An example of what I'm doing:

    library(tidyverse)
    library(sf)
    library(spdep)
    library(spatialreg)
    
    # Dataset
    df <- st_read(system.file("shapes/boston_tracts.shp", package = "spData"))
    
    # Spatial weights
    centroids <- st_centroid(df)
    neigh <- dnearneigh(centroids, d1 = 0, d2 = 30)
    weights <- nb2listw(neigh, glist = lapply(nbdists(neigh, centroids), \(x) 1/(x^2)))
    
    # Model
    mod <- lagsarlm(log(MEDV) ~ CRIM + ZN + INDUS + CHAS + I(NOX^2) + I(RM^2) +
                      AGE + log(DIS) + log(RAD) + TAX + PTRATIO + B + log(LSTAT), 
                    data = df,
                    Durbin = ~ CRIM,
                    listw = weights)
    summary(mod)
    
    # Predict
    dfp <- df %>% 
      mutate(TAX = 0.5 * TAX)
    
    predict(mod, dfp, listw = weights)

    I think this works fine. But just to be clear, predict is aware that only CRIM is a spatially lagged variable in the model, as specified in lagsarlm, right? I think I may have misunderstood this issue.

  4. rsbivand commented on Mar 7, 2024

    @rsbivand
    MemberAuthor

    Yes, predict only uses lag.CRIM, not other lagged X variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions