Actually, the problem is with the argument for the coords parameter. gwr()
function wants a 2-column matrix for that, Testpts2 is of class
SpatialPointsDataFrame, so the function doesn't know what to do with the
input. We need to specifically call the coords slot of Testpts2 class using
Testpts2@coords. There is probably another problem from TestSmall not
matching the dimensions of the raster layers being regressed, hence the
"rows mismatch" error.

The moral of the story is that when an R function returns error messages,
it's usually a good idea to look up the documentation for the function and
checking the structure of every argument you are passing to the function
using str(). For example (data from an arbitrary file):

> str(Testpts2)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 21752940 obs. of  1 variable:
  .. ..$ SJER2013_DTM: num [1:21752940] 465 465 465 465 465 ...
  ..@ coords.nrs : num(0)
  ..@ coords     : num [1:21752940, 1:2] 254570 254571 254572 254573 254574
...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "x" "y"
  ..@ bbox       : num [1:2, 1:2] 254570 4107302 258868 4112361
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+proj=utm +zone=11 +datum=WGS84 +units=m
+no_defs +ellps=WGS84 +towgs84=0,0,0"

Compare to a matrix with 2 columns:
> A=matrix(0,5,2)
> str(A)
 num [1:5, 1:2] 0 0 0 0 0 0 0 0 0 0

Though we would expect the specific number to be different, the general
format should be similar for objects of the same type. For example, the
str() of a two-column matrix with decimal entries should always begin with:
 num [1:n, 1:2]

Where n is the number of rows.

Hope that is slightly enlightening.

Richard


On Tue, May 9, 2017 at 4:35 PM, Sarah Goslee <sgos...@psu.edu> wrote:

> I suspect that you have incorrectly specified the model: why are you using
> the getValues() directly instead of naming the columns from df2?
>
> If you haven't yet, working through this example will give you a chance to
> look at what the various inputs should be.
> https://rstudio-pubs-static.s3.amazonaws.com/44975_0342ec49f
> 925426fa16ebcdc28210118.html
>
>
> For future questions, there are both ecology and spatial R help lists:
>
> R-sig-ecology (of which I am co-owner)
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>
> R-sig-geo
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> They are probably better places for this kind of question than ECOLOG.
>
> Sarah
>
> model=gwr(getValues(BBSSmall)~getValues(FireSmall_proj),data=df2,coords
> =Testpts2,bandwidth=1000)
>
>
> On 05/08/2017 08:49 PM, James Garrett wrote:
>
>> Hello all:
>>
>> I am having trouble running a geographically weighted regression on
>> raster data using R (raster, spgwr, and sp packages) and was hoping
>> someone on here might have some guidance.
>>
>> I keep getting two different errors when troubleshooting my model.
>> "'dimnames' applied to non-array" or "new data matrix rows mismatch."
>>
>> In order to test my model, I am using simulated data that I made to
>> ensure no missing data values. I have two datasets that I am looking at:
>> BBS and Fire.
>>
>> To my knowledge these errors mean there is missing data, but I can't
>> seem to find any. Does anyone have any insight into these errors? I
>> can't seem to find a good explanation online.
>>
>> I'd be happy to provide any extra output/data examples if needed. Below
>> is my code:
>>
>>
>> Data Import and Setup
>>
>>> BBSSmall=raster("Simulation/BBSraster_Sim_Clip.tif")
>>> FireSmall=raster("Simulation/FireWhole_Clip.tif")
>>> FireSmall_proj=projectRaster(FireSmall,BBSSmall)
>>> alldata2=brick(BBSSmall,FireSmall_proj)
>>> df2=as.data.frame(getValues(alldata2))
>>>
>>
>>
>> Data
>>
>>> FireSmall_proj
>>>
>> class       : RasterLayer
>> dimensions  : 577, 649, 374473  (nrow, ncol, ncell)
>> resolution  : 100, 100  (x, y)
>> extent      : 1050330, 1115230, 1117886, 1175586  (xmin, xmax, ymin,
>> ymax)
>> coord. ref. : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96
>> +x_0=0         +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80
>> +towgs84=0,0,0
>> data source : in memory
>> names       : FireWhole_Clip
>> values      : -2.349897, 12.19254  (min, max)
>>
>> BBSSmall
>>>
>> class       : RasterLayer
>> dimensions  : 577, 649, 374473  (nrow, ncol, ncell)
>> resolution  : 100, 100  (x, y)
>> extent      : 1050330, 1115230, 1117886, 1175586  (xmin, xmax, ymin,
>> ymax)
>> coord. ref. : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96
>> +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0
>> data source : C:\Users\Baruch\Documents\James
>> GIS\BBS\Simulation\BBSraster_Sim_Clip.tif
>> names       : BBSraster_Sim_Clip
>> values      : 8.053012, 35.7119  (min, max)
>>
>>
>> Importing Test Area to Get Coordinates of Each Pixel
>>
>>> TestSmall=raster("Simulation/NewTestSmall.tif")
>>> spts2=rasterToPoints(TestSmall,spatial=TRUE)
>>> geoproj2=proj4string(TestSmall)
>>> geoproj2
>>>
>> *[1] "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0
>> +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"*
>>
>>> Testpts2=spTransform(spts2,CRS(geoproj2))
>>>
>>
>>
>> Model
>>
>>> model=gwr(getValues(BBSSmall)~getValues(FireSmall_proj),data=df2,coords
>>>
>> =Testpts2,bandwidth=1000)
>> ***Error in `colnames<-`(`*tmp*`, value = c("coord.x", "coord.y")) :
>>    'dimnames' applied to non-array***
>>
>>   Using Coordinates (Testpts) as a data frame
>>
>>> Testpts3=as.data.frame(Testpts2)
>>> model2=gwr(getValues(BBSSmall)~getValues(FireSmall_proj),coords=Testpts
>>>
>> 3,bandwidth=1000)
>> **Error in gwr(getValues(BBSSmall) ~ getValues(FireSmall_proj), coords =
>> Testpts3,  :    new data matrix rows mismatch**
>>
>>
>> Thanks in advance,
>> -J
>>
>>
>>
>>
>
> --
> Dr. Sarah Goslee
> USDA-ARS Pasture Systems and Watershed Management Research Unit
> Adjunct Associate Professor, Crop and Soil Sciences Department
> Penn State
> Building 3702, Curtin Road
> University Park, PA 16802
> Phone: 814-863-0887
> Fax: 814-863-0935
> sgos...@psu.edu
>

Reply via email to