I ran my twopart model and I have tried following the but the margins and the AME commands to get the marginal effects on all the variables, but I am running into a really weird issue with the data frame for some reason.
> margin(tpmodel1)
Error in `[.data.frame`(newdata, , term) : undefined columns selected
But when looking at the guide for the twopm package, it says that all I need to do is just AME(tpmodel1) or margin(tpmodel1) to get all the marginal effects and it is still giving that error. so then i did
> margin(tpmodel1, newdata = NULL, term = NULL, se=TRUE)
Error in `[.data.frame`(newdata, , term) : undefined columns selected
And
> margin(tpmodel1, newdata = dfasthma, term = NULL, se=TRUE)
Error in margin(tpmodel1, newdata = dfasthma, term = NULL, se = TRUE) :
some of independent variables of the two-part model are not available in the newdata set
of which doesnt make sense cause The data set i created for the variables I am using is dfasthma.
the code from the package:
margin(object, newdata = NULL, term = NULL, value = NULL,
se = TRUE, se.method = c("delta","bootstrap"), CI = TRUE, CI.boots = FALSE,
level = 0.95,eps = 1e-7,na.action = na.pass, iter = 50)
The image attached is a guide to the margin command from the package
Related
I need to estimate some parameters in gp model so I install "kergp" package in R to generate a customized kernel. When I use "mle" function to estimate parameters, I get the error as below:
Error in fitList[[bestIndex]] : attempt to select less than one element in get1index
Here is the code:(where initial_data is a dataframe.)
q4<-q1CompSymm(af, input = "af", cov = "corr", intAsChar = TRUE)
quan<-covTP(k1Fun1 = k1Fun1PowExp,d = 4,cov = "homo",
iso1 = 0,parLower = c(rep(0,9)), parUpper = c(2,2,2,2,Inf,Inf,Inf,Inf,Inf))
inputNames(quan)<-c("hl","hn1","hn2","hn3")
kernel<-covComp(formula = ~quan()*q4())
mle(kernel,
parCovIni = c(rep(0.5,10)),
initial_data$y,select(initial_data,-c('index','y','iw','rts')), F = NULL,
parCovLower = c(rep(0,10)),
parCovUpper = c(2,2,2,2,Inf,Inf,Inf,Inf,Inf,1),
noise = TRUE, varNoiseIni = var(initial_data$y) / 10,
optimFun = "stats::optim",
optimMethod = "BFGS")
I had seen the related document from github(https://github.com/cran/kergp/blob/master/R/methodMLE.R) but still couldn't figure out how this situation happen...
Besides, this error occurred after some iterations if there are any help. Any help would be appreciated. Thanks
I'm setting up a grid search using the catboost package in R. Following the catboost documentation (https://catboost.ai/docs/), the grid search for hyperparameter tuning can be conducted using the 3 separate commands in R,
fit_control <- trainControl(method = "cv", number = 4, classProbs = TRUE)
grid <- expand.grid(depth = c(7,8,9,10), learning_rate = c(0.1,0.2,0.3,0.4), iterations = c(10,100,1000))
report <- train(df.scale, as.factor(make.names(as.matrix(tier1))), method = catboost.caret, logging_level = 'Verbose', preProc = NULL, tuneGrid = grid, trControl = fit_control)
searching across different values for depth, learning rate, and the number of iterations. These commands seem well enough, it's just I can't figure out where to input the option for the task_type = "GPU". Would appreciate any help on how to specify using the GPU for finding the optimal parameters using R.
It can be done the following way:
fit_control <- trainControl(method = "cv", number = 4, classProbs = TRUE)
grid <- expand.grid(depth = c(7,8,9,10), learning_rate = c(0.1,0.2,0.3,0.4), iterations = c(10,100,1000))
report <- train(df.scale, as.factor(make.names(as.matrix(tier1))), method = catboost.caret, logging_level = 'Verbose', preProc = NULL, tuneGrid = grid, trControl = fit_control,
task_type = "GPU")
This works due to ellipsis mechanics. All arguments that are unknown to caret.train itself are eventually passed to catboost.caret$fit and taken as training parameters for catboost. The exact place in catboost code where it happens is here:
...
catboost.caret$fit <- function(x, y, wts, param, lev, last, weights, classProbs, ...) {
param <- c(param, list(...)) # all ellipsis args are taken to param
if (is.null(param$loss_function)) {
...
If you pass an unknown parameter this way, catboost will trigger an error:
report <- train(x, as.factor(make.names(y)),
method = catboost.caret,
logging_level = 'Verbose', preProc = NULL,
tuneGrid = grid, trControl = fit_control, what_is_this = "GPU")
> warnings()
Warning messages:
1: model fit failed for Fold1: depth=4, learning_rate=0.1, l2_leaf_reg=0.001, rsm=1, border_count=64, iterations=100 Error in catboost.train(pool, test_pool, param) :
catboost/private/libs/options/plain_options_helper.cpp:501: Unknown option {what_is_this} with value "GPU"
It looks like you are using the caret package to perform the training. In this case, it looks like the caret package does not pass any additional arguments to the catboost.train function so it may not support the GPU functionality. You can see from the code in caret for this method that the ... argument is not passed to the catboost.train function.
#' Fit model based on input data
#'
#' #param x, y: the current data used to fit the model
#' #param wts: optional instance weights (not applicable for this particular model)
#' #param param: the current tuning parameter values
#' #param lev: the class levels of the outcome (or NULL in regression)
#' #param last: a logical for whether the current fit is the final fit
#' #param weights: weights
#' #param classProbs: a logical for whether class probabilities should be computed
#'
#' #noRd
catboost.caret$fit <- function(x, y, wts, param, lev, last, weights, classProbs, ...) {
param <- c(param, list(...))
if (is.null(param$loss_function)) {
param$loss_function <- "RMSE"
if (is.factor(y)) {
param$loss_function <- "Logloss"
if (length(lev) > 2) {
param$loss_function <- "MultiClass"
}
y <- as.double(y) - 1
}
}
test_pool <- NULL
if (!is.null(param$test_pool)) {
test_pool <- param$test_pool
if (class(test_pool) != "catboost.Pool")
stop("Expected catboost.Pool, got: ", class(test_pool))
param <- within(param, rm(test_pool))
}
pool <- catboost.from_data_frame(x, y, weight = wts)
model <- catboost.train(pool, test_pool, param)
model$lev <- lev
return(model)
}
Depending on your level of proficiency in R and caret, you can add your own model to caret by basically copying the model in the caret github location and modify it to accept the GPU argument which should go into the parameter list per their documentation
I followed this guideline for creating my own caret model Creating Your Own Model. There it states that
If a regression model is being used or if the classification model
does not create class probabilities a value of NULL can be used here
instead of a function
and so I do that
# Define the model cFBasic
cFBasic <- list(type = "Regression",
library = c("lubridate", "stringr"),
loop = NULL)
...
cFBasic$prob <- NULL
cFBasic$sort <- NULL
However, when I attempt testing the model the following error is produced:
control <- trainControl(method = "cv",
number = 10,
p = .9,
allowParallel = TRUE)
fit <- train(x = calib_set,
y = calib_set$y,
method = cFBasic,
trControl = control)
Error: some required components are missing: prob
How can I fix that? other than adding the function prob to generate a fake pro data frame to make caret happy.
By typing cFBasic$prob <- NULL, you are not actually adding a new item to your list.
Look at this:
cFBasic <- list(prob = NULL)
cFBasic
#> $prob
#> NULL
cFBasic$prob <- NULL
cFBasic
#> named list()
When you assign NULL to an object of a list, you delete that object. If you want to add a NULL object called prob and one NULL object called sort to a list you should type this way:
# Define the model cFBasic
cFBasic <- list(type = "Regression",
library = c("lubridate", "stringr"),
loop = NULL)
...
cFBasic <- c(cFBasic, list(prob = NULL))
cFBasic <- c(cFBasic, list(sort = NULL))
Have a try.
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
I'm trying to create an AICc table for a number of multinomial logistic regression models. However, when I try to run the line:
aictab(cand.set = Cand.mod, modnames = Modnames)
I get the error message:
Error in print(aictab(cand.set = Cand.models, modnames = Modnames)) :
could not find function "aictab"
The script I am using is:
install.packages("AICcmodavg")
dd$reaction2 <- relevel(dd$reaction, ref="nr")
Cand.mod<-list()
Cand.mod[[1]]<-multinom(reaction2~ camera + light, data = dd)
Cand.mod[[2]]<-multinom(reaction2~ camera + gender, data = dd)
Modnames <-c("fm1", "fm2", "fm3")
aictab(cand.set = Cand.mod, modnames = Modnames)
evidence(aictab(cand.set = Cand.mod, modnames = Modnames))
confset(cand.set = Cand.mod, modnames = Modnames, method= "raw")
I'm just wondering if anyone has any ideas where I am going wrong?
Thanks so much!!