How do you compute average marginal effects for glm.cluster models? - r

I am looking for a way to compute average marginal effects with clustered standard errors which i seem to be having a few problems with. My model is as follows:
cseLogit <- miceadds::glm.cluster(data = data_long,
formula = follow ~ f1_distance + f2_distance + PolFol + MediaFol,
cluster = "id",
family = binomial(link = "logit"))
Where the dependent variable is binary (0/1) and all explanatory variables are numeric. I've tried to different ways of getting average marginal effects. The first one is:
marginaleffects <- margins(cseLogit, vcov = your_matrix)
Which gives me the following error:
Error in find_data.default(model, parent.frame()) :
'find_data()' requires a formula call
I've also tried this:
marginaleffects <- with(cseLogit, margins(glm_res, vcov=vcov))
which gives me this error:
Error in eval(predvars, data, env) :
object 'f1_distance' was not found
In addition: warnings:
1: In dydx.default(X[[i]], ...) :
Class of variable, f1_distance, is unrecognized. Returning NA.
2: In dydx.default(X[[i]], ...) :
Class of variable, f2_distance, is unrecognized. Returning NA.
Can you tell me what i'm doing wrong? If i haven't provided enough information, please let me know. Thanks in advance.

Related

Error in running Logit model clustered by contry code

I am just starting out in R and am trying to run a logit model with clutered country codes. My attempt is to replicate a model from this paper with the following replication information :
Data available here (data file): https://drive.google.com/file/d/1O2Gvf7sqxYN7IsOI2zTRZANlbLnVQvvw/view
*Model 1: base
logit mk l.wtrivalry mkyear mkyear2 mkyear3, cluster(ccode)
*Model 2: Controls -- no conflict
logit mk l.wtrivalry l.lnrgdppc l.polity2 l.exclid l.lnpopWB l.coupdummy mkyear mkyear2 mkyear3, cluster(ccode)
I have been trying variations of code, but keep failing to cluster correctly (unless I do so, the significance of the variables does not align with that of the paper). My attempts have been:
simp.logit.general. <- glm(mk ~ wtrivalry + mkyear + mkyear2 + mkyear3, data = core.data, family="binomial")
summary(simp.logit.general.)
robcov(simp.logit.general)
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'as.matrix': 'arg' should be one of “deviance”, “pearson”, “working”, “response”, “partial”
I have also tried:
coeftest(simp.logit.general, vcov. = vcovCL(simp.logit.general, cluster = core.data$ccode, type = "HC0"))
IF anyone could help with this, I would really appreciate it.

Fail to predict woe in R

I used this formula to get woe with
library("woe")
woe.object <- woe(data, Dependent="target", FALSE,
Independent="shop_id", C_Bin=20, Bad=0, Good=1)
Then I want to predict woe for the test data
test.woe <- predict(woe.object, newdata = test, replace = TRUE)
And it gives me an error
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "data.frame"
Any suggestions please?
For prediction, you cannot do it with the package woe. You need to use the package. Take note of the masking of the function woe, see below:
#let's say we woe and then klaR was loaded
library(klaR)
data = data.frame(target=sample(0:1,100,replace=TRUE),
shop_id = sample(1:3,100,replace=TRUE),
another_var = sample(letters[1:3],100,replace=TRUE))
#make sure both dependent and independent are factors
data$target=factor(data$target)
data$shop_id = factor(data$shop_id)
data$another_var = factor(data$another_var)
You need two or more dependent variables:
woemodel <- klaR::woe(target~ shop_id+another_var,
data = data)
If you only provide one, you have an error:
woemodel <- klaR::woe(target~ shop_id,
data = data)
Error in woe.default(x, grouping, weights = weights, ...) : All
factors with unique levels. No woes calculated! In addition: Warning
message: In woe.default(x, grouping, weights = weights, ...) : Only
one single input variable. Variable name in resulting object$woe is
only conserved in formula call.
If you want to predict the dependent variable with only one independent, something like logistic regression will work:
mdl = glm(target ~ shop_id,data=data,family="binomial")
prob = predict(mdl,data,type="response")
predicted_label = ifelse(prob>0.5,levels(data$target)[1],levels(data$target)[0])

Errors with dredge() function in MuMin

I'm trying to use the dredge() function to evaluate models by completing every combination of variables (up to five variables per model) and comparing models using AIC corrected for small sample size (AICc).
However, I'm presented with one error and two warning messages as follows:
Fixed term is "(Intercept)"
Warning messages: 1: In dredge(MaxN.model,
m.min = 2, m.max = 5) : comparing models fitted by REML 2: In
dredge(MaxN.model, m.min = 2, m.max = 5) : arguments 'm.min' and
'm.max' are deprecated, use 'm.lim' instead
I've tried changing to 'm.lim' as specified but it comes up with the error:
Error in dredge(MaxN.model, m.lim = 5) : invalid 'm.lim' value In
addition: Warning message: In dredge(MaxN.model, m.lim = 5) :
comparing models fitted by REML
The code I'm using is:
MaxN.model<-lme(T_MaxN~Seagrass.cover+composition.pca1+composition.pca2+Sg.Richness+traits.pca1+
land.use.pc1+land.use.pc2+seascape.pc2+D.landing.site+T_Depth,
random=~1|site, data = sgdf, na.action = na.fail, method = "REML")
Dd_MaxN<-dredge(MaxN.model, m.min = 2 , m.max = 5)
What am I doing wrong?
You didn't tell us what you tried to specify for m.lim. ?dredge says:
m.lim ...optionally, the limits ‘c(lower, upper)’ for number of terms in a single model
so you should specify a two-element numeric (integer) vector.
You should definitely be using method="ML" rather than method="REML". The warning/error about REML is very serious; comparing models with different fixed effects that are fitted via REML will lead to nonsense.
So you should try:
MaxN.model <- lme(..., method = "ML") ## where ... is the rest of your fit
Dd_MaxN <- dredge(MaxN.model, m.lim=c(2,5))

Error in R: "Error in tree.control(nobs, ...) : unused argument (type = "class")"

I am building a decision tree using the tree library in R. I have attempted to fit my model as follows:
model <- tree(Outcome ~ Age + Sex + Income, data = train, type = "class")
Running the above line gives me an error as follows:
Error in tree.control(nobs, ...) : unused argument (type = "class")
I down sampled so that each class is equal and so did not specify any weights. If I remove the argument, type = "class", the model runs but when I predict using the model, it seems that it is building a regression model which I do not want.
Can someone help?
If you look at the help page ?tree there is no argument called type. If you are getting a regression, that is because Outcome is a numeric argument. I expect that you can fix this by adding
train$Outcome = factor(train$Outcome)

error in R: Error in model.frame.default.... variable lengths differ (no N/As in my data)

I had this this problem with an error message that I don't understand whilst trying to create a binomial mixed effects model, but I didn't have any N/As in my data which I thought this error message meant... can you help? thanks
formula:
bgmodel<-glmer(c(bare,NotBare)~year*treatment*mix+(1|block),data=bareground,family=binomial)
Error in model.frame.default(data = bareground, drop.unused.levels =
TRUE, : variable lengths differ (found for 'year')
The error is in the Model formula, you have to use the function cbind and not c

Resources