VIF function returning error message - r

I'm trying to perform a VIF on a multivariate regression model, but when I ran the vif function in r I get an error.
Code and error below:
vif(analys3.lm)
Error in if (names(coefficients(mod)[1]) == "(Intercept)") { :
argument is of length zero
The intercept is still there in my model though.
analys3.lm<- lm(formula = cbind(df$col1,
df$col2) ~
df$col3+ df$col4,
data = df)

Apparently, vif can't deal with an mlm object (multiple DVs). Run separate models and check them.

Related

Why do my variables disappear after using feature selection with step()?

I made a Multinomial Logistic Regression model using library(nnet) in R.
I notice I, one, get an error, and two, after using the step() function, my predictor variables convert into the variable I'm attempting to predict, solely (Depression).
summary(multinom_model)$call
produces:
multinom(formula = out ~ ., data = train)
Warning message:
In sqrt(diag(vc)) : NaNs produced
BUT
mult_model <- step(multinom_model, trace = FALSE)
summary(mult_model)$call
this code produces:
multinom(formula = out ~ Depressed, data = train)
Why is this happening? Also, both models predict the same output on the test data. Does it have to do with the warning message? How do I fix that?

"plot.new" error when checking normality of residuals using qqline()

I would like to check if my model (standardized) residuals are normally distributed.
model <- lm(ratiopermonth ~ Greenspace, data = mydata)
qqline(rstandard(model))
But I got an error message:
plot.new has not been called yet
What is wrong?
qqline is to be used after qqnorm.
r <- rstandard(model)
qqnorm(r)
qqline(r)

Marginal Effect from svyglm object with a subsample in R

I need to compute marginal effects out of a Generalized Linear Model (family=Poisson) estimated via the svyglm function from the R package survey for a subsample.
First, I declared the survey desgin with:
myDesisgn = svydesign(id=data$id, strata=data$strata, weights=data$sw, data=data)
Second, I estimated my model as:
fit = svyglm(y~ x1 +x2, design=myDesisgn, data=data, subset= x3 == 1, family= poisson(link = "log"))
Finally, when I want to get the Average Marginal Effect for, let's say, x1 I run:
summary(margins(fit, variables = "x1", design=myDesisgn))
... but I get the following error message:
"Error in h(simpleError(msg, call)) :
error in evaluating the argument 'object' in selecting a method for function 'summary': 'x' and 'w' must have the same length"
Running the following does not work either:
summary(margins(fit, variables = "x1", design=myDesisgn, subset=x3==1))
Solution:
summary(margins(fit, variables = "x1", design=myDesisgn[myDesisgn$variables$x3 == 1]))
Subsetting complex surveys leads to problems in the error estimation. When interested in a parameter for a specific subsample, one should use the desired subsample to estimate the parameter of interest and the full sample for the estimation of its error.
For example, svyglm(y~x, data=data, subset = z == 1) does exactly this (beta_hat estimated using observations for which z=1 and se(beta_hat) using the full sample).
Subsetting a svy design is possible and it keeps the original design information about number of clusters, strata. The code shown above is the "manual" way of doing so. Alternative one can directly rely on the subset.survey.design {survey} function.
myDesign_subset <- subset(myDesign, data$x3 == 1)
The two methods are equivalent and produce correct z-stats.

stepAIC handling of multinom models

I am seeing some weird behavior with the stepAIC function in the MASS package when dealing with multinomial logistic models. Here is some sample code:
library(nnet)
library(MASS)
example("birthwt")
race.model <- multinom(race ~ smoke, bwt)
race.model2 <- stepAIC(race.model, k = 2)
In this case race.model and race.model2 have identical terms; stepAIC did not prune anything. However, I need to query certain attributes of the models, and I get an error with race.model2:
formula(race.model)[2]
returns race() but
formula(race.model2)[2]
gives the error:
Error in terms.formula(newformula, specials = names(attr(termobj, "specials"))) :
invalid model formula in ExtractVars
This behavior only seems to occur when stepAIC does not remove terms from the model. In the following code, terms are removed by stepAIC, and both models can be properly queried:
race.big <- multinom(race ~ ., bwt)
race.big2 <- stepAIC(race.big, k = 2)
formula(race.big)[2]
formula(race.big2)[2]
Any ideas about what is going wrong here?

Binomial GLM using caret train

I would like to fit a Binomial GLM on a certain dataset. Using glm(...,family=binomial) everything works fine however I would like to do it with the caret train() function. Unfortunately I get an unexpected error which I cannot get rid of.
library("marginalmodelplots")
library("caret")
MissUSA <- MissAmerica08[,c(2,4,6,7,8,10)]
formula <- cbind(Top10, 9-Top10)~.
glmfit <- glm(formula=formula, data=MissUSA, family=binomial())
trainfit <-train(form=formula,data=MissUSA,trControl=trainControl(method = "none"), method="glm", family=binomial())
The error I get is:
"Error : nrow(x) == length(y) is not TRUE"
caret doesn't support grouped data for a binomial outcome. You can expand the data into a factor variable that is binary (Bernoulli) data. Also, if you do that, you do not need to use family=binomial() in the call to train.
Max

Resources