Error in model.frame.default invalid type (NULL) for variable

3k Views Asked by At

I'm quite new to R and trying to create a function. However, when I try to run it, I get an error and haven't been able to solve the problem.

This is my function:

 myfunction <- function(spatialdata,variable1,variable2,variable3){
  GWRbandwidth <- gwr.sel(spatialdata$variable1 ~ spatialdata$variable2+spatialdata$variable3, data=spatialdata,adapt=T)
  
  gwr.model <- gwr(spatialdata$variable1 ~ spatialdata$variable2+spatialdata$variable3, data = spatialdata, adapt=GWRbandwidth, hatmatrix=TRUE, se.fit=TRUE,
                  list(variable1 = as.name(variable1),
                       variable2 = as.name(variable2),
                       variable3 = as.name(variable3)) )
  
  results <-as.data.frame(gwr.model$SDF)
  
  gwr.map<- spatialdata
  
  gwr.map@data <- cbind(spatialdata@data, as.matrix(results))
  
  qtm(gwr.map, fill = "localR2")
}

And when I try to run it like this:

myfunction(variable1 = "Low_Occupancy",
           variable2 = "Qualification",
           variable3 = "Unemployed",
           spatialdata = OA.Census)

I get the following error:

Error in model.frame.default(formula = spatialdata$variable1 ~ spatialdata$variable2 +  : 
  invalid type (NULL) for variable 'spatialdata$variable1' 

It would be great if someone could help me out!

0

There are 0 best solutions below