Gerando a base de dados simulada
set.seed(123)
n <- 100 # Número de observações
x1 <- as.factor(sample(1:3, n, replace = TRUE)) # Variável ordinal 1 (ex: nível de educação)
x2 <- as.factor(sample(1:5, n, replace = TRUE)) # Variável ordinal 2 (ex: nível de satisfação)
# Variável resposta para Regressão Linear
y_linear <- 50 + 5 * as.numeric(x1) + 2 * as.numeric(x2) + rnorm(n, 0, 5)
# Variável resposta para Regressão de Poisson
y_poisson <- rpois(n, lambda = exp(0.5 * as.numeric(x1) + 0.3 * as.numeric(x2)))
# Criando o data frame
data <- data.frame(x1, x2, y_linear, y_poisson)
Ajustar os modelos
modelo_linear <- lm(y_linear ~ x1 + x2, data = data)
summary(modelo_linear)
##
## Call:
## lm(formula = y_linear ~ x1 + x2, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.5374 -3.2818 -0.6934 2.6165 14.2529
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 55.900 1.278 43.749 < 2e-16 ***
## x12 3.695 1.200 3.079 0.00273 **
## x13 10.214 1.181 8.646 1.49e-13 ***
## x22 3.400 1.522 2.234 0.02790 *
## x23 6.838 1.599 4.276 4.60e-05 ***
## x24 8.056 1.569 5.134 1.55e-06 ***
## x25 10.096 1.478 6.831 8.61e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.789 on 93 degrees of freedom
## Multiple R-squared: 0.6207, Adjusted R-squared: 0.5963
## F-statistic: 25.37 on 6 and 93 DF, p-value: < 2.2e-16
modelo_poisson <- glm(y_poisson ~ x1 + x2, family = poisson, data = data)
summary(modelo_poisson)
##
## Call:
## glm(formula = y_poisson ~ x1 + x2, family = poisson, data = data)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.8428 0.1350 6.245 4.25e-10 ***
## x12 0.5404 0.1064 5.079 3.80e-07 ***
## x13 0.9678 0.0985 9.825 < 2e-16 ***
## x22 0.1689 0.1530 1.104 0.26979
## x23 0.4069 0.1487 2.738 0.00619 **
## x24 0.9798 0.1331 7.361 1.82e-13 ***
## x25 1.1744 0.1246 9.427 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for poisson family taken to be 1)
##
## Null deviance: 436.53 on 99 degrees of freedom
## Residual deviance: 103.29 on 93 degrees of freedom
## AIC: 485.76
##
## Number of Fisher Scoring iterations: 4
Previsões dos modelos
data$pred_linear <- predict(modelo_linear)
data$pred_poisson <- predict(modelo_poisson, type = "response")
Gráficos
# ---------------------------
# Gráfico da Regressão Linear
# ---------------------------
ggplot(data, aes(x = pred_linear, y = y_linear)) +
geom_point(color = "pink", alpha = 0.6, size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "blue", linetype = "dashed", size = 1.2) +
labs(title = "Modelo de Regressão Linear",
x = "Valores Preditos",
y = "Valores Observados") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5, size = 16),
axis.text = element_text(size = 10),
axis.title = element_text(size = 12))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'

# ---------------------------
# Gráfico da Regressão de Poisson
# ---------------------------
ggplot(data, aes(x = pred_poisson, y = y_poisson)) +
geom_point(color = "pink", alpha = 0.6, size = 3) +
geom_smooth(method = "glm", method.args = list(family = "poisson"), se = FALSE, color = "purple", linetype = "dashed", size = 1.2) +
labs(title = "Modelo de Regressão de Poisson",
x = "Valores Preditos (Poisson)",
y = "Contagens Observadas") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5, size = 16),
axis.text = element_text(size = 10),
axis.title = element_text(size = 12))
## `geom_smooth()` using formula = 'y ~ x'
