I have the data and code below and would appreciate your help to estimate DIC (or AIC) for a beta regression model with zoib:
library(zoib)
data("GasolineYield", package = "zoib")
re.md <- zoib(yield ~ temp | 1 | 1, data=GasolineYield,
joint = FALSE, random=1, EUID=GasolineYield$batch,
zero.inflation = FALSE, one.inflation = FALSE,
n.iter=3200, n.thin=15, n.burn=200)
sample2 <- re.md$coeff
summary(sample2)
Following the package's vignette and the article published by the authors, I implemented the code below to get DIC, but I get an error:
dic.samples(sample2, n.iter=201, thin = 1, type="pD")
Error in nchain(model) : Invalid JAGS model object in nchain
I ran the same code with an object from coda.samples {rjags} documentation and it worked:
data(LINE)
LINE$recompile()
LINE.out <- coda.samples(LINE, c("alpha","beta","sigma"), n.iter=1000)
summary(LINE.out)
dic.samples(LINE, n.iter=201, thin = 1, type="pD")
Then I checked the class of each of the objects above LINE, sample2, re.md, and LINE.out, and the output is:
class(LINE)
"jags"
class(re.md)
"list"
class(sample2)
"mcmc.list"
class(LINE.out)
"mcmc.list"
This suggests that my error is probably because the object sample2 is not of class jags.
Therefore, I would appreciate any ideas on how I can get sample2 in a form that can be accepted by dic.samples in order to be able to get DIC (or AIC) for my rd.md model.
Related
I've recently been attempting to evaluate output from k-modes (a cluster label), relative to a so-called True cluster label (labelled 'class' below).
In other words: I've been attempting to external validate the clustering output. However, when I tried external validation measures from the 'fpc' package, I was unsuccessful (error term posted below script).
I've attached my code for the mushroom dataset. I would appreciate if anyone could show me how to successful execute these external validation measures in the context of categorical data.
Any help appreciated.
# LIBRARIES
install.packages('klaR')
install.packages('fpc')
library(klaR)
library(fpc)
#MUSHROOM DATA
mushrooms <- read.csv(file = "https://raw.githubusercontent.com/miachen410/Mushrooms/master/mushrooms.csv", header = FALSE)
names(mushrooms) <- c("edibility", "cap-shape", "cap-surface", "cap-color",
"bruises", "odor", "gill-attachment", "gill-spacing",
"gill-size", "gill-color", "stalk-shape", "stalk-root",
"stalk-surface-above-ring", "stalk-surface-below-ring",
"stalk-color-above-ring", "stalk-color-below-ring", "veil-type",
"veil-color", "ring-number", "ring-type", "spore-print-color",
"population", "habitat")
names(mushrooms)[names(mushrooms)=="edibility"] <- "class"
indexes <- apply(mushrooms, 2, function(x) any(is.na(x) | is.infinite(x)))
colnames(mushrooms)[indexes]
table(mushrooms$class)
str(mushrooms)
#REMOVING CLASS VARIABLE
mushroom.df <- subset(mushrooms, select = -c(class))
#KMODES ANALYSIS
result.kmode <- kmodes(mushroom.df, 2, iter.max = 50, weighted = FALSE)
#EXTERNAL VALIDATION ATTEMPT
mushrooms$class <- as.factor(mushrooms$class)
class <- as.numeric(mushrooms$class))
clust_stats <- cluster.stats(d = dist(mushroom.df),
class, result.kmode$cluster)
#ERROR TERM
Error in silhouette.default(clustering, dmatrix = dmat) :
NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning message:
In dist(mushroom.df) : NAs introduced by coercion
I'm running below code to call rpart function but it keeps giving me error Error in !isord : invalid argument type
# set arguments for rpart function
group.target.metric <- "loan_amount"
group.data.variables <- c(data.config$dict[is_group == TRUE, variable_name_modeling], group.target.metric)
print(group.data.variables)
group.training.data <- complete.data[, ..group.data.variables]
# run main code
group.tree <- rpart(formula = paste(group.target.metric, "~." ),
data = group.training.data,
method = "anova")
Can anyone please guide what this could be about?
Rpart version I'm using is 4.1-15
The issue was while creating data.config$dict it had missing definitions/datatypes for one variable I was using in the model. To check & update datatype in complete.data table use query:
complete.data <- UpdateDataTypes(complete.data, data.config$dict)
I am trying to run supervised LDA on a set of annotated tweets. I keep getting this error msg:
Error in structure(.Call("collapsedGibbsSampler", documents, as.integer(K), : This should not have happened (nan).
How do I fix it?
It only happens when I set logistic = T.
Code below. s is a sample of the data.
corpus1 <- lexicalize(tweets[s], lower=TRUE)
to.keep <- corpus1$vocab[word.counts(corpus1$documents, corpus1$vocab) >= 1]
documents <- lexicalize(tweets[s], lower=TRUE, vocab=to.keep)
params <- sample(c(-1, 1), num.topics, replace=TRUE)
result <- slda.em(documents=documents,
K=num.topics,
vocab=to.keep,
num.e.iterations=10,
num.m.iterations=4,
alpha=1.0, eta=0.1,
annotations = as.integer(annotations[s]),
params,
variance=0.25,
lambda=1.0,
logistic=T)
I know that Zelig is a wrapper... But still, it provides nice simulation capabilities (which I wouldn't be able to do on my own).
Lets say I have this data,
set.seed(123)
x1 = rnorm(5)
x2 = rnorm(5)
z = 1 + 2*x1 + 3*x2
pr = 1/(1+exp(-z))
y = rbinom(5,1,pr)
df = data.frame(y=y,x1=x1,x2=x2)
Now, we estimate the model,
library(Zelig)
relogit <- zelig(y ~ x1 + x2, model = "relogit", data = df)
And now, we (try to) make the table
library(texreg)
texreg(relogit)
... only to get this error.
Error in (function (classes, fdef, stable):
unable to find an inherited method for function ‘extract’ for
signature ‘"Zelig-relogit"’
I am aware of the $getvcov() and $getcoef() functions. But I wonder how I could make a straightforward table using texreg. Any advice will be greatly appreciated. Thanks!
texreg uses a generic function called extract to pull the relevant data from a model object and then processes the resulting texreg object to create a regression table. In order to extend the range of models texreg is applicable to, you can write your own methods for the extract function.
Zelig-relogit objects apparently store a glm object with the relevant data somewhere inside the object and attach a different class name to it. So it should be relatively straightforward to create a copy of this sub-object, fix its class name, and apply the existing extract.glm method to this object to extract the data. More specifically:
# extension for Zelig-relogit objects (Zelig package >= 5.0)
extract.Zeligrelogit <- function(model, include.aic = TRUE, include.bic = TRUE,
include.loglik = TRUE, include.deviance = TRUE, include.nobs = TRUE, ...) {
g <- model$zelig.out$z.out[[1]]
class(g) <- "glm"
e <- extract(g, include.aic = include.aic, include.bic = include.bic,
include.loglik = include.loglik, include.deviance = include.deviance,
include.nobs = include.nobs, ...)
return(e)
}
setMethod("extract", signature = className("Zelig-relogit", "Zelig"),
definition = extract.Zeligrelogit)
This code creates a Zelig-relogit method for the extract function. You can use it by typing something like screenreg(relogit), where relogit is the name of your Zelig-relogit object. The result should look like this:
==================================
Model 1
----------------------------------
(Intercept) -9446502571.59 ***
(62615.78)
x1 19409089045.70 ***
(141084.20)
x2 856836055.47 ***
(98175.65)
----------------------------------
AIC 6.00
BIC 4.83
Log Likelihood -0.00
Deviance 0.00
Num. obs. 5
==================================
*** p < 0.001, ** p < 0.01, * p < 0.05
More generally, if you want to make any Zelig model work with texreg, you should look at model$zelig.out$z.out[[1]] to find the relevant information. I will include the Zelig-relogit extract method in the next texreg release.
I am using a glmulti wrapper for glmer (binomial) and the summary is:
This is glmulti 1.0.7, Apr. 2013.
Length Class Mode
0 NULL NULL
Following what has been done on this this thread, though this is for lmer,
glmulti runs indefinitely when using genetic algorithm with lme4, I get the same result as above. Could it be that the versions have changed since and the wrapping has to be done differently? The following is the dummy code (lifted form the link above):
x = as.factor(round(runif(30),1))# dummy grouping factor
yind = runif(30,0,10) # mock dependent variable
a = runif(30) # dummy covariate
b = runif(30) # another dummy covariate
c = runif(30) # an another one
d = runif(30)
tmpdata <- data.frame(x=x,yind=yind,a=a,b=b,c=c,d=d)
lmer.glmulti <- function (formula, data, random = "", ...) {
lmer(paste(deparse(formula), random), data = data, REML=F, ...)
}
summary(glmulti(formula = yind~a*b*c*d,
data = tmpdata,
random = '+(1|x)',
level = 2,
method = 'h',
crit = 'aicc',
marginality = TRUE,
fitfunc = lmer.glmulti))
lme4 version: 1.1.5
glmulti version: 1.0.7
"R version 3.0.2 (2013-09-25)"
SOLUTION
This works:
lmer.glmulti <- function (formula, data, random, ...) {
lmer(paste(deparse(formula), random), data = data)
}
glmulti(y = yind~a*b*c*d,
data = tmpdata,
random = '+(1|x)',
level = 2,
method = 'h',
crit = 'aicc',
marginality = TRUE,
fitfunc = lmer.glmulti)
packageVersion('lme4')
‘1.1.5’
packageVersion('glmulti')
‘1.0.7’
R.version: 3.1.0
FYI: From the package maintainer:
"fitfunc must be the name of a function so your other call including the function definition in the glmulti call cannot work."
"you named the first argument to glmulti 'formula', where it must be unnamed or 'y'... Sorry. But y is a formula (if passing a string it is the dependent variable only). "