I am working on running an OLS on a shapefile and running tests on the residuals of that OLS where I am getting an error.


#read in my shapefile
fires_OGR = readOGR(dsn = "C:/Users/westi/Desktop/unm_grad_21/580/project", layer="grid_1acre_fires")


class(fires_OGR$FIRE) = "Numeric"
class(fires_OGR$CA_GAP) = "Numeric"
class(fires_OGR$MEAN_PPT) = "Numeric"
class(fires_OGR$MEAN_SLOPE) = "Numeric"
class(fires_OGR$MEAN_ELEV) = "Numeric"
class(fires_OGR$MEAN_VPD_M) = "Numeric"
class(fires_OGR$MEAN_VPD_M.1) = "Numeric"
class(fires_OGR$MEAN_ASPEC) = "Numeric"

#row standardized "listw" object (default style = "W")
fires_queen_nb = poly2nb(fires_OGR, queen=TRUE)
fires_w = nb2listw(fires_queen_nb, zero.policy = TRUE)

#ols for fire count per grid
fires_ogr_OLS = lm(FIRE~CA_GAP + MEAN_PPT + MEAN_ELEV + MEAN_SLOPE +  MEAN_VPD_M +  MEAN_VPD_M.1 + MEAN_ASPEC, data = fires_OGR)

#saving residuals and fitted values
fires_OGR$olsresid = residuals(fires_ogr_OLS)
fires_OGR$ols_fitted = fitted(fires_ogr_OLS)

#moran test for residuals
lm.morantest(fires_ogr_OLS, fires_w)

#lagrange multiplier tests for residuals
#fits a linear regression model for the squared residuals and examines 
#whether the fitted model is significant
lm.LMtests(fires_ogr_OLS, fires_w, test = "all")

#breusch pagan test for heteroskedasticity

Here is the error in the R terminal: enter image description here

I have tried making all the attributes I am using in my regression numeric since the error suggests that "y is not numeric" and when I run the "class(fires_OGR)" enter image description here So I thought changing all the attributes used to numeric would fix the problem, but it did not.

I guess the problem is I don't know what "y" is in this situation.

Here is the summary from fires_OGR:

Object of class SpatialPolygonsDataFrame
         min        max
x -120.69009 -115.78261
y   32.49987   35.02487
Is projected: FALSE 
proj4string : [+proj=longlat +datum=WGS84 +no_defs]
Data attributes:
    CA_GAP             MEAN_PPT         MEAN_VPD_M     MEAN_VPD_M.1      MAX_SLOPE       MEAN_SLOPE       MEAN_ASPEC      MEAN_ELEV     
 Length:2346        Min.   :  61.84   Min.   :0.697   Min.   : 4.269   Min.   : 0.00   Min.   : 0.000   Min.   :  0.0   Min.   : -66.5  
 Class :character   1st Qu.: 285.70   1st Qu.:2.427   1st Qu.:18.219   1st Qu.:19.45   1st Qu.: 3.708   1st Qu.:160.9   1st Qu.: 220.7  
 Mode  :character   Median : 389.03   Median :3.544   Median :22.199   Median :29.23   Median : 7.937   Median :182.0   Median : 552.6  
                    Mean   : 404.13   Mean   :3.923   Mean   :22.441   Mean   :26.65   Mean   : 8.741   Mean   :178.1   Mean   : 687.7  
                    3rd Qu.: 497.21   3rd Qu.:4.997   3rd Qu.:25.526   3rd Qu.:34.57   3rd Qu.:12.912   3rd Qu.:200.3   3rd Qu.:1077.2  
                    Max.   :1237.33   Max.   :9.588   Max.   :40.049   Max.   :57.39   Max.   :27.985   Max.   :279.1   Max.   :2904.3  
  MAJORITY_E            FIRE              mn_ppt           mn_vpd_max         mx_slope64          mn_slope          mn_aspect        
 Length:2346        Length:2346        Length:2346        Length:2346        Length:2346        Length:2346        Length:2346       
 Class :character   Class :character   Class :character   Class :character   Class :character   Class :character   Class :character  
 Mode  :character   Mode  :character   Mode  :character   Mode  :character   Mode  :character   Mode  :character   Mode  :character  
   mn_elev            maj_elev          mn_vpd_min       
 Length:2346        Length:2346        Length:2346       
 Class :character   Class :character   Class :character  
 Mode  :character   Mode  :character   Mode  :character 
  • "Numeric" is NOT what numeric-valued data has for a class. Not sure why you thought that. It should be "numeric", with lower-case, but even so, assigning a class doesn't change the actual data. To convert to numeric you'd typically use as.numeric(Z) where Z might be some vector of character objects, eg Z=c("1.2","BAD","2.3","99").
    – Spacedman
    May 2, 2022 at 12:08
  • 1
    You need to inspect fires_OGR to find out what the columns are. We don't have your data so we can't do this. The summary function should help you, and if you edit your question and show us the output (as pasted text, not screenshots) that will help us help you.
    – Spacedman
    May 2, 2022 at 12:10
  • @Spacedman Hi, I added the summary(fires_OGR)
    – sarakota
    May 2, 2022 at 16:10
  • 1
    That tells us you have character columns - eg CA_GAP. Is that expected? What do they look like? Have you tried simplifying the model to see if that's the problem? Really the best way to solve problems yourself in R is to try stuff*. Simplify models, shrink the data, look at the data - test all your assumptions and figure out which one is wrong!
    – Spacedman
    May 2, 2022 at 17:22
  • @Spacedman I decided to take that part out of the model and it still throws the same error. I just tried fires_OGR$FIRE = as.numeric(fires_OGR$FIRE) and that seems to have worked actually
    – sarakota
    May 2, 2022 at 17:41

1 Answer 1


Changing the dependent variable's class from character to numeric resolved the problem.

fires_OGR$FIRE = as.numeric(fires_OGR$FIRE)
  • As long as that doesn't give a warning about "NAs introduced by coercion" then its successfully converted any character representations of numbers to numeric form. But definitely check that the range of numbers and maybe the histogram of the values looks sensible before proceeding!
    – Spacedman
    May 2, 2022 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.