Coder Social home page Coder Social logo

Error in predict function about bmstdr HOT 3 CLOSED

Hallo951 avatar Hallo951 commented on June 20, 2024
Error in predict function

from bmstdr.

Comments (3)

Hallo951 avatar Hallo951 commented on June 20, 2024

That's very strange.

I have tried their package on different computers and the error occurs on all of them. The incorrect line for me is the following:

call.f<-as.formula(paste("tmp~",paste(call.f,sep="")[[3]]))

The error is:
Error in paste(call.f, sep = "")[[3]] : subscript out of bounds

Where call.f is the model formula. Does the following work for them:

call.f <- as.formula(y8hrmax ~ xmaxtemp + xwdsp + xrh)

call.f<-as.formula(paste("tmp~",paste(call.f,sep="")[[3]]))

After all, this line does nothing but replace "y8hrmax" with "tmp". And just this causes me the error no matter which computer I test it on...

Do you have an additional package installed or something?

from bmstdr.

sujit-sahu avatar sujit-sahu commented on June 20, 2024

Thank you. But the file spGPP.r is not within bmstdr package. It belongs to spTimer. Please contact the spTimer maintainer.
However, there is an easier solution which worked for me. Just do not attach the library string. I believe that is messing up some function used in the spTimer code.

Please see below the code and the output I produced without error on my Windows computer.

Restarting R session...

Restart R

rm(list=ls())
library(bmstdr)
Loading required package: Rcpp
Registered S3 method overwritten by 'GGally':
method from
+.gg ggplot2
Warning messages:
1: package ‘bmstdr’ was built under R version 4.1.3
2: package ‘Rcpp’ was built under R version 4.1.3
library(spTimer)

spTimer version: 3.3.1

set.seed(11)
s <- sort(sample(unique(nysptime$s.index), size = floor((length(unique(nysptime$s.index))/100)*20)))
DataFit <- spT.subset(data = nysptime, var.name = "s.index", s = s, reverse = T)
DataValPred <- spT.subset(data = nysptime, var.name = "s.index", s = s, reverse = F)
mod <- Bsptime(package = "spTimer",

  •            model = "GPP", 
    
  •            formula = as.formula(y8hrmax ~ xmaxtemp + xwdsp + xrh), 
    
  •            data = DataFit, 
    
  •            n.report = 5, 
    
  •            coordtype = "utm", 
    
  •            coords = 4:5, 
    
  •            scale.transform = "NONE", 
    
  •            g_size = 4,
    
  •            N = 2000, 
    
  •            mchoice = F,
    
  •            plotit = F)
    

Output: GPP approximation models

Sampled: 400 of 2000, 20.00%.
Batch Acceptance Rate (phi): 47.62%
Checking Parameters:
phi: 0.0082, rho: 0.2140, sig2eps: 24.8181, sig2eta: 213.0410
beta[1]: -18.3156 beta[2]: 2.3864 beta[3]: 0.6344 beta[4]: -0.5185


Sampled: 800 of 2000, 40.00%.
Batch Acceptance Rate (phi): 46.18%
Checking Parameters:
phi: 0.0078, rho: 0.2410, sig2eps: 24.8995, sig2eta: 177.1832
beta[1]: -5.5031 beta[2]: 1.9212 beta[3]: 1.1077 beta[4]: -0.2455


Sampled: 1200 of 2000, 60.00%.
Batch Acceptance Rate (phi): 44.37%
Checking Parameters:
phi: 0.0088, rho: 0.3057, sig2eps: 26.5538, sig2eta: 178.2912
beta[1]: -8.7637 beta[2]: 1.9623 beta[3]: 1.2766 beta[4]: -0.8588


Sampled: 1600 of 2000, 80.00%.
Batch Acceptance Rate (phi): 44.97%
Checking Parameters:
phi: 0.0102, rho: 0.2496, sig2eps: 25.4951, sig2eta: 180.1180
beta[1]: -23.6339 beta[2]: 2.3384 beta[3]: 1.1674 beta[4]: 0.6791


Sampled: 2000 of 2000, 100.00%.
Batch Acceptance Rate (phi): 44.22%
Checking Parameters:
phi: 0.0090, rho: 0.1954, sig2eps: 26.7600, sig2eta: 194.2956
beta[1]: -19.1704 beta[2]: 2.3443 beta[3]: 0.7325 beta[4]: 0.2659

nBurn = 1000 . Iterations = 2000 .

Acceptance rate: (phi) = 44.2 %

Elapsed time: 1.42 Sec.

Model: GPP

Total time taken:: 1.46 - Sec.

Spatial prediction using spT.Gibbs output

pred.gp <- predict(mod$fit, tol.dist=0.0, newdata = DataValPred, newcoords = ~ Longitude + Latitude)

Prediction: GPP models

Sampled: 100 of 1000, 10.00%


Sampled: 200 of 1000, 20.00%


Sampled: 300 of 1000, 30.00%


Sampled: 400 of 1000, 40.00%


Sampled: 500 of 1000, 50.00%


Sampled: 600 of 1000, 60.00%


Sampled: 700 of 1000, 70.00%


Sampled: 800 of 1000, 80.00%


Sampled: 900 of 1000, 90.00%


Sampled: 1000 of 1000, 100.00%

Predicted samples and summary statistics are given.

nBurn = 1000 . Iterations = 2000 .

Elapsed time: 0.56 Sec.

from bmstdr.

Hallo951 avatar Hallo951 commented on June 20, 2024

I have found the solution or the error. Your tip with the string - package has helped me very well. I have taken out all times one after the other and so I could identify the package which causes the error. It was the package "formula.tools" which overwrites the base paste() function. If I take out this package and restart r then the prediction works without error.

Here again the code:

    rm(list=ls())
    library(bmstdr)
    library(spTimer)

    set.seed(11)
    s <- sort(sample(unique(nysptime$s.index), size = floor((length(unique(nysptime$s.index))/100)*20)))
    DataFit <- spT.subset(data = nysptime, var.name = "s.index", s = s, reverse = T)
    DataValPred <- spT.subset(data = nysptime, var.name = "s.index", s = s, reverse = F)
    mod <- Bsptime(package = "spTimer",
               model = "GPP", 
               formula = as.formula(y8hrmax ~ xmaxtemp + xwdsp + xrh), 
               data = DataFit, 
               n.report = 5, 
               coordtype = "utm", 
               coords = 4:5, 
               scale.transform = "NONE", 
               g_size = 4,
               N = 2000, 
               mchoice = F,
               plotit = F)

# without error
pred.gp <- predict(mod$fit, tol.dist=0.0, newdata = DataValPred, newcoords = ~ Longitude + Latitude)

# with error if the package "formula.tools" is loaded
if(!require(formula.tools)){install.packages("formula.tools");library(formula.tools)}
pred.gp <- predict(mod$fit, tol.dist=0.0, newdata = DataValPred, newcoords = ~ Longitude + Latitude)

Great, then one error would be already done :=)

Thanks for the help. Because of the other things I write to the maintrainer.

from bmstdr.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.