Run a regression analysis
Inputs
question(required): The question the regression should answer, and whether the goal is to explain a relationship, estimate the effect of one variable, or predict an outcome.data_description(required): The dataset (columns with types and units, number of rows, how it was collected, missing values, whether rows are grouped such as repeated measures or users within accounts), or a sample.tool(optional; one of: python, r, excel; default: python): Where the analysis will be run.
Arguments fill these in order. If a required value is empty, take it from the user’s message or ask for it once.
- Restate the goal: association, effect of a specific variable (and note that observational data only supports a causal reading under strong assumptions), or prediction. If the outcome variable or the goal is unclear, ask up to three questions and stop.
- Choose the model from the outcome type and structure, and say why:
- Continuous outcome: linear regression (OLS), with a log transform if the outcome is positive and right-skewed and effects are multiplicative.
- Binary outcome: logistic regression. Counts: Poisson, or negative binomial if overdispersed, with an exposure offset where relevant. Ordered categories: ordinal logistic. Time to event: Cox regression.
- Grouped or repeated observations: mixed-effects models or cluster-robust standard errors.
- Choose variables: the outcome, the predictor of interest, and covariates justified by subject knowledge. For effect estimation, include confounders and exclude mediators and colliders, and explain each choice. For prediction, plan for held-out validation instead. Handle categorical variables (reference level), non-linearity (splines or polynomials when plausible), and interactions only when hypothesised in advance.
- Write complete, runnable code for $tool: load data, prepare variables, fit the model, and print a summary. In Python use pandas and statsmodels' formula API (or scikit-learn only for prediction); in R use
lm,glmorlme4; in Excel useLINESTor the Analysis ToolPak, and state what Excel cannot do (logistic regression, robust standard errors, mixed models) so the user can choose another tool. - Diagnostics, with code: residuals versus fitted, a Q-Q plot, heteroskedasticity (use robust standard errors if present), multicollinearity (variance inflation factors), influential points (Cook's distance), and for logistic models separation and calibration. Say what each looks like when it is fine and what to do when it is not.
- Explain how to interpret the output for this model in the units of the question (for example "each extra year of tenure is associated with a 3.2% higher salary, holding role and region constant"), including how to interpret log transforms, odds ratios and interactions, and to report confidence intervals before p-values.
- List the limits: sample size relative to the number of parameters (as a rough guide at least 10 to 20 observations, or events for logistic models, per parameter), missing data handling, extrapolation outside the data range, and what the model cannot tell us.