Wavelet function: rewrite matlab wavelet code to R - r

I am having matlab code:
wt = modwt(datavalue,'db1',7);
I want to conver this code to R, which I have done in following manner:
wt = modwt(datavalue,wf = 'db2',n.levels = 7)
But this keeps giving me error
Error in modwt(datavalue, wf = "db2", n.levels = 7) :
unused argument (wf = "db2")
Also if I write the code like this
wt = modwt(datavalue,'db2',7)
It gives me the following error
Error in wt.filter(filter, modwt = TRUE) : Invalid filter name.
I am using the wavelet function and probably I am not able to understand the wavelet function in R.
Is this the right of of transforming the code to R or is there any other package in R that would be better than wavelet package

I'm not fully familiar with the MATLAB function modwt. However, you can try the R package waveslim, and the function modwt.
It looks the same, see the documentation.
require(waveslim)
data(ibm)
ibm.returns <- diff(log(ibm))
ibmrWave <- modwt(ibm.returns, wf = "la8", n.levels = 4)

Related

renewalCount() function in countr package in R

I am trying to use renewalCount() function in R to get the fitted model of Weibull Distribution. Here is my piece of code:
wei <- renewalCount(formula = packets ~ 1,
data = dfdata,
dist = "weibull", weiMethod = "series_acc",
computeHessian = FALSE,
control = renewal.control(trace = 0))
I am facing the following exception while running this:
error: Mat::init(): requested size is too large; suggest to enable ARMA_64BIT_WORD
Error in optimx.check(par, optcfg$ufn, optcfg$ugr, optcfg$uhess, lower, :
Cannot evaluate function at initial parameters
I am a newbie in the field of R programming and have tried solutions like importing optimx and RcppArmadillo library but it did not work. Is there a way to enable ARMA_64BIT_WORD in RStudio. Any help would be really appreciated. Thanks in advance!

Error in eval(parse()) - r unable to find argument input

I am very new to R, and this is my first time of encountering the eval() function. So I am trying to use the med and boot.med function from the following package: mma. I am using it to conduct mediation analysis. med and boot.med take in models such as linear models, and dataframes that specify mediators and predictors and then estimate the mediation effect of each mediator.
The author of the package gives the flexible option of specifying one's own custom.function. From the source code of med, it can be seen that the custom.function is passed to the eval(). So I tried insert the gbmt function as the custom function. However, R kept giving me error message: Error during wrapup: Number of trees to be used in prediction must be provided. I have been searching online for days and tried many ways of specifying the number of trees parameter n.trees, but nothing works (I believe others have raised similar issues: post 1, post 2).
The following codes are part of the source code of the med function:
cf1 = gsub("responseY", "y[,j]", custom.function[j])
cf1 = gsub("dataset123", "x2", cf1)
cf1 = gsub("weights123", "w", cf1)
full.model[[j]] <- eval(parse(text = cf1))
One custom function example the author gives in the package documentation is as follows:
temp1<-med(data=data.bin,n=2,custom.function = 'glm(responseY~.,data=dataset123,family="quasibinomial",
weights=weights123)')
Here the glm is the custom function. This example code works and you can replicate it easily (if you have mma installed and loaded). However when I am trying to use the gbmt function on a survival object, I got errors and here is what my code looks like:
temp1 <- med(data = data.surv,n=2,type = "link",
custom.function = 'gbmt(responseY ~.,
data = dataset123,
distribution = dist,
train_params = start_stop,
cv_folds=10,
keep_gbm_data = TRUE,
)')
Anyone has any idea how the argument about number of trees n.trees can be added somewhere in the above code?
Many thanks in advance!
Update: in order to replicate the example code, please install mma and try the following:
library("mma")
data("weight_behavior") ##binary x #binary y
x=weight_behavior[,c(2,4:14)]
pred=weight_behavior[,3]
y=weight_behavior[,15]
data.bin<-data.org(x,y,pred=pred,contmed=c(7:9,11:12),binmed=c(6,10), binref=c(1,1),catmed=5,catref=1,predref="M",alpha=0.4,alpha2=0.4)
temp1<-med(data=data.bin,n=2) #or use self-defined final function
temp1<-med(data=data.bin,n=2, custom.function = 'glm(responseY~.,data=dataset123,family="quasibinomial",
weights=weights123)')
I changed the custom.function to gbmt and used a survival object as responseY and the error occurs. When I use the gbmt function on my data outside the med function, there is no error.

R mlogit package: use LAPACK instead of LINPACK

I am estimating a fairly simple McFadden choice model using a very large data set (101.6 million unit-alternatives). I can estimate this model just fine in Stata using the asclogit command, but when I try to use the mlogit package in R, I get the following error:
region1 <- mlogit(chosen ~ mean_log.wage + mean_log.rent + bornNear + Dim.1 + regionFE | 0,
shape= "long", chid.var = "chid", alt.var = "alternatives", data = ready)
Error in qr.default(na.omit(X)) : too large a matrix for LINPACK
Calls: mlogit ... model.matrix -> model.matrix.mFormula -> qr -> qr.default
If I look at the source code of qr.R it's clear that the number of elements in my design matrix is too big relative to the LINPACK limit of 2,147,483,647. However, no such limit exists for LAPACK (that I can tell, at least).
From qr.R:
qr.default <- function(x, tol = 1e-07, LAPACK = FALSE, ...)
{
x <- as.matrix(x)
if(is.complex(x))
return(structure(.Internal(La_qr_cmplx(x)), class = "qr"))
## otherwise :
if(LAPACK)
return(structure(.Internal(La_qr(x)), useLAPACK = TRUE, class = "qr"))
## else "Linpack" case:
p <- as.integer(ncol(x))
if(is.na(p)) stop("invalid ncol(x)")
n <- as.integer(nrow(x))
if(is.na(n)) stop("invalid nrow(x)")
if(1.0 * n * p > 2147483647) stop("too large a matrix for LINPACK")
...
qr() appears to be called in the mFormula method of mlogit, when model.matrix is being created, and probably while checking NAs. But I can't tell if there is a way to pass LAPACK = TRUE to mlogit, or if there is a way to skip the NA checking.
I'm hoping #YvesCroissant will see this.
As I mentioned, I can estimate this model just fine in Stata, so it's not a question of resources. My Stata license is not portable, however, which is why I would like to use R.
Thanks to Julius' comment and this post on namespaces in R, I figured out the answer. I added the following code right after my library statements:
source("mymFormula.R")
tmpfun <- get("model.matrix.mFormula", envir = asNamespace("mlogit"))
environment(mymFormula) <- environment(tmpfun)
attributes(mymFormula) <- attributes(tmpfun) # don't know if this is really needed
assignInNamespace("model.matrix.mFormula", mymFormula, ns="mlogit")
mymFormula.R is an R script where I copy/pasted the contents of mlogit:::model.matrix.mFormula and added mymFormula <- before the function invocation at the top of the file.
I viewed the contents of mlogit:::model.matrix.mFormula by typing trace(mlogit:::model.matrix.mFormula, edit=TRUE) in RStudio. (Thanks to this answer for help on how to do that.)

Not finding 'weightedMean' object for numFun using kNN in VIM package for R

I'm getting an error stating that the 'weightedMean' argument is not found for the 'numFun' parameter in the kNN imputation function within the VIM R package. I'm attempting to impute data in a fairly large dataset, and I want to use kNN with 5 neighbors using weighted means.
Here is my code:
df.imputed <- kNN(df, variable = c(...), dist_var = c(...), numFun = weightedMean, k = 5, weightDist = TRUE, trace = TRUE, imp_var = TRUE)
And the exact error is:
Error in args(numFun) : object 'weightedMean' not found
Based on the documentation (https://cran.r-project.org/web/packages/VIM/VIM.pdf page 29) it seems like this should work.
Try put this way numFun = weighted.mean,
at least worked for me

Stargazer error with polr in R

I obtain an error when using stargazer in conjunction with polr from the MASS package in R. Here is an example:
library(MASS)
library(stargazer)
# Fake data
set.seed(1234)
fake_data <- data.frame(y = as.factor(sample.int(4, 20, replace = TRUE)),
x1 = rnorm(20, mean = 1, sd = 1),
x2 = rnorm(20, mean = -1, sd = 1))
# Ordered logistic regression
o_log <- MASS::polr(y ~ x1 + x2,
data = fake_data,
Hess = TRUE, method = "logistic")
summary(o_log)
# Create regression table
stargazer(o_log)
I receive the following error message:
% Error: Unrecognized object type.
Does anyone know how to solve this? Thanks in advance.
P.S.: I'm on OS X 10.13, using R 3.4.3, MASS 7.3.47, and stargazer 5.2.
EDIT: According to stargazer's vignette, objects from polr should be supported.
I don't know the reason but when I change MASS::polr into plor, the error is removed and it works fine. It seems that it is a bug of package stargazer.
I encountered the same problem. For some strange reason, this only happens when you call the function using :: (in your case: MASS::polr). It doesn't happen when you first load the package via library(MASS) and then call the specific function.
See: Why do I get different results when using library(MASS) vs. MASS::?
I guess it was because you didn't load the MASS library and instead called the function using ::. MASS library doing some updates on how summary works for polr, which is being used by stargazer to generate the table. By not loading the library, the update was not happened, hence bringing you some trouble with stargazer.

Resources