Cross-validation and variance calculation in the `gstat` package in R The 2019 Stack Overflow Developer Survey Results Are In Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceHow to unload a package without restarting RWhat are the units of distance in gstat variogram?finalPolygonCRS + over() function sp package in RCalculating variance covariance matrix for improved kppm modelLocal Block Kriging with Local Variogram with gstatCreate variogram in R's gstat packageCreate Grid in R for kriging in gstatUniversal kriging using lat long gstat RDefining new correlation model in gstat package in R?calculating centroid of raster

Why can't devices on different VLANs, but on the same subnet, communicate?

Can withdrawing asylum be illegal?

60's-70's movie: home appliances revolting against the owners

How to determine omitted units in a publication

Single author papers against my advisor's will?

Mortgage adviser recommends a longer term than necessary combined with overpayments

Huge performance difference of the command find with and without using %M option to show permissions

Do I have Disadvantage attacking with an off-hand weapon?

Are spiders unable to hurt humans, especially very small spiders?

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

Are there continuous functions who are the same in an interval but differ in at least one other point?

Presidential Pardon

What's the point in a preamp?

Sub-subscripts in strings cause different spacings than subscripts

Why did Peik Lin say, "I'm not an animal"?

Store Dynamic-accessible hidden metadata in a cell

should truth entail possible truth

Is this wall load bearing? Blueprints and photos attached

Is every episode of "Where are my Pants?" identical?

Does Parliament need to approve the new Brexit delay to 31 October 2019?

Does Parliament hold absolute power in the UK?

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

For what reasons would an animal species NOT cross a *horizontal* land bridge?

Can the DM override racial traits?



Cross-validation and variance calculation in the `gstat` package in R



The 2019 Stack Overflow Developer Survey Results Are In
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceHow to unload a package without restarting RWhat are the units of distance in gstat variogram?finalPolygonCRS + over() function sp package in RCalculating variance covariance matrix for improved kppm modelLocal Block Kriging with Local Variogram with gstatCreate variogram in R's gstat packageCreate Grid in R for kriging in gstatUniversal kriging using lat long gstat RDefining new correlation model in gstat package in R?calculating centroid of raster



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Good day,



I am getting some difficulty when trying to calculate the variance from an inverse distance krig done in the gstat package. I would also like to run a cross-validation on an independent test set of variables, but I am not sure of how to do so in R with spatial data. Using the meuse dataset, this is what I attempted for calculating variance:



data(meuse); coordinates(meuse) <- ~x+y 

#randomly sample to get training and test data for later cross-validation
set.seed = (123)

sub1 <- nrow(meuse@data); len1 <- ceiling(sub1*2/3)

m.train <- meuse
m.train@data <- meuse@data[1:len1,]
m.train@coords <- meuse@coords[1:len1,]

m.test <- meuse
m.test@data <- meuse@data[(len1+1):sub1,]
m.test@coords <- meuse@coords[(len1+1):sub1,]

## load grids:
data(meuse.grid); coordinates(meuse.grid) <- ~x+y
gridded(meuse.grid) <- TRUE; fullgrid(meuse.grid) <- TRUE

zinc.id <- krige(zinc~1, m.train, meuse.grid) ## inverse distance weighting

# --- My attempt at calculation of variance
rmse.id <- sqrt(mean((meuse.test@data$zinc - zinc.id@data$var1.pred)^2))

Warning message:
In meuse.test@data$z - zinc.id@data$var1.pred :
longer object length is not a multiple of shorter object length


I can see why I am getting the error, but I am not sure how to proceed. I can do the cross-validation outside of R with a bit of trouble, but I really would like to keep all my working within R. Any suggestions would be most welcomed.



Kurt










share|improve this question




























    0















    Good day,



    I am getting some difficulty when trying to calculate the variance from an inverse distance krig done in the gstat package. I would also like to run a cross-validation on an independent test set of variables, but I am not sure of how to do so in R with spatial data. Using the meuse dataset, this is what I attempted for calculating variance:



    data(meuse); coordinates(meuse) <- ~x+y 

    #randomly sample to get training and test data for later cross-validation
    set.seed = (123)

    sub1 <- nrow(meuse@data); len1 <- ceiling(sub1*2/3)

    m.train <- meuse
    m.train@data <- meuse@data[1:len1,]
    m.train@coords <- meuse@coords[1:len1,]

    m.test <- meuse
    m.test@data <- meuse@data[(len1+1):sub1,]
    m.test@coords <- meuse@coords[(len1+1):sub1,]

    ## load grids:
    data(meuse.grid); coordinates(meuse.grid) <- ~x+y
    gridded(meuse.grid) <- TRUE; fullgrid(meuse.grid) <- TRUE

    zinc.id <- krige(zinc~1, m.train, meuse.grid) ## inverse distance weighting

    # --- My attempt at calculation of variance
    rmse.id <- sqrt(mean((meuse.test@data$zinc - zinc.id@data$var1.pred)^2))

    Warning message:
    In meuse.test@data$z - zinc.id@data$var1.pred :
    longer object length is not a multiple of shorter object length


    I can see why I am getting the error, but I am not sure how to proceed. I can do the cross-validation outside of R with a bit of trouble, but I really would like to keep all my working within R. Any suggestions would be most welcomed.



    Kurt










    share|improve this question
























      0












      0








      0


      1






      Good day,



      I am getting some difficulty when trying to calculate the variance from an inverse distance krig done in the gstat package. I would also like to run a cross-validation on an independent test set of variables, but I am not sure of how to do so in R with spatial data. Using the meuse dataset, this is what I attempted for calculating variance:



      data(meuse); coordinates(meuse) <- ~x+y 

      #randomly sample to get training and test data for later cross-validation
      set.seed = (123)

      sub1 <- nrow(meuse@data); len1 <- ceiling(sub1*2/3)

      m.train <- meuse
      m.train@data <- meuse@data[1:len1,]
      m.train@coords <- meuse@coords[1:len1,]

      m.test <- meuse
      m.test@data <- meuse@data[(len1+1):sub1,]
      m.test@coords <- meuse@coords[(len1+1):sub1,]

      ## load grids:
      data(meuse.grid); coordinates(meuse.grid) <- ~x+y
      gridded(meuse.grid) <- TRUE; fullgrid(meuse.grid) <- TRUE

      zinc.id <- krige(zinc~1, m.train, meuse.grid) ## inverse distance weighting

      # --- My attempt at calculation of variance
      rmse.id <- sqrt(mean((meuse.test@data$zinc - zinc.id@data$var1.pred)^2))

      Warning message:
      In meuse.test@data$z - zinc.id@data$var1.pred :
      longer object length is not a multiple of shorter object length


      I can see why I am getting the error, but I am not sure how to proceed. I can do the cross-validation outside of R with a bit of trouble, but I really would like to keep all my working within R. Any suggestions would be most welcomed.



      Kurt










      share|improve this question














      Good day,



      I am getting some difficulty when trying to calculate the variance from an inverse distance krig done in the gstat package. I would also like to run a cross-validation on an independent test set of variables, but I am not sure of how to do so in R with spatial data. Using the meuse dataset, this is what I attempted for calculating variance:



      data(meuse); coordinates(meuse) <- ~x+y 

      #randomly sample to get training and test data for later cross-validation
      set.seed = (123)

      sub1 <- nrow(meuse@data); len1 <- ceiling(sub1*2/3)

      m.train <- meuse
      m.train@data <- meuse@data[1:len1,]
      m.train@coords <- meuse@coords[1:len1,]

      m.test <- meuse
      m.test@data <- meuse@data[(len1+1):sub1,]
      m.test@coords <- meuse@coords[(len1+1):sub1,]

      ## load grids:
      data(meuse.grid); coordinates(meuse.grid) <- ~x+y
      gridded(meuse.grid) <- TRUE; fullgrid(meuse.grid) <- TRUE

      zinc.id <- krige(zinc~1, m.train, meuse.grid) ## inverse distance weighting

      # --- My attempt at calculation of variance
      rmse.id <- sqrt(mean((meuse.test@data$zinc - zinc.id@data$var1.pred)^2))

      Warning message:
      In meuse.test@data$z - zinc.id@data$var1.pred :
      longer object length is not a multiple of shorter object length


      I can see why I am getting the error, but I am not sure how to proceed. I can do the cross-validation outside of R with a bit of trouble, but I really would like to keep all my working within R. Any suggestions would be most welcomed.



      Kurt







      r geospatial spatial






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 16 '14 at 5:57









      user2507608user2507608

      170514




      170514






















          1 Answer
          1






          active

          oldest

          votes


















          2














          To perform this kind of comparison, you need to use meuse and not meuse.grid as the newdata. Or even better, use krige.cv.



          For example using the meuse dataset:



          kr_cv = krige.cv(log(zinc)~1, meuse, vgm(.59, "Sph", 874, .04))
          kr_cv[1:5,]
          coordinates var1.pred var1.var observed residual zscore fold
          1 (181072, 333611) 6.784729 0.1681011 6.929517 0.14478795 0.35314023 1
          2 (181025, 333558) 6.777372 0.1635077 7.039660 0.26228828 0.64864901 2
          3 (181165, 333537) 6.294508 0.1723531 6.461468 0.16696067 0.40216530 3
          4 (181298, 333484) 6.033072 0.2191244 5.549076 -0.48399603 -1.03394256 4
          5 (181307, 333330) 5.576879 0.1643513 5.594711 0.01783242 0.04398694 5


          From this you can easily calculate the RMSE of the cross-validation. The automap package (disclaimer: which I wrote) contains a convienient function that can calculate a lot of these stats for you. Normally it only accepts the output of autoKrige.cv, but using a small hack you can still use it:



          library(automap)
          compare.cv(list(krige.cv_output = kr_cv))
          krige.cv_output
          mean_error 0.0003146
          me_mean 5.345e-05
          MAE 0.2898
          MSE 0.1515
          MSNE 0.8607
          cor_obspred 0.8416
          cor_predres 0.05449
          RMSE 0.3892
          RMSE_sd 0.5391
          URMSE 0.3892
          iqr 0.3949





          share|improve this answer

























          • Thanks a million Paul, your package certainly looks helpful and I will definitely use it. However I am still a bit unsure with how to include the test and training datasets. Going back to my example would the correct formula to run a cross validation on the unseen (test) data set be kr.cv.id<-krige.cv(zinc~1, m.train, newdata=m.test) #For inverse distance interpolation

            – user2507608
            May 16 '14 at 22:51











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23693521%2fcross-validation-and-variance-calculation-in-the-gstat-package-in-r%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          To perform this kind of comparison, you need to use meuse and not meuse.grid as the newdata. Or even better, use krige.cv.



          For example using the meuse dataset:



          kr_cv = krige.cv(log(zinc)~1, meuse, vgm(.59, "Sph", 874, .04))
          kr_cv[1:5,]
          coordinates var1.pred var1.var observed residual zscore fold
          1 (181072, 333611) 6.784729 0.1681011 6.929517 0.14478795 0.35314023 1
          2 (181025, 333558) 6.777372 0.1635077 7.039660 0.26228828 0.64864901 2
          3 (181165, 333537) 6.294508 0.1723531 6.461468 0.16696067 0.40216530 3
          4 (181298, 333484) 6.033072 0.2191244 5.549076 -0.48399603 -1.03394256 4
          5 (181307, 333330) 5.576879 0.1643513 5.594711 0.01783242 0.04398694 5


          From this you can easily calculate the RMSE of the cross-validation. The automap package (disclaimer: which I wrote) contains a convienient function that can calculate a lot of these stats for you. Normally it only accepts the output of autoKrige.cv, but using a small hack you can still use it:



          library(automap)
          compare.cv(list(krige.cv_output = kr_cv))
          krige.cv_output
          mean_error 0.0003146
          me_mean 5.345e-05
          MAE 0.2898
          MSE 0.1515
          MSNE 0.8607
          cor_obspred 0.8416
          cor_predres 0.05449
          RMSE 0.3892
          RMSE_sd 0.5391
          URMSE 0.3892
          iqr 0.3949





          share|improve this answer

























          • Thanks a million Paul, your package certainly looks helpful and I will definitely use it. However I am still a bit unsure with how to include the test and training datasets. Going back to my example would the correct formula to run a cross validation on the unseen (test) data set be kr.cv.id<-krige.cv(zinc~1, m.train, newdata=m.test) #For inverse distance interpolation

            – user2507608
            May 16 '14 at 22:51















          2














          To perform this kind of comparison, you need to use meuse and not meuse.grid as the newdata. Or even better, use krige.cv.



          For example using the meuse dataset:



          kr_cv = krige.cv(log(zinc)~1, meuse, vgm(.59, "Sph", 874, .04))
          kr_cv[1:5,]
          coordinates var1.pred var1.var observed residual zscore fold
          1 (181072, 333611) 6.784729 0.1681011 6.929517 0.14478795 0.35314023 1
          2 (181025, 333558) 6.777372 0.1635077 7.039660 0.26228828 0.64864901 2
          3 (181165, 333537) 6.294508 0.1723531 6.461468 0.16696067 0.40216530 3
          4 (181298, 333484) 6.033072 0.2191244 5.549076 -0.48399603 -1.03394256 4
          5 (181307, 333330) 5.576879 0.1643513 5.594711 0.01783242 0.04398694 5


          From this you can easily calculate the RMSE of the cross-validation. The automap package (disclaimer: which I wrote) contains a convienient function that can calculate a lot of these stats for you. Normally it only accepts the output of autoKrige.cv, but using a small hack you can still use it:



          library(automap)
          compare.cv(list(krige.cv_output = kr_cv))
          krige.cv_output
          mean_error 0.0003146
          me_mean 5.345e-05
          MAE 0.2898
          MSE 0.1515
          MSNE 0.8607
          cor_obspred 0.8416
          cor_predres 0.05449
          RMSE 0.3892
          RMSE_sd 0.5391
          URMSE 0.3892
          iqr 0.3949





          share|improve this answer

























          • Thanks a million Paul, your package certainly looks helpful and I will definitely use it. However I am still a bit unsure with how to include the test and training datasets. Going back to my example would the correct formula to run a cross validation on the unseen (test) data set be kr.cv.id<-krige.cv(zinc~1, m.train, newdata=m.test) #For inverse distance interpolation

            – user2507608
            May 16 '14 at 22:51













          2












          2








          2







          To perform this kind of comparison, you need to use meuse and not meuse.grid as the newdata. Or even better, use krige.cv.



          For example using the meuse dataset:



          kr_cv = krige.cv(log(zinc)~1, meuse, vgm(.59, "Sph", 874, .04))
          kr_cv[1:5,]
          coordinates var1.pred var1.var observed residual zscore fold
          1 (181072, 333611) 6.784729 0.1681011 6.929517 0.14478795 0.35314023 1
          2 (181025, 333558) 6.777372 0.1635077 7.039660 0.26228828 0.64864901 2
          3 (181165, 333537) 6.294508 0.1723531 6.461468 0.16696067 0.40216530 3
          4 (181298, 333484) 6.033072 0.2191244 5.549076 -0.48399603 -1.03394256 4
          5 (181307, 333330) 5.576879 0.1643513 5.594711 0.01783242 0.04398694 5


          From this you can easily calculate the RMSE of the cross-validation. The automap package (disclaimer: which I wrote) contains a convienient function that can calculate a lot of these stats for you. Normally it only accepts the output of autoKrige.cv, but using a small hack you can still use it:



          library(automap)
          compare.cv(list(krige.cv_output = kr_cv))
          krige.cv_output
          mean_error 0.0003146
          me_mean 5.345e-05
          MAE 0.2898
          MSE 0.1515
          MSNE 0.8607
          cor_obspred 0.8416
          cor_predres 0.05449
          RMSE 0.3892
          RMSE_sd 0.5391
          URMSE 0.3892
          iqr 0.3949





          share|improve this answer















          To perform this kind of comparison, you need to use meuse and not meuse.grid as the newdata. Or even better, use krige.cv.



          For example using the meuse dataset:



          kr_cv = krige.cv(log(zinc)~1, meuse, vgm(.59, "Sph", 874, .04))
          kr_cv[1:5,]
          coordinates var1.pred var1.var observed residual zscore fold
          1 (181072, 333611) 6.784729 0.1681011 6.929517 0.14478795 0.35314023 1
          2 (181025, 333558) 6.777372 0.1635077 7.039660 0.26228828 0.64864901 2
          3 (181165, 333537) 6.294508 0.1723531 6.461468 0.16696067 0.40216530 3
          4 (181298, 333484) 6.033072 0.2191244 5.549076 -0.48399603 -1.03394256 4
          5 (181307, 333330) 5.576879 0.1643513 5.594711 0.01783242 0.04398694 5


          From this you can easily calculate the RMSE of the cross-validation. The automap package (disclaimer: which I wrote) contains a convienient function that can calculate a lot of these stats for you. Normally it only accepts the output of autoKrige.cv, but using a small hack you can still use it:



          library(automap)
          compare.cv(list(krige.cv_output = kr_cv))
          krige.cv_output
          mean_error 0.0003146
          me_mean 5.345e-05
          MAE 0.2898
          MSE 0.1515
          MSNE 0.8607
          cor_obspred 0.8416
          cor_predres 0.05449
          RMSE 0.3892
          RMSE_sd 0.5391
          URMSE 0.3892
          iqr 0.3949






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 16 '14 at 18:37

























          answered May 16 '14 at 6:01









          Paul HiemstraPaul Hiemstra

          48.7k9106134




          48.7k9106134












          • Thanks a million Paul, your package certainly looks helpful and I will definitely use it. However I am still a bit unsure with how to include the test and training datasets. Going back to my example would the correct formula to run a cross validation on the unseen (test) data set be kr.cv.id<-krige.cv(zinc~1, m.train, newdata=m.test) #For inverse distance interpolation

            – user2507608
            May 16 '14 at 22:51

















          • Thanks a million Paul, your package certainly looks helpful and I will definitely use it. However I am still a bit unsure with how to include the test and training datasets. Going back to my example would the correct formula to run a cross validation on the unseen (test) data set be kr.cv.id<-krige.cv(zinc~1, m.train, newdata=m.test) #For inverse distance interpolation

            – user2507608
            May 16 '14 at 22:51
















          Thanks a million Paul, your package certainly looks helpful and I will definitely use it. However I am still a bit unsure with how to include the test and training datasets. Going back to my example would the correct formula to run a cross validation on the unseen (test) data set be kr.cv.id<-krige.cv(zinc~1, m.train, newdata=m.test) #For inverse distance interpolation

          – user2507608
          May 16 '14 at 22:51





          Thanks a million Paul, your package certainly looks helpful and I will definitely use it. However I am still a bit unsure with how to include the test and training datasets. Going back to my example would the correct formula to run a cross validation on the unseen (test) data set be kr.cv.id<-krige.cv(zinc~1, m.train, newdata=m.test) #For inverse distance interpolation

          – user2507608
          May 16 '14 at 22:51



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23693521%2fcross-validation-and-variance-calculation-in-the-gstat-package-in-r%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript