raster.kendall function on spatialEco not EnvStats package - r
Recently I came across over this library in R spatialEco. I want to calculate the Kendall tau statistic for a raster stack in R. but I get an Error to "install EnvStats package before running this function",
when I install this package, then get another Error :"Error in (function (y, x = seq(along = y), alternative = "two.sided", :
When ci.slope=TRUE, there must be at least 3 non-missing, finite observations"
> files_Bio1 <- list.files(path="e:/Pastclim/trend/Bio1/", pattern='asc$', full.names=TRUE)
> Current_Bio2 <- stack(files_Bio2)
> names(Current_Bio2)
[1] "Bio1_370Kbp" "Bio1_380Kbp" "Bio1_390Kbp" "Bio1_400Kbp" "Bio1_410Kbp"
[6] "Bio1_420Kbp"
> library(spatialEco)
> m_Bio1 <- spatialEco::raster.kendall(Current_Bio2, intercept = FALSE, p.value = FALSE, confidence = FALSE,
+ tau = FALSE)
Error in spatialEco::raster.kendall(Current_Bio2, intercept = FALSE, p.value = FALSE, :
please install EnvStats package before running this function
Related
Function passes test inside the package and fails from vignette (due to cpp_object_initializer)
I am writing an R package that uses rstan for Bayesian sampling. (Here is the specific commit if you want to reproduce the issue.) I only succeed in running a function that calls rstan from the vignette if I use library(rstan) in the vignette, and not with a few workarounds. The setup A function in the package calls rstan (edited for clarity): #' #importFrom Rcpp cpp_object_initializer #' #export run_variational_bayes <- function(x, y, output_samples, beta_sd, stan_file) { n_input <- length(y) p <- ncol(x) train_dat <- list(n = n_input, p = p, x = x, y = y, beta_sd = beta_sd) stan_model <- rstan::stan_model(file = stan_file) stan_vb <- rstan::vb(object = stan_model, data = train_dat, output_samples = output_samples) return(rstan::extract(stan_vb)$beta) } I test this function in the package: context("RStan variational Bayes model") test_that("Rstan variational Bayes model runs", { german <- PosteriorBootstrap::get_german_credit_dataset() n_bootstrap <- 10 prior_variance <- 100 stan_vb_sample <- PosteriorBootstrap::run_variational_bayes(x = german$x, y = german$y, output_samples = n_bootstrap, beta_sd = sqrt(prior_variance), iter = 10) expect_true(nrow(stan_vb_sample) == n_bootstrap) expect_true(ncol(stan_vb_sample) == ncol(german$x)) }) The tests pass locally and on Travis, so the function works from inside the package. The problem The vignette code works if I include library(rstan): library(rstan) prior_sd <- 10 n_bootstrap <- 1000 german <- PosteriorBootstrap::get_german_credit_dataset() stan_vb_sample <- PosteriorBootstrap::run_variational_bayes(x = german$x, y = german$y, output_samples = n_bootstrap, beta_sd = prior_sd) dim(stan_vb_sample) #> [1] 1000 25 but I see it as bad practice that the user needs to attach another package to use my package. If I use requireNamespace(), building the vignette works but the Stan model does not run: requireNamespace("PosteriorBootstrap", quietly = TRUE) # ... stan_vb_sample <- PosteriorBootstrap::run_variational_bayes(x = german$x, y = german$y, output_samples = n_bootstrap, beta_sd = prior_sd) #> Error in cpp_object_initializer(.self, .refClassDef, ...) : #> could not find function "cpp_object_initializer" #> failed to create the model; variational Bayes not done #> Stan model 'bayes_logit' does not contain samples. dim(stan_vb_sample) #> NULL Note that I used #' #importFrom Rcpp cpp_object_initializer in the Roxygen comment, which should import the function that rstan says is missing. Comparison with another package that uses rstan This package has similar values in DESCRIPTION, yet I tested that it does not require library(rstan) to run rstan. It uses #import Rcpp in one function, which I tested with my package by replacing #importFrom Rcpp cpp_object_initializer in front of the function and got the same error. Failed workarounds The difference between requireNamespace() and library() is that the latter imports the namespace of the package into the current environment. But rstan does import(Rcpp) so that object should be available. (1) I tried library("PosteriorBootstrap") in the vignette, since the package imports that object into its namespace: I got the same error (with #import Rcpp or with #importFrom Rcpp cpp_object_initializer). (2) I copied that object to the environment of the function: requireNamespace("Rcpp", quietly = TRUE) #' #import Rcpp #' #export run_variational_bayes <- function(x, y, output_samples, beta_sd, stan_file = get_stan_file(), iter = 10000, seed = 123, verbose = FALSE) { cpp_object_initializer <- Rcpp:cpp_object_initializer # ... } and I was surprised to get a vignette error: E creating vignettes (1.8s) Quitting from lines 151-157 (anpl.Rmd) Error: processing vignette 'anpl.Rmd' failed with diagnostics: object 'Rcpp' not found Execution halted Temporary solution As a temporary solution, I moved the code in the function to the vignette entirely. The vignette fails with requireNamespace(): requireNamespace("rstan") #> Loading required namespace: rstan prior_sd <- 10 n_bootstrap <- 1000 german <- PosteriorBootstrap::get_german_credit_dataset() train_dat <- list(n = length(german$y), p = ncol(german$x), x = german$x, y = german$y, beta_sd = prior_sd) stan_file <- PosteriorBootstrap::get_stan_file() stan_model <- rstan::stan_model(file = stan_file) stan_vb <- rstan::vb(object = stan_model, data = train_dat, seed = seed, output_samples = n_bootstrap) #> Error in cpp_object_initializer(.self, .refClassDef, ...) : #> could not find function "cpp_object_initializer" #> failed to create the model; variational Bayes not done stan_vb_sample <- rstan::extract(stan_vb)$beta #> Stan model 'bayes_logit' does not contain samples. dim(stan_vb_sample) #> NULL and succeeds with library(rstan): library("rstan") #> Loading required package: ggplot2 # ... stan_model <- rstan::stan_model(file = stan_file) stan_vb <- rstan::vb(object = stan_model, data = train_dat, seed = seed, output_samples = n_bootstrap) #> Chain 1: ------------------------------------------------------------ # ... #> Chain 1: COMPLETED. stan_vb_sample <- rstan::extract(stan_vb)$beta dim(stan_vb_sample) #> [1] 1000 25 In moving the code out of the package, I realised that a test that uses library("rstan") and calls the rstan package directly, e.g. context("Adaptive non-parametric learning function") library("rstan") # ... test_that("Adaptive non-parametric learning with posterior samples works", { german <- get_german_credit_dataset() n_bootstrap <- 100 # Get posterior samples seed <- 123 prior_sd <- 10 train_dat <- list(n = length(german$y), p = ncol(german$x), x = german$x, y = german$y, beta_sd = prior_sd) stan_model <- rstan::stan_model(file = get_stan_file()) stan_vb <- rstan::vb(object = stan_model, data = train_dat, seed = seed, output_samples = n_bootstrap) stan_vb_sample <- rstan::extract(stan_vb)$beta # ... } passes the tests inside the package: ✔ | 24 | Adaptive non-parametric learning function [53.1 s] ══ Results ═════════════════════════════════════════════════════════════════════ Duration: 53.2 s OK: 24 Failed: 0 Warnings: 0 Skipped: 0 but the same test with requireNamespace("rstan") fails them: ⠋ | 21 | Adaptive non-parametric learning functionError in cpp_object_initializer(.self, .refClassDef, ...) : could not find function "cpp_object_initializer" Stan model 'bayes_logit' does not contain samples. ... ══ Results ═════════════════════════════════════════════════════════════════════ Duration: 51.7 s OK: 22 Failed: 1 Warnings: 0 Skipped: 0 Conclusion I wonder if rstan code is calling cpp_object_initializer without a qualifier, and if it's doing that in a new environment that does not inherit the objects from the calling environment. I acknowledge that I did not use rstantools to start the package (my employer decided to stick with the MIT license and chose not to restart the package structure from scratch) and that I am compiling the model at call time. I suppose that users providing their own model would face the same errors when using requireNamespace() instead of library(). How can I allow users to run package functions that call rstan without library(rstan), short of restarting the package from scratch with rstantools?
Error in gam function in R
I have a problem with my R code about a multiple linear regression. First, I try to use the gam function but this gives me an error. Here is the code: install.packages("nlme") library("mgcv") library("ggplot2") #Import dataset setwd("/Users/Sarah/Documents/Master T&O/Master 1/Statistics IV/Assignment 2 ") weight_data = read.csv("WeightLossGroup190.csv", sep = "", dec = ".", header = TRUE) #Name of used data weight <- weight_data$weight date <- weight_data$date dayNr <- weight_data$dayNumber time <- weight_data$time #Check linearity gam1 <- gam(as.numeric(weight_data$weight) ~ s(as.numeric(weight_data$dayNumber)) + s(as.numeric(weight_data$time))) summary(gam1) plot.gam(gam1, se = FALSE, rug = TRUE, all.terms = TRUE) This gives me the following error: Error in smooth.construct.tp.smooth.spec(object, dk$data, dk$knots) : A term has fewer unique covariate combinations than specified maximum degrees of freedom Does anyone have an idea of what I'm doing wrong?
You might want to try controlling the number of knots with "k = " in the gam.
mcmc modeling in r by metropolis hastings
I am trying to modeling mcmc by using mhadaptive package in R. But one error appear. What should I do? #importing data from excel q<-as.matrix(dataset1) #input data from spread price F1<-as.matrix(F_1_) #input data from F li_reg<-function(pars,data) #defining function { a01<-pars[1] #defining parameters a11<-pars[2] epsilon<-pars[3] b11<-pars[4] a02<-pars[5] a12<-pars[6] b12<-pars[7] v<-pars[8] pred<-((a01+a11*epsilon^2+b11)+F1[,2]*(a02+a12*epsilon^2+b12)) #parametes which exist here should be optimize by cinsidering this formula log_likelihood<-sum(dnorm(data[,2],pred,log = TRUE)) prior<-prior_reg(pars) return(log_likelihood+prior) } prior_reg<-function(pars) #here there is prior values { epsilon<-pars[3] v<-pars[8] prior_epsilon<-pt(0.85,5,lower.tail = TRUE,log.p = FALSE) } mcmc_r<-Metro_Hastings(li_func = li_reg,pars =NULL,prop_sigma = NULL,par_names = c('a01','a11','epsilon','b11','a02','a12','b12'),data=q,iterations = 2000,burn_in = 1000,adapt_par = c(100,20,0.5,0.75),quiet = FALSE) mcmc_r<-mcmc_thin(mcmc_r) I used mhadaptive package for calculating optimized parameters. But this error eppear Error in optim(pars, li_func, control = list(fnscale = -1), hessian = TRUE, : function cannot be evaluated at initial parameters
Plot a Neural Net Curve Using neuralnet and ROCR package
Here I have a classification task and I need to use neuralnet and ROCR packages. The problem is that I got the error messages when I use prediction function. Here is my code: #load packages require(neuralnet) library(ROCR) #create data set train<-read.table(file="train.txt",header=TRUE,sep=",") test<- read.table(file="test.txt",header=TRUE,sep=",") #build model and make predictions nn.sag <- neuralnet(Type ~ Area+Perimeter+Compactness+Length+Width+Asymmetry+Groove, data = train, hidden = 5, algorithm = "sag", err.fct = "sse", linear.output = FALSE) prob = compute(nn.sag, test[, -ncol(test)] ) prob.result <- prob$net.result nn.pred = prediction(prob.result, test$Type) pref <- performance(nn.pred, "tpr", "fpr") plot(pref) And here I got the error message for the 'prediction' function: '$ operator is invalid for atomic vectors' The dataset looks like (only training dataset here): Area,Perimeter,Compactness,Length,Width,Asymmetry,Groove,Type 14.8,14.52,0.8823,5.656,3.288,3.112,5.309,1 14.79,14.52,0.8819,5.545,3.291,2.704,5.111,1 14.99,14.56,0.8883,5.57,3.377,2.958,5.175,1 19.14,16.61,0.8722,6.259,3.737,6.682,6.053,0 15.69,14.75,0.9058,5.527,3.514,1.599,5.046,1 14.11,14.26,0.8722,5.52,3.168,2.688,5.219,1 13.16,13.55,0.9009,5.138,3.201,2.461,4.783,1 16.16,15.33,0.8644,5.845,3.395,4.266,5.795,0 15.01,14.76,0.8657,5.789,3.245,1.791,5.001,1 14.11,14.1,0.8911,5.42,3.302,2.7,5,1 17.98,15.85,0.8993,5.979,3.687,2.257,5.919,0 21.18,17.21,0.8989,6.573,4.033,5.78,6.231,0 14.29,14.09,0.905,5.291,3.337,2.699,4.825,1 14.59,14.28,0.8993,5.351,3.333,4.185,4.781,1 11.42,12.86,0.8683,5.008,2.85,2.7,4.607,1 12.11,13.47,0.8392,5.159,3.032,1.502,4.519,1 15.6,15.11,0.858,5.832,3.286,2.725,5.752,0 15.38,14.66,0.899,5.477,3.465,3.6,5.439,0 18.94,16.49,0.875,6.445,3.639,5.064,6.362,0 12.36,13.19,0.8923,5.076,3.042,3.22,4.605,1 14.01,14.29,0.8625,5.609,3.158,2.217,5.132,1 17.12,15.55,0.8892,5.85,3.566,2.858,5.746,0 15.78,14.91,0.8923,5.674,3.434,5.593,5.136,1 16.19,15.16,0.8849,5.833,3.421,0.903,5.307,1 14.43,14.4,0.8751,5.585,3.272,3.975,5.144,1 13.8,14.04,0.8794,5.376,3.155,1.56,4.961,1 14.46,14.35,0.8818,5.388,3.377,2.802,5.044,1 18.59,16.05,0.9066,6.037,3.86,6.001,5.877,0 18.75,16.18,0.8999,6.111,3.869,4.188,5.992,0 15.49,14.94,0.8724,5.757,3.371,3.412,5.228,1 12.73,13.75,0.8458,5.412,2.882,3.533,5.067,1 13.5,13.85,0.8852,5.351,3.158,2.249,5.176,1 14.38,14.21,0.8951,5.386,3.312,2.462,4.956,1 14.86,14.67,0.8676,5.678,3.258,2.129,5.351,1 18.45,16.12,0.8921,6.107,3.769,2.235,5.794,0 17.32,15.91,0.8599,6.064,3.403,3.824,5.922,0 20.2,16.89,0.8894,6.285,3.864,5.173,6.187,0 20.03,16.9,0.8811,6.493,3.857,3.063,6.32,0 18.14,16.12,0.8772,6.059,3.563,3.619,6.011,0 13.99,13.83,0.9183,5.119,3.383,5.234,4.781,1 15.57,15.15,0.8527,5.92,3.231,2.64,5.879,0 16.2,15.27,0.8734,5.826,3.464,2.823,5.527,1 20.97,17.25,0.8859,6.563,3.991,4.677,6.316,0 14.16,14.4,0.8584,5.658,3.129,3.072,5.176,1 13.45,14.02,0.8604,5.516,3.065,3.531,5.097,1 15.5,14.86,0.882,5.877,3.396,4.711,5.528,1 16.77,15.62,0.8638,5.927,3.438,4.92,5.795,0 12.74,13.67,0.8564,5.395,2.956,2.504,4.869,1 14.88,14.57,0.8811,5.554,3.333,1.018,4.956,1 14.28,14.17,0.8944,5.397,3.298,6.685,5.001,1 14.34,14.37,0.8726,5.63,3.19,1.313,5.15,1 14.03,14.16,0.8796,5.438,3.201,1.717,5.001,1 19.11,16.26,0.9081,6.154,3.93,2.936,6.079,0 14.52,14.6,0.8557,5.741,3.113,1.481,5.487,1 18.43,15.97,0.9077,5.98,3.771,2.984,5.905,0 18.81,16.29,0.8906,6.272,3.693,3.237,6.053,0 13.78,14.06,0.8759,5.479,3.156,3.136,4.872,1 14.69,14.49,0.8799,5.563,3.259,3.586,5.219,1 18.85,16.17,0.9056,6.152,3.806,2.843,6.2,0 12.88,13.5,0.8879,5.139,3.119,2.352,4.607,1 12.78,13.57,0.8716,5.262,3.026,1.176,4.782,1 14.33,14.28,0.8831,5.504,3.199,3.328,5.224,1 19.46,16.5,0.8985,6.113,3.892,4.308,6.009,0 19.38,16.72,0.8716,6.303,3.791,3.678,5.965,0 15.26,14.85,0.8696,5.714,3.242,4.543,5.314,1 20.24,16.91,0.8897,6.315,3.962,5.901,6.188,0 19.94,16.92,0.8752,6.675,3.763,3.252,6.55,0 20.71,17.23,0.8763,6.579,3.814,4.451,6.451,0 16.17,15.38,0.8588,5.762,3.387,4.286,5.703,0 13.02,13.76,0.8641,5.395,3.026,3.373,4.825,1 16.53,15.34,0.8823,5.875,3.467,5.532,5.88,0 13.89,14.02,0.888,5.439,3.199,3.986,4.738,1 18.98,16.57,0.8687,6.449,3.552,2.144,6.453,0 17.08,15.38,0.9079,5.832,3.683,2.956,5.484,1 15.03,14.77,0.8658,5.702,3.212,1.933,5.439,1 16.14,14.99,0.9034,5.658,3.562,1.355,5.175,1 18.65,16.41,0.8698,6.285,3.594,4.391,6.102,0 20.1,16.99,0.8746,6.581,3.785,1.955,6.449,0 17.99,15.86,0.8992,5.89,3.694,2.068,5.837,0 15.88,14.9,0.8988,5.618,3.507,0.7651,5.091,1 13.22,13.84,0.868,5.395,3.07,4.157,5.088,1 18.3,15.89,0.9108,5.979,3.755,2.837,5.962,0 19.51,16.71,0.878,6.366,3.801,2.962,6.185,0
The prediction() function is available in both neuralnet and ROCR package in R. So do not load both packages together. First load neuralnet, train your model and then detach it using detach() and then load ROCR package. Try following code: #load packages require(neuralnet) #create data set train<-read.table(file="train.txt",header=TRUE,sep=",") test<- read.table(file="test.txt",header=TRUE,sep=",") #build model and make predictions nn.sag <- neuralnet(Type ~ Area+Perimeter+Compactness+Length+Width+Asymmetry+Groove, data = train, hidden = 5, algorithm = "sag", err.fct = "sse", linear.output = FALSE) prob = compute(nn.sag, test[, -ncol(test)] ) prob.result <- prob$net.result detach(package:neuralnet,unload = T) library(ROCR) nn.pred = prediction(prob.result, test$Type) pref <- performance(nn.pred, "tpr", "fpr") plot(pref)
Or just simply use ROCR::prediction(prediction(prob.result, test$Type)) For selecting the right package.
Error: object not found - cor.ci
I'm trying to use cor.ci to obtain polychoric correlations with significance tests, but it keeps giving me an error message. Here is the code: install.packages("Hmisc") library(Hmisc) mydata <- spss.get("S-IAT for R.sav", use.value.labels=TRUE) install.packages('psych') library(psych) poly.example <- cor.ci(mydata(nvar = 10,n = 100)$items,n.iter = 10,poly = TRUE) poly.example print(corr.test(poly.example$rho), short=FALSE) Here is the error message it gives: > library(psych) > poly.example <- cor.ci(mydata(nvar = 10,n = 100)$items,n.iter = 10,poly = TRUE) Error in cor.ci(mydata(nvar = 10, n = 100)$items, n.iter = 10, poly = TRUE) : could not find function "mydata" > poly.example Error: object 'poly.example' not found > print(corr.test(poly.example$rho), short=FALSE) Error in is.data.frame(x) : object 'poly.example' not found How can I make it recognize mydata and/or select certain variables from this dataset for the analysis? I got the above code from here: Polychoric correlation matrix with significance in R Thanks!
You have several problems. 1) As previously commented upon, you are treating mydata as a function, but you need to treat it as a data.frame. Thus the call should be poly.example <- cor.ci(mydata,n.iter = 10,poly = TRUE) If you are trying to just get the first 100 cases and the first 10 variables, then poly.example <- cor.ci(mydata[1:10,1:100],n.iter = 10,poly = TRUE) 2) Then, you do not want to run corr.test on the resulting correlation matrix. corr.test should be run on the data. print(corr.test(mydata[1:10,1:100],short=FALSE) Note that corr.test is testing the Pearson correlation.