# Linear and Logistic Regression — R Programming

Source: https://www.geekswithgeeks.com/en/r-programming/s-models

> Fit, interpret and check regression models with lm and glm.

## Modelling relationships

**Linear regression** models a numeric outcome as a linear combination of predictors. In R, **`lm(outcome ~ predictor1 + predictor2, data = df)`** fits a model using **formula** syntax: `~` separates outcome from predictors, `+` adds terms, `*` adds interactions, and categorical predictors (factors) are converted to dummy variables automatically. **`summary()`** shows coefficients (the expected change in the outcome for a one-unit change in a predictor, holding others fixed), standard errors, p-values and R-squared; **`confint()`** gives confidence intervals. Check assumptions with residual plots (`plot(model)`): roughly linear relationships, constant variance and no influential outliers. **Logistic regression** models a binary outcome (churned or not, paid or not) with **`glm(y ~ x, family = binomial, data = df)`**; coefficients are on the **log-odds** scale, so `exp(coef(model))` gives **odds ratios**. Use **`predict()`** for new data (with `type = "response"` for probabilities). The **broom** package turns model output into tidy tibbles, and **tidymodels** provides a consistent framework for preprocessing, resampling and evaluating many model types.

## Linear and logistic models

Formulas, summaries, tidy output and predictions.

```r
library(broom)

# linear model: order value explained by number of items and city
fit <- lm(amount_inr ~ items + city, data = sales)
summary(fit)                 # coefficients, R-squared, p-values
confint(fit)
tidy(fit, conf.int = TRUE)   # coefficients as a tibble
glance(fit)                  # model-level statistics
par(mfrow = c(2, 2)); plot(fit)   # residual diagnostics

# logistic model: probability a customer churns
churn_fit <- glm(churned ~ days_since_last_order + orders_last_90d + support_tickets,
                 family = binomial, data = customers)
tidy(churn_fit, exponentiate = TRUE)    # odds ratios

new_customers <- data.frame(days_since_last_order = c(10, 120),
                            orders_last_90d = c(5, 0),
                            support_tickets = c(0, 3))
predict(churn_fit, newdata = new_customers, type = "response")   # churn probabilities
```

## Coefficients are conditional

A coefficient describes the association holding the other predictors fixed, in this data. It is not automatically a causal effect: confounders, selection bias and reverse causation can all produce strong coefficients.

**Quiz:** Which call fits a logistic regression for a binary outcome in R?

- [x] glm(y ~ x, family = binomial)
- [ ] lm(y ~ x)
- [ ] t.test(y, x)
- [ ] cor(y, x)

*Answer:* glm(y ~ x, family = binomial). glm with the binomial family fits logistic regression.
