Error when running stepwise regression - r

I am trying to run a stepwise regression model. I keep receiving this message:
#Error in step(cdc.fit, direction = "backward") :
# number of rows in use has changed: remove missing values?
#In addition: There were 50 or more warnings (use warnings() to see the first 50)
Am I receiving this because of missing values?
Here is my code:
model=glm(health~
ALCDAY5+
AVEDRNK2+
CHILDREN+
CHKHEMO3+
POORHLTH+
BLOODCHOYes+
BPHIGH4No+
CHCOCNCRYes
, data=data, fmaily=binomial)
stepmodel_back <- model(cdc.fit,direction='backward')
summary(stepmodel_back)
Thanks!

I've not used the stepAIC() function in MASS, but it looks like the following line is incorrect:
stepmodel_back <- step(cdc.fit, direction = "backward")
You've assigned your glm model to an object called 'model', but this line doesn't reference this object. Should it be:
stepmodel_back <- step(model, direction = "backward)
?
Hope this helps.

Related

Error message when using psych::ICC: argument is of length zero

I would like to compute the ICC for a regression model (done using lmer function), but I always get this error message:
Error in if (n.obs < n.obs.original) message("Warning, missing data were found for ", :
argument is of length zero
Here is the function I used:
ICC(model1, missing = TRUE, alpha = .05,lmer = TRUE,check.keys = FALSE)
What I don't understand is that the ICC function should be able to handle missing values. This is why I have used the package "psych and not the package irr`...
Thank you very much for your help :)

Unused argument error when building a Confusion Matrix in R

I am currently trying to run Logistic Regression model on my DF.
While I was creating a new modelframe with the actual and predicted values i get get the following error message.
Error
Error in confusionMatrix(as.factor(log_class), lgtest$Satisfaction, positive = "satisfied") :
unused argument (positive = "satisfied")
This is my model:
#### Logistic regression model
log_model = glm(Satisfaction~., data = lgtrain, family = "binomial")
summary(log_model)
log_preds = predict(log_model, lgtest[,1:22], type = "response")
head(log_preds)
log_class = array(c(99))
for (i in 1:length(log_preds)){
if(log_preds[i]>0.5){
log_class[i]="satisfied"}else{log_class[i]="neutral or dissatisfied"}}
### Creating a new modelframe containing the actual and predicted values.
log_result = data.frame(Actual = lgtest$Satisfaction, Prediction = log_class)
lgtest$Satisfaction = factor(lgtest$Satisfaction, c(1,0),labels=c("satisfied","neutral or dissatisfied"))
lgtest
confusionMatrix(log_class, log_preds, threshold = 0.5) ####this works
mr1 = confusionMatrix(as.factor(log_class),lgtest$Satisfaction, positive = "satisfied") ## this is the line that causes the error
I had same problem. I typed "?confusionMatrix" and take this output:
Help on topic 'confusionMatrix' was found in the following packages:
confusionMatrix
(in package InformationValue in library /home/beyza/R/x86_64-pc-linux-gnu-library/3.6)
Create a confusion matrix
(in package caret in library /home/beyza/R/x86_64-pc-linux-gnu-library/3.6)
Confusion Matrix
(in package ModelMetrics in library /home/beyza/R/x86_64-pc-linux-gnu-library/3.6)
As we can understand from here, since it is in more than one package, we need to specify which package we want to use.
So I typed code with "caret::confusionMatrix(...)" and it worked!
This is how we can write the code to get rid of argument error when building a confusion matrix in R
caret::confusionMatrix(
data = new_tree_predict$predicted,
reference = new_tree_predict$actual,
positive = "True"
)

Getting "invalid type (list) for variable" when fitting a logistic regression model

I am trying to fit a logistic regression model with all predictors on training data, but I keep getting errors. I got this:
library(kernlab)
data(spam)
tr_idx = sample(nrow(spam), 1000)
spam_tr = spam[tr_idx,] # training
spam_te = spam[-tr_idx] # testing
fit_tr = lm(spam_te ~ spam_tr, data=spam)
but this error always comes out:
Error in model.frame.default(formula = spam_te ~ spam_tr, data = spam, :
invalid type (list) for variable 'spam_te'
and when I input this:
fit_tr = lm(spam_te ~ spam_tr, data=tri_dx)
I got another error:
Error in is.data.frame(data) : object 'tri_dx' not found
There are multiple issues with your code.
1. your third line misses a coma
2. your fourth line needs to have the only spam_tr because a linear model is fitted on training data first and then tested on testing data.
tr_idx = sample(nrow(spam), 1000)
spam_tr = spam[tr_idx,]
spam_te = spam[-tr_idx,]
fit_tr = lm(spam_tr , data = spam)
Hope this helps.
In the formula you need to specify the variables in the model, not the data sets.
lm is also a linear model, not logistic.

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)

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