Plotting a GLM Model - plot

I am facing trouble with plotting the predictions of a glm model . When i run the below code, R draws up an empty plot.
Logistic regression
model2 = glm(as.factor(loan_status) ~ . , data = train , family = binomial(link = 'logit'))
summary(model2)
Prediction
pred1 <- predict(model2,test,type = 'response')
ggplot(data.frame(pred1), aes(pred1))
train dataset consists of categorical and numerical values
Call:
glm(formula = as.factor(loan_status) ~ ., family = binomial(link = "logit"),
data = train)
Appreciate any assistance
Thank you

Related

Only calling a Binomial model

I am under the assumption that there are issues just calling a binomial model in R without specifying the family type (logit/probit) right?
model <- glm(y ~ x, family = binomial, data = data)
vs
model_2 <- glm(y ~ x, binomial(link = "logit"), data = data)

Plotting multiple logistic regression in R

I've built this logistic regression model which includes four predictors, optimized from a dataframe that includes ten predictors (I've uploaded the data here http://www.filedropper.com/df). This is my first time trying to plot a logistic model in R, and I'm not sure how to go about visualizing the results. Here's my code:
df <- read.csv("df.csv")
model <- glm(y ~ v1 + v6 + v9 + v3, data = df,
family = binomial, maxit = 100)
summary(model)
df$pred_res <- predict(model, df, type = "response")
ggplot(data = df, aes(x = v3, y = pred_res)) +
geom_line(mapping = aes(colour = "blue"), size = 1)
Can anyone suggest a better way to visualize/plot the predicted values of the model? In general, does anyone have any tips for visualizing a logit model with multiple predictors? Thank you!

GLM Family using tidymodels

I am trying to use the tidymodels package for a GLM and want to use the Gamma or Poisson distribution.
Using glm I would use something like the following
# using glm
mdl <- glm(data = data, y ~ x, family = Gamma(link = "inverse"))
mdl <- glm(data = data, y ~ x, family = poisson(link = "log"))
# using glmnet
library(glmnet)
mdl <- glmnet(data$x, data$y, family = Gamma(link = "inverse"))
mdl <- glmnet(data$x, data$y, family = poisson(link = "log"))
How can I achieve the same using tidymodels? Note that I am trying to do a regression and not a classification (logistic regression) for which I could use parsnip::logistic_reg().
I found one article on Generalized Linear Models on tidymodels, which belongs to the embed package but does not show how to specify the family.
I would expect something similar to this (which does not work as neither linear_reg has the parameters family or link, nor does set_engine support glm in linear regression mode)
mdl <- linear_reg(mode = "regression", family = "gamma", link = "inverse") %>% set_engine("glm") # or glmnet
That was easier than expected:
mdl <- linear_reg(mode = "regression") %>%
set_engine("glmnet", family = "gamma")
# or
mdl <- linear_reg(mode = "regression") %>%
set_engine("glmnet", family = Gamma(link = "inverse"))

Polynomial regression model to predict values for a variable with 500 rows

There are around 500 values planned and on the basis of this I have to predict new values actual.
Kindly help me with the coding, here I'm showing what I am doing manually:
predict(poly_reg, data.frame(planned= 48.80000,
Level2 = 48.80000^2,
Level3 = 48.80000^3))
lin_reg = lm(formula = actual ~ ., data = dataset)
summary(lin_reg)
# Fitting Polynomial Regression to the dataset
dataset$Level2 = dataset$planned^2
dataset$Level3 = dataset$planned^3
dataset$Level4 = dataset$Planned_FTM^0.25000
poly_reg = lm(formula = actual~ ., data = dataset)
# Visualising the Linear Regression results
# install.packages('ggplot2')
library(ggplot2)
ggplot() + geom_point(aes(x = dataset$planned, y = dataset$actual), colour = 'red') + geom_line(aes(x = dataset$planned, y = predict(lin_reg, newdata = dataset)), colour = 'blue') + ggtitle('Truth or Bluff (Linear Regression)') + xlab('planned') + ylab('actual')
# Predicting a new result with Linear Regression
predict(lin_reg, data.frame(planned= 48.80000))
# Predicting a new result with Polynomial Regression
predict(poly_reg, data.frame(planned= 48.80000, Level2 = 48.80000^2, Level3 = 48.80000^3))
Try using poly on planned to whatever order polynomial you're using (I used 4):
# Fitting Linear Regression to the dataset
lin_reg = lm(formula = actual ~ planned, data = dataset)
# Fitting Polynomial Regression to the dataset
poly_reg = lm(formula = actual ~ poly(planned, 4), data = dataset)
# Predicting a new result with Linear Regression
predict(lin_reg)
# Predicting a new result with Polynomial Regression
predict(poly_reg)

Run HLM mediation in R

I try to run HLM mediation with the "mediation" package:
med.fit <- glmer(M ~ treat + control + (1|subject_id) ,family = binomial(link = "logit"), data = R1_data)
out.fit <- glmer(Y ~ M+ treat + control+ (1 + M|subject_id),family = binomial(link = "logit"), data = R1_data)
med.out <- mediate(med.fit, out.fit, treat = "treat", mediator = "M", sims = 1000)
I got this error message:
Error in [.data.frame(y.data, int.term.name[p]) : undefined columns selected
How to solve this problem?
Here is the original data and code:
names(R1_data)
[1] "subject_id"
[3] "Presented_is_solvable"
[5] "JOS"
[17] "Answer_JOS"
[23] "Matrix_Z_score"
library(mediation)
library(lme4)
med.fit <- glmer(JOS ~ Matrix_Z_score + Presented_is_solvable + (1|subject_id) ,family = binomial(link = "logit"), data = R1_data)
out.fit <- glmer(Answer_JOS ~ JOS + Matrix_Z_score +Presented_is_solvable + (1 + JOS|subject_id),family = binomial(link = "logit"), data = R1_data)
med.out <- mediate(med.fit, out.fit, treat = "Matrix_Z_score", mediator = "JOS", sims = 1000)
Figured out that this happens when treatment or mediator data is classified as factor data in R. The mediate function can't properly locate the names of those variables from the fitted models as in the models, they are displayed as "variablename"+factor level.
The solution is to make sure those variables are classified as integers. You can take a look at the variable classifications in the student data set within the mediation package.

Resources