Logistic regression in R

Datacamp Learning

Logistic regression in R


Building simple logistic regression models

The donors dataset contains 93,462 examples of people mailed in a fundraising solicitation for paralyzed military veterans. The donated column is 1 if the person made a donation in response to the mailing and 0 otherwise. This binary outcome will be the dependent variable for the logistic regression model.
The remaining columns are features of the prospective donors that may influence their donation behavior. These are the model’s independent variables.
When building a regression model, it is often helpful to form a hypothesis about which independent variables will be predictive of the dependent variable. The bad_address column, which is set to 1 for an invalid mailing address and 0 otherwise, seems like it might reduce the chances of a donation. Similarly, one might suspect that religious interest (interest_religion) and interest in veterans affairs (interest_veterans) would be associated with greater charitable giving.
In this exercise, you will use these three factors to create a simple model of donation behavior.

Logistic回归:glm() family=‘binomial’.

# Examine the dataset to identify potential independent variables
str(donors)
# Explore the dependent variable
table(donors$donated)
# Build the donation model
donation_model <- glm(donated~bad_address+interest_religion+interest_veterans,data = donors, family = "binomial")
# Summarize the model results
summary(donation_model)

Making a binary prediction

In the previous exercise, you used the glm() function to build a logistic regression model of donor behavior. As with many of R’s machine learning methods, you can apply the predict() function to the model object to forecast future behavior. By default, predict() outputs predictions in terms of log odds unless type = “response” is specified. This converts the log odds to probabilities.

Because a logistic regression model estimates the probability of the outcome, it is up to you to determine the threshold at which the probability implies action. One must balance the extremes of being too cautious versus being too aggressive. For example, if you were to solicit only the people with a 99% or greater donation probability, you may miss out on many people with lower estimated probabilities that still choose to donate. This balance is particularly important to consider for severely imbalanced outcomes, such as in this dataset where donations are relatively rare.

# Estimate the donation probability
donors$donation_prob <- predict(donation_model, type = "response")

# Find the donation probability of the average prospect
mean(donors$donated)

# Predict a donation if probability of donation is greater than average (0.0504)
donors$donation_pred <- ifelse(donors$donation_prob > 0.0504, 1, 0)

# Calculate the model's accuracy
mean(donors$donation_pred == donors$donated)

Calculating ROC Curves and AUC

The previous exercises have demonstrated that accuracy is a very misleading measure of model performance on imbalanced datasets. Graphing the model’s performance better illustrates the tradeoff between a model that is overly agressive and one that is overly passive.

In this exercise you will create a ROC curve and compute the area under the curve (AUC) to evaluate the logistic regression model of donations you built earlier.

# Load the pROC package
library(pROC)

# Create a ROC curve
ROC <- roc(donors$donated,donors$donation_prob)

# Plot the ROC curve
plot(ROC, col = 'blue')

# Calculate the area under the curve (AUC)
auc(ROC)

Coding categorical features

Sometimes a dataset contains numeric values that represent a categorical feature.
In the donors dataset, wealth_rating uses numbers to indicate the donor’s wealth level:
0 = Unknown
1 = Low
2 = Medium
3 = High
This exercise illustrates how to prepare this type of categorical feature and the examines its impact on a logistic regression model.
Factors have specific sequence. ‘Relevel’ means move up the pointed factor to the first postion.

# Convert the wealth rating to a factor
donors$wealth_rating <- factor(donors$wealth_rating, levels = c(0,1,2,3), labels = c('Unknown','Low','Medium','High'))
donors$wealth_rating

# Use relevel() to change reference category
donors$wealth_rating <- relevel(donors$wealth_rating, ref = 'Medium')

# See how our factor coding impacts the model
summary(glm(donated~wealth_rating,data=donors,family='binomial'))

Handling missing data

Some of the prospective donors have missing age data. Unfortunately, R will exclude any cases with NA values when building a regression model.
One workaround is to replace, or impute, the missing values with an estimated value. After doing so, you may also create a missing data indicator to model the possibility that cases with missing data are different in some way from those without.

# Find the average age among non-missing values
summary(donors$age)

# Impute missing age values with mean(age)
donors$imputed_age <- ifelse(is.na(donors$age), 61.65, donors$age)

# Create missing value indicator for age
donors$missing_age <- ifelse(is.na(donors$age), 1, 0)

#Building a more sophisticated model
One of the best predictors of future giving is a history of recent, frequent, and large gifts. In marketing terms, this is known as R/F/M:
Recency
Frequency
Money
Donors that haven’t given both recently and frequently may be especially likely to give again; in other words, the combined impact of recency and frequency may be greater than the sum of the separate effects.
Because these predictors together have a greater impact on the dependent variable, their joint effect must be modeled as an interaction.

# Build a recency, frequency, and money (RFM) model
rfm_model <- glm(donated ~ recency * frequency + money, data = donors, family = "binomial")

# Summarize the RFM model to see how the parameters were coded
summary(rfm_model)

# Compute predicted probabilities for the RFM model
rfm_prob <- predict(rfm_model, data = donors, type = "response")

# Plot the ROC curve for the new model
library(pROC)
ROC <- roc(donors$donated, rfm_prob)
plot(ROC, col = "red")
auc(ROC)

在这里插入图片描述


Building a stepwise regression model

In the absence of subject-matter expertise, stepwise regression can assist with the search for the most important predictors of the outcome of interest.
In this exercise, you will use a forward stepwise approach to add predictors to the model one-by-one until no additional benefit is seen.

# Specify a null model with no predictors
null_model <- glm(donated ~ 1, data = donors, family = "binomial")

# Specify the full model using all of the potential predictors
full_model <- glm(donated ~ ., data = donors, family = "binomial")

# Use a forward stepwise algorithm to build a parsimonious model
step_model <- step(null_model, scope = list(lower = null_model, upper = full_model), direction = "forward")

在这里插入图片描述


Merci, c’est tout.

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Here is an example Python code for Uplift model with low-rank regularization logistic regression for multiple correlated binary responses and calculating Individual Treatment Effect (ITE). ```python import numpy as np from sklearn.linear_model import LogisticRegression from scipy.optimize import minimize def fit_low_rank_regression(X, Y, Z, r=10, alpha=0.01): n, p = X.shape q = Y.shape[1] U, s, Vt = np.linalg.svd(Z) U = U[:, :r] Vt = Vt[:r, :] S_inv = np.diag(1/s[:r]) X_tilde = U.T @ X @ Vt.T Y_tilde = U.T @ Y model = LogisticRegression(penalty='none', solver='lbfgs') beta = np.zeros((p, q)) for j in range(q): beta[:, j] = model.fit(X_tilde, Y_tilde[:, j]).coef_ beta = Vt.T @ beta @ S_inv return beta def estimate_ite(X, Y, Z, delta): p = X.shape[1] beta = fit_low_rank_regression(X, Y, Z) eta = X @ beta Y_pred = np.exp(eta)/(1+np.exp(eta)) ite = np.zeros(p) for i in range(p): Y_treat = Y_pred.copy() Y_treat[:, i] = np.exp(eta[:, i] + delta)/(1+np.exp(eta[:, i] + delta)) ite[i] = np.mean(Y_treat - Y_pred) return ite ``` Input: - `X`: the matrix of features (n x p) - `Y`: the matrix of binary responses (n x q) - `Z`: the matrix of treatment indicators (n x r) - `delta`: the treatment effect for each feature (p x 1) Output: - `ite`: the Individual Treatment Effect for each feature (p x 1) Example usage: ```python # generate synthetic data n = 1000 p = 5 q = 3 r = 2 X = np.random.normal(size=(n, p)) Z = np.random.binomial(n=1, p=0.5, size=(n, r)) beta_true = np.random.normal(size=(p, q)) Y_true = (X @ beta_true) * Z + np.random.normal(size=(n, q)) delta_true = np.random.normal(size=(p, 1)) # estimate ITE ite = estimate_ite(X, Y_true, Z, delta_true) print(ite) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值