Perform regression in R in 7 steps
Performing a regression in R means more than running a command and reading off a p-value. For a thesis, dissertation, or publication, the model must fit the research question, the data must be plausibly prepared, and the results must be reported correctly in a professional context. This is exactly where errors occur that later become apparent during supervision, examination, or peer review.
R offers a major advantage here: every step is documentable and reproducible. This, however, requires that you not only copy code, but also understand what your model estimates and what assumptions it must meet.
1. Translate the research question into a regression model
Linear regression is appropriate if you want to test whether one or more independent variables statistically explain or predict a metric dependent variable. A psychological research question could be: Do stress, sleep duration, and age predict the depression score? In medicine, it could be: Do BMI, blood pressure, and smoking status explain a laboratory value?
The dependent variable is at the center of the model. It is explained by predictors. In simple linear regression, there is exactly one predictor, while multiple regression has several. The key is not to include as many variables in R as possible. Every variable should be derivable from your hypothesis, the study design, or a professionally justified control strategy.
If your dependent variable is dichotomous, such as therapy success yes or no, you usually need a logistic rather than a linear regression. For count data, ordinal scales, or repeated measures, other model assumptions apply. The first methodological gain therefore occurs before the first R command.
2. Check the data and prepare the variables correctly
Before estimating the model, get an overview of your data. For example, import the dataset as `daten` and check the structure, distributions, and missing values:
str(daten)
summary(daten)
colSums(is.na(daten))
Pay particular attention to variables that were read into R as character strings, even though they represent categorical features. Gender, study group, or treatment arm should generally be available as a factor. Otherwise, R may process categories incorrectly or provide a model whose coefficients do not correspond to your intended reference group.
daten$gruppe <- factor(daten$gruppe,
levels = c("Kontrolle", "Intervention"))
The first category specified here is the reference. The regression coefficient for `Intervention` then describes the estimated difference compared to the control group, holding the other predictors constant.
You should not automatically delete missing values. First check how many cases and which variables are affected. For a few randomly missing individual values, a complete case analysis may be justifiable. For systematic or more extensive missing data, it can bias the sample. Then a justified imputation or methodological advice is often the better decision.
3. Estimate the regression model in R
For linear regression, use the `lm()` function in R. The formula follows a clear pattern: the dependent variable on the left, the predictors on the right.
modell <- lm(depressionsscore ~ stress + schlafdauer + alter,
data = daten)
summary(modell)
The output contains several pieces of information that answer different questions. The coefficient `Estimate` shows the direction and size of an effect. A positive coefficient for stress means: As stress increases, the predicted depression score increases, when sleep duration and age are held constant. The p-value assesses whether the data speak against the null hypothesis of a coefficient of zero. The confidence interval additionally shows the range of plausible effect sizes.
confint(modell)
The coefficient of determination R² indicates what proportion of the variance of the dependent variable is explained by the model. A high R² is not automatically proof of quality. In small samples or with many predictors, it can lead to overly optimistic estimates. Therefore, when reporting multiple models, you should also report the adjusted R².
4. Check model assumptions instead of reporting results prematurely
Linear regression is based on assumptions. Not every minor deviation makes a model unusable. However, you should recognize anomalies, classify them professionally, and react appropriately if necessary.
With the following command, R generates the central diagnostic plots:
par(mfrow = c(2, 2))
plot(modell)
In the Residuals-versus-Fitted plot, you are looking for a random cloud. Distinct curves may indicate a non-linear relationship. The Q-Q plot helps assess whether the residuals are approximately normally distributed. For estimating the coefficients, the normality of the residuals is not the core point, but in small samples, it can become relevant for inference tests.
Homogeneity of variance is particularly relevant in practice. If the spread of the residuals significantly increases or decreases with the predicted values, heteroskedastic errors are present. Then standard errors and p-values can be misleading. Depending on the research question, transformations, heteroskedasticity-consistent standard errors, or a different model may be appropriate. Which solution fits depends on the data structure and objectives.
Influential cases also deserve attention. A single case can strongly shift a regression line without necessarily being a data error. First check for input errors, measurement peculiarities, and professional plausibility. Removing cases only because they interfere with the desired result is methodologically unjustifiable.
5. Detect multicollinearity with multiple predictors
If two or more predictors are highly correlated, it becomes difficult to reliably separate their respective individual effects. This is called multicollinearity. Common examples are closely related scale values or clinical values collected simultaneously.
A warning sign is large standard errors, unexpected signs, or effects that change significantly when another predictor is added. The Variance Inflation Factor, or VIF, can support the check. As a rough guideline, values from 5 are considered noteworthy, and from 10 usually clearly problematic. However, these limits are not automatic decision rules.
library(car)
vif(modell)
If multicollinearity is present, it may be useful not to model redundant similar variables together, to use a theoretically justified sum score, or to transparently compare separate models. The software does not decide this, but rather the combination of theory, measurement concept, and data situation.
6. Interpret and report results correctly in a professional context
A statistically significant coefficient is not a complete interpretation. Report the effect size, uncertainty, model fit, and substantive meaning. A result like `b = 0.42, 95% CI [0.15; 0.69], p = 0.004` does not simply mean that an effect "exists". It means that one unit more stress is associated with a predicted depression score that is 0.42 points higher, holding the other model variables constant.
A possible results text could look like this:
> The multiple linear regression showed that higher stress was significantly associated with higher depression scores (b = 0.42, 95% CI [0.15; 0.69], p = 0.004). Sleep duration and age were included as covariates. The overall model explained 31% of the variance in the depression scores (adjusted R² = 0.31).
Avoid causal formulations such as "Stress causes depression" if your data come from a cross-sectional study. Regression statistically controls for observed variables, but does not replace an experimental design and does not eliminate unobserved confounding factors.
7. Work reproducibly and seek support in time
Save data preparation, analysis, and graphics in an R script. This allows you to repeat every step, trace changes, and respond reliably to questions from your supervisor. Also document decisions regarding outliers, missing data, coding, and model variants. Especially in final theses, this transparency often determines whether an analysis appears convincing.
If the research question, scale level, or assumptions are not clear, a methodological clarification before the final evaluation is worthwhile. Easy Statistik supports you individually with PhD statisticians - from model selection, R code, and diagnostics to a traceable results text for your work. Request a statistics consultation via the contact form.
A good regression is not created by the longest code, but by traceable decisions. If you can clearly justify these decisions, R will change from a factor of uncertainty into a reliable tool for your research.