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

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)

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?

Compare regression with robust standard errors to null using Wald's Test in R

I am running a regression model that looks like this:
wwMLR <- lm(contAOMIdiff ~ PHQ9 + KVIQtot, data = wwMeanWide4)
Having used check_heteroscedasticity(wwMLR) from the Performance package I can see that the regression model violates the assumption of homoscedasticity. Due to this I have built a model with robust standard errors shown below:
library(estimatr)
wwMLR_hc3 <- lm_robust(formula = contAOMIdiff ~ PHQ9 + KVIQtot, data = wwMeanWide4,
se_type = "HC3", alpha = 0.0482)
What I would like to do now is compare this regression model to a null using Wald's Test. The null model looks like the below:
wwnull_hc3 <- lm_robust(formula = contAOMIdiff ~ 1, data = wwMeanWide4,
se_type = "HC3", alpha = 0.0482)
When I try to compare these using a Wald's Test:
library(lmtest)
waldtest(wwMLR_hc3, wwnull_hc3, vcov = vcovHC)
I get an error:
Error in eval(predvars, data, env) : object 'contAOMIdiff' not found
contAOMIdiff is my response variable in the regression. I am not sure why it can't be found but I am assuming this may be a compatibility issue between the lm_robust model type and the waldtest() function.
If anyone has any ideas on how I can get this to work, or an alternate way to run a Wald's Test on these two models I would be very grateful.
I have found a similar question here, which has not been answered: R Wald test for cluster robust se's

VIF function returning error message

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.

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