I keep on getting an error for confusion matrix. I get an error for table function too
advertising <- read.csv('C:/Users/matpo/Desktop/advertising_1.csv', stringsAsFactors = TRUE)
LogMod <- glm(Clicked.on.Ad ~ Daily.Time.Spent.on.Site + Age + Area.Income, data=advertising, family=binomial(link="logit"))
predicted <- predict(LogMod, advertising, type="response")
#matrix below
glm.pred <- ifelse(predicted > 0.8, "click", "not_click")
table(glm.pred, Clicked.on.Ad)
confusionMatrix(glm.pred, advertising)
The error I get:
Error in table(glm.pred, Clicked.on.Ad) :
object 'Clicked.on.Ad' not found
> confusionMatrix(glm.pred, advertising)
Error: `data` and `reference` should be factors with the same levels.
Could you please point me where I am making a mistake?
Related
So this is the code we used,
#Applying Normalization
n<-function(b){
(b-min(b))/(max(b)-min(b))
}
f2<-as.data.frame(lapply(f1[2:19],n))
f3<-cbind(f2,f1$fips)
f<-cbind(f3,f1$score)
#Partitioning Data into Training & Validation Data sets
set.seed(1234) # we set seed so that the value of sample remains same throughout the code
DroughtData <- f[sample(nrow(f), 5000), ]
pd <- sample(2,nrow(DroughtData), replace = TRUE, prob = c(0.8,0.2))
train <- DroughtData[pd == 1,]
validate <- DroughtData[pd == 2,]
regtree <- rpart(formula= f1$score~f1$fips+PRECTOT+QV2M+T2M+T2MDEW+T2MWET+TS++WS50M_MAX+WS50M_MIN+WS50M_RANGE
,data = train, control = rpart.control(maxdepth = 3))
I'm getting an error while applying a regression model on a dataset. The error is as follows:
Error :
Error in model.frame.default(formula = f1$score ~ f1$fips + PRECTOT + :
variable lengths differ (found for 'PRECTOT')
Can anyone assist me to resolve this error
I am trying to do a kfold cross validation with logistics regression but ran into some errors when trying to run the confusion matrix. I cannot figure out how to solve this. Any help is greatly appreciated.
In addition:
Warning message:
In predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :prediction from a rank-deficient fit may be misleading
Here is my code:
for(i in 1:10){
fold_val <- tele.df[folds[[i]],]
fold_train <- tele.df[-folds[[i]],]
tele.default.lr <- glm(Churn ~ ., data = fold_train, family = "binomial")
tele.default.lr.pred.train <- predict(tele.default.lr, fold_train, type = "response")
print(confusionMatrix(tele.default.lr.pred.train, as.factor(fold_train$Churn)))
tele.default.lr.pred.valid <- predict(tele.default.lr, fold_val, type = "response")
print(confusionMatrix(tele.default.lr.pred.valid, as.factor(fold_val$Churn)))
library(C50)
model <- C5.0(training$`Class variable`~ ., data = training[, -9])
plot(model)
pred <- predict.C5.0(model, testing[, -9])
a <- table(testing$`Class variable`, pred)
sum(diag(a))/sum(a)
here while plotting my model i am getting this error, kindly help me on this.
I am getting the following error when I run a linear mixed model.
Error in validObject(.Object) :
invalid class “corMatrix” object: 'sd' slot has non-finite entries
In addition: Warning message:
In vcov.merMod(object, use.hessian = use.hessian) :
Computed variance-covariance matrix problem: not a positive definite matrix;
returning NA matrix
I have created a loop that runs through the columns of my data frame and run a LMM for each column. I have run the same process on 2 other data frames and I had no errors. The other data frames have similar properties (i.e contain 0s etc.). I have also tried excluding rows that contain zero values and this has not fixed the error.
Can anyone explain what this error means?
My loop: I am running LMM with likelihood ratio test
uni_HPOS_total1_A = data.frame(metab = character(), beta = double(), pvalue = double(), stringsAsFactors = FALSE)
for (i in 1:length(colnames(HPOS_XCMS_metab_0_new))){
model0 = lmer(HPOS_XCMS_metab_0_new[, i] ~ (1|technical covariate) ,
REML = FALSE, data = HPOS_XCMS_metab_0_covars)
model1 = lmer(HPOS_XCMS_metab_0_new[, i] ~ (1|technical covariate) + outcome,
REML = FALSE, data = HPOS_XCMS_metab_0_covars)
beta = summary(model1)$coefficients["outcome",1]
res <- anova(model0, model1)
pval = res$`Pr(>Chisq)`[2]
uni_HPOS_total1_A[nrow(uni_HPOS_total1_A) + 1,] = list(colnames(HPOS_XCMS_metab_0)[i], beta, pval)
}
Thanks
I am doing multinomial logistic regression:
Here is the important code:
ml$Category2 <- relevel(ml$Category, ref = "NON-CRIMINAL")
test <- multinom(Category2 ~ DayOfWeek +PdDistrict+liquor+district+MoonPhase,data = ml)
b<- predict(test, type="class")
The problem is when I tried to make classification table, after I run the code:
factcrime<- factor(b, levels=levels(ml$Category))
ctab<- xtabs(~ Category +factcrime, data=ml)
addmargins(ctab)
Then I received the error message:
Error in model.frame.default(formula = ~Category + factcrime, data =
ml) : variable lengths differ (found for 'factcrime')