Memuat
Memuat
Tobit and Heckman Selection Models for Informatics and Information Systems
0 views
Through synthetic data, the illustrated aim is to correct censoring or selection bias in cybersecurity spending with Tobit and Heckman models using number of employees and digital asset value. 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.

Through synthetic data, the illustrated aim is to correct censoring or selection bias in cybersecurity spending with Tobit and Heckman models using number of employees and digital asset value.
| Role | Variable | Unit |
|---|---|---|
| Main | Cybersecurity spending | IDR million |
| Supporting 1 | Number of employees | persons |
| Supporting 2 | Digital asset value | IDR billion |
43.7% of Cybersecurity spending observations are zero, so OLS (Number of employees coefficient 0.497) is biased towards zero relative to the Tobit model (0.750; p < 0.001). The two-step Heckman model gives 0.461 for participants; the inverse Mills ratio (p = 0.063, rho = 0.44) shows no significant selection bias.
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(number_of_employees = round(rnorm(n, 50, 10), 3),
digital_asset_value = round(rnorm(n, 30, 6), 3),
akses = round(rnorm(n, 50, 10), 1)) # variabel eksklusi: hanya memengaruhi partisipasi
z1 <- as.numeric(scale(d$number_of_employees))
z2 <- as.numeric(scale(d$digital_asset_value))
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$cybersecurity_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(cybersecurity_spending ~ number_of_employees + digital_asset_value, data = d)
tob <- tobit(cybersecurity_spending ~ number_of_employees + digital_asset_value, 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 ~ number_of_employees + digital_asset_value + akses,
outcome = cybersecurity_spending ~ number_of_employees + digital_asset_value, 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)", "number_of_employees", "digital_asset_value")
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$cybersecurity_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$cybersecurity_spending == 0, "Zero (censored / not observed)", "Positive"),
levels = c("Zero (censored / not observed)", "Positive"))
p1 <- ggplot(d, aes(cybersecurity_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 cybersecurity_spending (%.0f%% zeros)", 100 * mean(d$cybersecurity_spending == 0)), x = "cybersecurity_spending", y = "Count")
# 2) koefisien number_of_employees: 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)["number_of_employees"], st["number_of_employees", 1], b_hk["number_of_employees", 1]),
se = c(coef(summary(ols))["number_of_employees", 2], st["number_of_employees", 2], b_hk["number_of_employees", 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 number_of_employees (95% CI)", x = "Estimate", y = NULL)
# 3) prediksi E[Y] menurut number_of_employees (X2 di rata-rata)
xs <- seq(min(d$number_of_employees), max(d$number_of_employees), length.out = 100)
nd <- data.frame(number_of_employees = xs, digital_asset_value = mean(d$digital_asset_value))
xb <- as.numeric(model.matrix(~ number_of_employees + digital_asset_value, 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(~ number_of_employees + digital_asset_value, nd) %*% b_hk[, 1]), model = "Heckman E[Y*]"))
p3 <- ggplot() +
geom_point(data = d, aes(number_of_employees, cybersecurity_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 cybersecurity_spending by number_of_employees", x = "number_of_employees", y = "cybersecurity_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$31
excl. 11% VAT
Duration: 7 working days
Output: R files + result figures
Deliverables
Package 2 · most chosen
US$94
excl. 11% VAT
Duration: 13 working days
Output: Code + analysis + interpretation notes
Includes all of Package 1, plus
Package 3
US$203
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 Informatics and Information Systems. 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$31, Package 2 (your data analysis and interpretation) US$94 and Package 3 (full guidance) US$203, 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