Error in running Logit model clustered by contry code - r

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.

Related

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

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.

How do I solve an error in x : non-conformable arguments (R)?

I am trying to implement ordinal logistic regression on my dataset in r. I use the function 'polr' for this, but cannot seem to find a lot of information regarding its implementation.
The following errors are the ones I'm stuck on:
> dat.polr <- polr(as.factor(relevance)~allterms+idf.title, data=dat.one)
Warning message:
In polr(as.factor(relevance) ~ allterms + idf.title + idf.desc + :
design appears to be rank-deficient, so dropping some coefs
> dat.pred <- predict(dat.polr,dat.test,type="class")
Error in X %*% object$coefficients : non-conformable arguments
I want to train my model to guess the relevance of a new dataset. dat.one is the dataset I'm using to train the data, dat.test is the dataset I'm using to test the data. I believe that the predict variable's error is caused by the warning in polr. However, I have no clue how to resolve this. Any help would be appreciated :)

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 model selection (gamm4) dredge function (MuMIn R package): family not recognised, model skipped

I am trying to do model selection for generalized additive mixed models (made using gamm4 using the MuMIn package in R.
I am essentially trying to follow this piece of literature for model selection with MuMIn and gamm4.
I am creating a model with 9 variables, and a random individual effect - which looks likes this:
library(gamm4)
library(MuMIn)
SouthFull = gamm4(OtoWidth ~ s(Ages) +
LagFinFSLdat_AnnEsts +
FSL_months_Feb +
LagFSLdat_Annual +
LagFSLdat_Spring +
LagFSL_months_Oct+
FinFSLdat_SummerDat +
FinFSLdat_AutumnDat +
LagFSL_months_Nov , random = ~(1|FishName), data = South)
But when I try to use the dredge function, it fails with the error message:
(dd <- dredge(global.model=SouthFull))
Error in dredge(global.model = SouthFull) : result is empty
In addition: There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In gamm4::gamm4(...) : family not recognized (model 1 skipped)
...
50: In gamm4::gamm4(...) : family not recognized (model 50 skipped)
Is this due to a problem with MuMIn not recognizing the gamm4 model family?
Full code & data can be found at my GitHub repository and the specific issue is in the code gamm_analysis.R, with the specific warnings found at line 81.
Thank you in advance
I think that there is something wrong with the package. I have the same error. Then, I tried to run the example available in the help of the function dredge of the MumIn package to see what I am doing differently and I get the same error even using the example below:
fm1 <- lm(y ~ ., data = Cement)
dd <- dredge(fm1)
The error message is:
Error in nobs.default(global.model) : no 'nobs' method is available

R segmented regression predict gives error: "subscript out of bounds"

I'm building a segmented regression model using R's Segmented package.
I was able to create the model but have trouble using the predict.segmented function. It always throws an error saying "subscript out of bounds"
This is the exact error message:
Error in newdata[[nameZ[i]]] : subscript out of bounds
Traceback just gives this:
1: predict.segmented(seg_model, xtest)
I created a simple case that gives the same error:
require(segmented)
x = c(1:90, 991:1000)
y = c((x[1:10]/2), (x[11:100]*2))
lm_model = lm(y~x)
seg_model = segmented(lm_model, seg.Z=~x, psi=list(x=NA),
control=seg.control(display=FALSE, K=1, random=TRUE))
xtest = c(1:1000)
predict.segmented(seg_model, xtest)
I am starting to think this could be a bug. I'm new to R and not sure how to debug this either. Any help is appreciated!
You are using predict.segemented incorrectly. Like nearly all the predict() functions, your newdata parameter should be a data.frame, not a vector. Also, it needs to have names that match the variables used in your regression. Try
predict.segmented(seg_model, data.frame(x=xtest))
instead. When using a function for the first time, be sure the read the help page (?predict.segmented) to know what the function expects for each of the parameters.

Resources