Unused arguments (length(X1), length(X2)) - r

I have used this code in R. When plotting it, I get an error message.
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
Error in matrix(as.numeric(y_grid), length(X1), length(X2)) :
unused arguments (length(X1), length(X2))

Related

Is it possible to use "summarise" function by indicating a variable name in a bracket format?

I encountered a problem while programming a function to calculate weighted mean and standard deviation (error message: "Error in summarise_impl(.data, dots) : Evaluation error: non-numeric argument to binary operator").
For reproducible data,
set.seed(1)
sample<-as.data.frame(cbind(treat, weight))
treat<-c(rep(0, 10), rep(1, 10))
weight<-runif(20)
for (i in 1:nrow(sample)){
sample$Y1[i]<-sample$treat[i] + rnorm(1, mean = 0, sd = 1)
sample$Y2[i]<- -sample$treat[i] + rnorm(1, mean = 0, sd = 1)
}
sample[3, "Y1"]<-NA
sample[5, "weight"]<-NA
sample[1, "weight"]<1000
And, the code for the function I am working is as follows.
func<-function(data.name, varlist, cond.var, relation, comp.value, gen.var){
varlist.de<-unlist(strsplit(varlist, "[+]"))
### This is where I ecnountered the problem
for (i in 1:nrow(data.name)){
for (j in 1:(length(varlist.de)-2)){
df_weighted<-
data.name %>%
summarise(weighted_mean = wt.mean(varlist.de[j+2], varlist.de[j+1]),
weighted_sd = wt.sd(varlist.de[j+2], varlist.de[j+1]))
}
}
return(data.name)
}
## I ran this code, then the error occured.
func(data.name=sample,
varlist="treat+weight+Y1+Y2",
cond.var="weight",
relation = "!=",
comp.value = "NA",
My_New_Index1)
Specifically, "df_weighted<-data.name %>% group_by (TT) %>% summarise(weighted_mean = wt.mean(varlist.de[j+2], varlist.de[j+1]), weighted_sd = wt.sd(varlist.de[j+2], varlist.de[j+1]))" seems to generate the error message that "Error in summarise_impl(.data, dots) : Evaluation error: non-numeric argument to binary operator".
Any suggestion to fix the error will be appreciated. Thanks.

Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90

it just reports:
Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90.
i don't know why.
x.m <- data.matrix(train[,c(1:43)])
x.m [is.na(x.m)] <- 0
y.m <- train$NPL
cv.m <-data.matrix(cv)
set.seed(356)
cvfit.m.lasso = cv.glmnet(x.m, y.m,
family = "binomial",
alpha = 1,
type.measure = "class")
par(mfrow=c(1,2))
plot(cvfit.m.lasso, main = "Lasso")
coef(cvfit.m.lasso, s = "lambda.min")
predTrain.M = predict(cvfit.m.lasso, newx=cv.m, type="class")
table(cv$NPL, predTrain.M)
It is fixed for me now. It was because I forgot to remove my Target variable from the test data , and that is why i was getting this error.
predict(cvFit, newx= x1[1:5], s= "lambda.min")
Error in cbind2(1, newx) %*% nbeta :
Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90
View(x1)
predict(cvFit, newx= x1[1:5,], s= "lambda.min")
Maybe it's because your newx didn't include all your preditictive variables used to build your model.
I change my code newx = x[1:5] to newx = x[1:5,], and it worked.
Give it a try?

How to make topic modelling?

I was trying my hand at topic modeling for the first time. Tried running the vignette code but getting following errors;
library(pacman)
p_load("tm", "SnowballCC", "RColorBrewer", "ggplot2", "wordcloud", "biclust",
"cluster", "igraph", "fpc", "Rcampdf")
p_load("topicmodels", "devtools", "ldatuning")
data("AssociatedPress", package="topicmodels")
dtm <- AssociatedPress[1:10, ]
result <- FindTopicsNumber(
dtm,
topics = seq(from = 2, to = 15, by = 1),
metrics = c("Griffiths2004", "CaoJuan2009", "Arun2010", "Deveaud2014"),
method = "Gibbs",
control = list(seed = 77),
mc.cores = 2L,
verbose = TRUE
)
knitr::kable(result)
Error in subset.default(values, select = 2:ncol(values)) :
argument "subset" is missing, with no default
FindTopicsNumber_plot(result)
Error in seq_len(m) : argument must be coercible to non-negative integer
In addition: Warning messages:
1: In rep(digits, length.out = m) :
first element used of 'length.out' argument
2: In seq_len(m) : first element used of 'length.out' argument
Need help in resolving the error. Thank you.

Unused argument error in EpiR

I'm getting an unused argument error in the following code:
epi.tests(dat4, verbose = TRUE)
# Error in epi.tests(dat4, verbose = TRUE) :
# unused argument (verbose = TRUE)
What could be causing this and how could I fix the issue?
Thanks
Look at ?epiR::epi.tests.
The Usage section indicates that the function should be used as:
epi.tests(dat, conf.level = 0.95)
There is no verbose argument to that function - it only accepts dat (to which you are passing dat4, and conf.level (whose default is 0.95).
Try again with epi.tests(dat4).

Using your own model in train (caret package)?

I am trying to use train from Caret with a package which is not included, and I get an error that I don't manage to figure out, any idea ? I used the following link to get started
bmsMeth<-list(type="Regression",library="BMS",loop=NULL,prob=NULL)
prm<-data.frame(parameter="mprior.size",class="numeric",label="mprior.size")
bmsMeth$parameters<-prm
bmsGrid<-function(x,y,len=NULL){
out<-expand.grid(mprior.size=seq(2,3,by=len))
out
}
bmsMeth$grid<-bmsGrid
bmsFit<-function(x,y,param, lev=NULL) {bms(cbind(y,x),burn=5000,iter=100000,nmodel=1000,mcmc="bd",g="UIP",mprior.size=param$mprior.size)}
bmsMeth$fit<-bmsFit
bmsPred<-function(modelFit,newdata,preProcess=NULL,submodels=NULL){predict(modelFit,newdata)}
bmsMeth$predict<-bmsPred
library(caret)
data.train<-data.frame(runif(100),runif(100),runif(100),runif(100),runif(100))#synthetic data for testing
bms(cbind(data.train[,1],data.train[,-1]),burn=5000,iter=100000,nmodel=1000,mcmc="bd",g="UIP",mprior.size=2)#function out of caret is working
preProcess=c('center','scale')
myTimeControl <- trainControl(method = "timeslice",initialWindow = 0.99*nrow(data.train), horizon = 1, fixedWindow = FALSE)
tune <- train(data.train[,-1],data.train[,1],preProcess=preProcess,method = bmsMeth,tuneLength=2,metric= "RMSE",trControl =myTimeControl,type="Regression")
Error I get :
Error in train.default(data.train[, -1], data.train[, 1], preProcess =
preProcess, : Stopping In addition: Warning messages: 1: In
eval(expr, envir, enclos) : model fit failed for Training1:
mprior.size=2 Error in method$fit(x = x, y = y, wts = wts, param =
tuneValue, lev = obsLevels, : unused arguments (wts = wts, last =
last, classProbs = classProbs, type = "Regression")
2: In nominalTrainWorkflow(x = x, y = y, wts = weights, info =
trainInfo, : There were missing values in resampled performance
measures.
Apparantly, I just had to put the arguments in the function even if I never use them :
bmsFit<-function(x,y,param, lev=NULL, last, weights, classProbs, ...) {bms(data.frame(y,x),burn=5000,iter=100000,nmodel=1000,mcmc="bd",g="UIP",mprior.size=param$mprior.size)}
Your function bms() does not seem to exist ...

Resources