Memuat
Memuat
Tobit and Heckman Selection Models for Marketing and Consumer Behaviour
0 views
The analysis aims to estimate the effect of income and social media time on spending with many zeros, illustrated with synthetic data. The Tobit model handles a dependent variable censored at zero (many zero observations), while the Heckman selection model corrects the bias when the outcome is only observed for participants. It suits theses on spending, hours or wages that are often zero or unobserved.

The analysis aims to estimate the effect of income and social media time on spending with many zeros, illustrated with synthetic data.
| Role | Variable | Unit |
|---|---|---|
| Main | Online fashion spending | IDR thousand |
| Supporting 1 | Monthly income | IDR million |
| Supporting 2 | Social media time | hours |
38.3% of Online fashion spending observations are zero, so OLS (Monthly income coefficient 8.170) is biased towards zero relative to the Tobit model (11.271; p < 0.001). The two-step Heckman model gives 8.263 for participants; the inverse Mills ratio (p = 0.048, rho = 0.49) indicates significant selection bias, so the Heckman correction matters.
Computed from illustrative synthetic data, not from a real study.
The Tobit model handles a dependent variable censored at zero (many zero observations), while the Heckman selection model corrects the bias when the outcome is only observed for participants. It suits theses on spending, hours or wages that are often zero or unobserved.
Report the share of zeros, then compare the OLS, Tobit and Heckman coefficients to show the OLS bias. Tobit coefficients are effects on the latent variable, Heckman coefficients are effects for participants, and a significant inverse Mills ratio (non-zero rho) signals selection bias.
A short starting script with this project's variable names. The delivered version is adapted to your data and fully commented.
# Contoh kode R: Tobit and Heckman Selection Models
suppressPackageStartupMessages({
library(ggplot2)
library(patchwork)
library(AER)
library(sampleSelection)
})
set.seed(42)
# ---- Gaya grafik ----
pal <- c("#1f6feb", "#e8590c", "#2f9e44", "#ae3ec9", "#f59f00")
warna <- list(bg = "#f4f6fa", panel = "#ffffff", teks = "#1f2933", redup = "#5f6b78", grid = "#e3e8ee", bar = "#1d3557")
tata <- 1
theme_set(theme_minimal(base_size = 11) + theme(legend.position = "bottom"))
# ---- Data sintetis: Y bernilai nol untuk yang tidak berpartisipasi ----
n <- max(300, 200)
d <- data.frame(monthly_income = round(rnorm(n, 50, 10), 3),
social_media_time = round(rnorm(n, 30, 6), 3),
akses = round(rnorm(n, 50, 10), 1)) # variabel eksklusi: hanya memengaruhi partisipasi
z1 <- as.numeric(scale(d$monthly_income))
z2 <- as.numeric(scale(d$social_media_time))
za <- as.numeric(scale(d$akses))
rho <- runif(1, 0.3, 0.7) # korelasi galat seleksi dan galat outcome
u <- rnorm(n)
e <- rho * u + sqrt(1 - rho^2) * rnorm(n)
# persamaan seleksi (partisipasi)
g0 <- qnorm(runif(1, 0.55, 0.72))
d$partisipasi <- as.integer(g0 + runif(1, 0.4, 0.8) * z1 + runif(1, 0.1, 0.3) * z2 + runif(1, 0.6, 1) * za + u > 0)
# persamaan outcome (hanya teramati bila partisipasi = 1)
b1 <- runif(1, 0.25, 0.45); b2 <- runif(1, 0.08, 0.25) * sample(c(-1, 1), 1)
y_star <- 100 * (1 + b1 * z1 + b2 * z2 + 0.3 * e)
d$online_fashion_spending <- round(ifelse(d$partisipasi == 1, pmax(y_star, 0), 0), 3)
write.csv(d, "data_contoh.csv", row.names = FALSE)
# ---- Analisis 1: OLS naif dan Tobit (tersensor kiri di 0) ----
ols <- lm(online_fashion_spending ~ monthly_income + social_media_time, data = d)
tob <- tobit(online_fashion_spending ~ monthly_income + social_media_time, left = 0, data = d)
print(summary(ols)); print(summary(tob))
st <- coef(summary(tob))
sig_tob <- tob$scale
# ---- Analisis 2: Heckman dua tahap (heckit) dengan variabel eksklusi akses ----
hk <- selection(selection = partisipasi ~ monthly_income + social_media_time + akses,
outcome = online_fashion_spending ~ monthly_income + social_media_time, data = d, method = "2step")
print(summary(hk))
sh <- coef(summary(hk))
io <- hk$param$index$betaO # baris persamaan outcome
b_hk <- sh[io, ]; rownames(b_hk) <- c("(Intercept)", "monthly_income", "social_media_time")
imr <- sh[hk$param$index$Mills, ] # koefisien inverse Mills ratio (lambda)
rho_hat <- unname(coef(hk)["rho"])
p_imr <- unname(imr[4])
nol <- 100 * mean(d$online_fashion_spending == 0)
cat(sprintf("Proporsi nol: %.1f%%, rho = %.3f, p IMR = %.4f\n", nol, rho_hat, p_imr))
# ---- Grafik ----
# 1) histogram Y dengan massa di nol
d$jenis <- factor(ifelse(d$online_fashion_spending == 0, "Zero (censored / not observed)", "Positive"),
levels = c("Zero (censored / not observed)", "Positive"))
p1 <- ggplot(d, aes(online_fashion_spending, fill = jenis)) +
geom_histogram(bins = 30, colour = warna$panel, alpha = 0.9) +
scale_fill_manual(values = c(pal[2], pal[1]), name = NULL) +
labs(title = sprintf("Distribution of online_fashion_spending (%.0f%% zeros)", 100 * mean(d$online_fashion_spending == 0)), x = "online_fashion_spending", y = "Count")
# 2) koefisien monthly_income: OLS vs Tobit vs Heckman (IK 95%)
dk <- data.frame(model = factor(c("OLS", "Tobit", "Heckman"), levels = c("Heckman", "Tobit", "OLS")),
b = c(coef(ols)["monthly_income"], st["monthly_income", 1], b_hk["monthly_income", 1]),
se = c(coef(summary(ols))["monthly_income", 2], st["monthly_income", 2], b_hk["monthly_income", 2]))
dk$teks <- sprintf("%.3f", dk$b)
p2 <- ggplot(dk, aes(b, model, colour = model)) +
geom_errorbarh(aes(xmin = b - 1.96 * se, xmax = b + 1.96 * se), height = 0.18, linewidth = 1) +
geom_point(size = 3.2) +
geom_text(aes(label = teks), vjust = -1.2, size = 3, colour = warna$teks) +
scale_colour_manual(values = c(OLS = pal[2], Tobit = pal[1], Heckman = pal[3]), guide = "none") +
scale_y_discrete(expand = expansion(add = 0.7)) +
labs(title = "Coefficient of monthly_income (95% CI)", x = "Estimate", y = NULL)
# 3) prediksi E[Y] menurut monthly_income (X2 di rata-rata)
xs <- seq(min(d$monthly_income), max(d$monthly_income), length.out = 100)
nd <- data.frame(monthly_income = xs, social_media_time = mean(d$social_media_time))
xb <- as.numeric(model.matrix(~ monthly_income + social_media_time, nd) %*% coef(tob))
dp <- rbind(data.frame(x = xs, y = predict(ols, nd), model = "OLS"),
data.frame(x = xs, y = pnorm(xb / sig_tob) * xb + sig_tob * dnorm(xb / sig_tob), model = "Tobit E[Y]"),
data.frame(x = xs, y = as.numeric(model.matrix(~ monthly_income + social_media_time, nd) %*% b_hk[, 1]), model = "Heckman E[Y*]"))
p3 <- ggplot() +
geom_point(data = d, aes(monthly_income, online_fashion_spending), colour = warna$redup, alpha = 0.3, size = 1.1) +
geom_line(data = dp, aes(x, y, colour = model), linewidth = 1.1) +
scale_colour_manual(values = c("OLS" = pal[2], "Tobit E[Y]" = pal[1], "Heckman E[Y*]" = pal[3]), name = NULL) +
labs(title = "Predicted online_fashion_spending by monthly_income", x = "monthly_income", y = "online_fashion_spending")
p_hasil <- if (tata == 1) p1 / (p2 | p3) else p1 | (p2 / p3)
print(p_hasil)
The original project file is available. It is not just the picture above: you get the working R project. It can be downloaded after payment, and we send it by WhatsApp or email.
ZIP: analisis.R + data_contoh.csv + BACA-DULU.txt
Choose how much help you need. Each higher package includes everything in the lower ones. Prices exclude 11% VAT.
Package 1
US$28
excl. 11% VAT
Duration: 7 working days
Output: R files + result figures
Deliverables
Package 2 · most chosen
US$84
excl. 11% VAT
Duration: 13 working days
Output: Code + analysis + interpretation notes
Includes all of Package 1, plus
Package 3
US$184
excl. 11% VAT
Duration: 18-21 working days
Output: Code + analysis + guidance until ready to present
Includes all of Packages 1 & 2, plus
Yes. The original project file is a ZIP containing the complete analisis.R script, a data_contoh.csv sample dataset and a BACA-DULU.txt guide. It can be downloaded after payment, and we send it by WhatsApp or email. For your own data (Package 2 and 3) the script is adapted to your dataset.
Yes. Tobit and Heckman Selection Models is commonly used at this level in Marketing and Consumer Behaviour. We adapt the analysis to your research question, your own data and your supervisor's requirements.
No. The sheet is computed from illustrative synthetic data so you can see the kind of output R Studio gives. For your order we run the same method on your own data.
That is the point of the packages. Every script is commented, and Package 3 includes two coaching sessions plus practice questions so you can explain the method and the results at your seminar and defence.
Each project has three packages: Package 1 (code and explanation) US$28, Package 2 (your data analysis and interpretation) US$84 and Package 3 (full guidance) US$184, excluding 11% VAT. Larger datasets, extra methods or tight deadlines adjust the quote after a short consultation.
The result sheet uses illustrative synthetic data. R is a programming language of the R Foundation project; RStudio and Posit are trademarks of Posit PBC.
Back to R Studio portfolio