Data Analysis
Data analysis step by step: question, collection, cleaning, exploration, outliers, charts, correlation, regression and metrics to evaluate models.
https://morrisonkuhlsen.com/en/data-analysis/
Data Analysis
Data Analysis is the process of collecting, organizing, cleaning, transforming, visualizing and interpreting data in order to produce useful information. It combines Statistics, Probability, Computing and critical thinking to understand real phenomena and support decisions.
What is Data Analysis?
Analyzing data is not just about making charts or computing averages.
Data Analysis is a complete process that starts with a question and ends with an interpretation.
In general, it tries to answer questions such as:
- What do the data show?
- Is there an important pattern?
- Are there strange or inconsistent values?
- Which variables seem to be related?
- Can some behavior be predicted or explained?
- What decision can be made from the evidence?
The Data Analysis cycle
A good analysis usually follows a logical sequence.
This cycle helps avoid hasty conclusions and improves the quality of the results.
1. Question
Every analysis starts with a well-defined question. Without a clear question, it is easy to produce charts that look nice but are of little use.
2. Collection
Data can come from spreadsheets, databases, forms, information systems, APIs, surveys, sensors or administrative records.
3. Cleaning
Before analyzing, you need to fix errors, handle missing values, standardize names, remove duplicates and check for inconsistencies.
4. Exploration
Exploratory analysis looks for patterns, distributions, relationships between variables and possible outliers.
5. Modeling
When needed, statistical models or algorithms are used to explain, predict or classify phenomena.
6. Communication
Results must be presented clearly, visually and honestly, with conclusions that the data can support.
Data, information and decision
A table full of numbers is not necessarily information yet.
We can think in three levels:
| Level | Description | Example |
|---|---|---|
| Data | Raw record, not yet interpreted. | Service time: 12 minutes. |
| Information | Data organized and put in context. | The mean service time was 12 minutes. |
| Decision | Action taken based on the interpretation of the information. | Add staff during the busiest hours. |
Types of data
Before choosing a statistical technique or a chart, you need to understand the type of variable being analyzed.
| Variable type | Description | Examples |
|---|---|---|
| Nominal qualitative | Categories with no natural order. | Sex, color, city, product type. |
| Ordinal qualitative | Categories with a natural order. | Low, medium or high satisfaction. |
| Discrete quantitative | Countable numeric values. | Number of children, number of service requests. |
| Continuous quantitative | Numeric values measured on a continuous scale. | Time, weight, height, income, temperature. |
Data cleaning
Data cleaning is one of the most important stages of the analysis.
It often takes more time than the modeling itself.
Some common problems are:
- missing values;
- names written in different ways;
- dates in inconsistent formats;
- numbers stored as text;
- duplicate records;
- typos;
- impossible values;
- mixed units of measurement.
Simple example
Imagine a column called city with the following values (Petrolina is a city in the Brazilian state of Pernambuco, abbreviated PE):Missing values
Missing values appear when some piece of information was not recorded.
They can come from a system failure, human error, a refusal to answer, a lost file, a collection problem, or simply because the information does not apply to the case.
| Situation | Possible treatment |
|---|---|
| Few missing values | Consider removing the rows, if this does not harm the analysis. |
| Many missing values | Investigate the cause and assess whether the variable is still reliable. |
| Informative missingness | Create a specific category or indicator for missing values. |
| Incomplete numeric data | Use imputation with the mean, the median or more advanced methods, when it makes sense. |
Outliers
Outliers are observations that lie very far from the general pattern of the data.
They can represent:
- a typo;
- a measurement failure;
- a rare but genuine case;
- a real change in the process;
- an extreme event.
A common rule for flagging possible outliers uses the interquartile range:
Here, \(Q_1\) and \(Q_3\) are the first and third quartiles: 25% of the data lie below \(Q_1\) and 75% below \(Q_3\). There are several methods for computing quartiles, and different software may give slightly different values for the same data.
The lower and upper fences are:
Values below \(LF\) or above \(UF\) can be investigated as possible outliers.
Exploratory Data Analysis
Exploratory Data Analysis, also known as EDA, is the stage in which we investigate the data before applying more complex models.
At this stage, we use statistics, tables and charts to understand:
- the distribution of the variables;
- means, medians and spread;
- patterns by group;
- relationships between variables;
- the presence of outliers;
- seasonality or trends;
- inconsistencies in the data.
Useful descriptive measures
Some measures come up often in Data Analysis.
| Measure | Formula or idea | Interpretation |
|---|---|---|
| Mean | $$\bar{x} = \frac{1}{n}\sum_{i=1}^{n}x_i$$ | Average value of the data set. |
| Median | Middle value of the sorted data. | A center that resists extreme values. |
| Standard deviation | $$s = \sqrt{\frac{\sum_{i=1}^{n}(x_i-\bar{x})^2}{n-1}}$$ | Degree of spread of the data. |
| Coefficient of variation | $$CV = \frac{s}{\bar{x}}$$ | Spread relative to the mean. It only makes sense for positive data (mean greater than zero). |
| Correlation | Ranges from −1 to 1. | Strength and direction of the linear relationship between two variables. |
Data visualization
Charts are essential tools for seeing patterns that a table can hide.
| Chart | When to use it? |
|---|---|
| Bar chart | Compare categories. |
| Histogram | See the distribution of a quantitative variable. |
| Boxplot | Compare distributions and spot possible outliers. |
| Line chart | Follow changes over time. |
| Scatter plot | Look at the relationship between two quantitative variables. |
| Heat map | Show intensity, correlations or patterns in a matrix. |
Relationships between variables
Many analyses try to find out whether two or more variables are related.
For example:
- income and years of schooling;
- study time and grade;
- waiting time and satisfaction;
- price and quantity sold;
- age and blood pressure.
For two quantitative variables, a common measure is the Pearson correlation:
The general interpretation is:
| Value of r | Interpretation |
|---|---|
| Close to 1 | Strong positive linear relationship. |
| Close to −1 | Strong negative linear relationship. |
| Close to 0 | No strong linear relationship. |
Data modeling
Once we understand the data, we can use models to explain or predict phenomena.
A model is a simplified representation of reality.
For example, in a simple linear regression we can write:
Where:
- \(Y\) is the response variable;
- \(X\) is the explanatory variable;
- \(\beta_0\) is the intercept;
- \(\beta_1\) is the slope;
- \(\varepsilon\) is the random error.
The idea is to study how \(Y\) changes when \(X\) varies.
Linear regression
Linear regression is one of the best-known techniques in Data Analysis.
It can be used to answer questions such as:
- Does study time help explain the grade?
- Does income vary with years of schooling?
- Does waiting time affect satisfaction?
- Does price affect the quantity sold?
In a simple linear regression:
Here, \(b_0\) and \(b_1\) are the estimates of \(\beta_0\) and \(\beta_1\) computed from the data, and \(\hat{Y}\) is the value predicted by the line.
The difference between the observed value and the predicted value is called the residual:
The Linear Regression page shows how to compute \(b_0\) and \(b_1\) by least squares, with an example worked out by hand.
Evaluation metrics
When we use predictive models, we need to evaluate how well they perform.
| Metric | Used for | Idea |
|---|---|---|
| MAE | Regression | Mean absolute error. |
| RMSE | Regression | Root mean squared error. |
| R² | Regression | Proportion of the variability explained by the model. |
| Accuracy | Classification | Proportion of correct classifications. |
| Precision | Classification | Among the predicted positives, how many were actually positive. |
| Recall | Classification | Among the actual positives, how many were identified. |
Mean absolute error
Root mean squared error
Coefficient of determination
In all these formulas, \(y_i\) is the observed value, \(\hat{y}_i\) is the value predicted by the model and \(n\) is the number of observations. In \(R^2\), \(SS_{res} = \sum_{i=1}^{n}(y_i-\hat{y}_i)^2\) is the residual sum of squares and \(SS_{tot} = \sum_{i=1}^{n}(y_i-\bar{y})^2\) is the sum of squared deviations around the mean \(\bar{y}\).
Training and testing
In predictive modeling, the data are commonly split into two parts:
| Set | Role |
|---|---|
| Training | Used to fit the model. |
| Test | Used to evaluate the model's performance on unseen data. |
This split helps check whether the model learned general patterns or just memorized the data.
Overfitting and underfitting
| Problem | What does it mean? | Common symptom |
|---|---|---|
| Underfitting | The model is too simple to capture the pattern in the data. | High error on both training and test sets. |
| Overfitting | The model fits the training data too closely. | Low error on the training set and high error on the test set. |
| Good fit | The model captures general patterns without memorizing noise. | Reasonable performance on both training and test sets. |
Practical example: analyzing a front desk
Imagine a data set with service times at a front desk.
Some of the variables could be:
| Variable | Type | Possible analysis |
|---|---|---|
| Date | Temporal | Analyze volume by day, month or hour. |
| Staff member | Categorical | Compare the number of customers served by each person. |
| Waiting time | Continuous quantitative | Compute the mean, median, quartiles and outliers. |
| Service time | Continuous quantitative | Study spread, distribution and bottlenecks. |
| Type of service | Categorical | Compare demand by the nature of the service. |
A first question could be:
At what times of day does the waiting time go up?
To answer it, we could compute the mean waiting time for each hour:
Here, \(x_1, x_2, \ldots, x_n\) are the waiting times recorded in that hour and \(n\) is how many customers were served in it.
Then we could compare the hours in a table or a line chart.
An example of analytical reasoning
Suppose the following waiting times, in minutes:
The mean is:
On average, each person waited about 13.33 minutes (the exact value is 80/6 = 40/3 minutes).
The six values are already in ascending order. Since the number of values is even, the median is the mean of the 3rd and 4th values:
Half of the waits lasted up to 11 minutes and the other half, 11 minutes or more.
In this case, the mean came out larger than the median because of the value 30. Without it, the mean of the five remaining values would be \(\frac{5+8+10+12+15}{5} = \frac{50}{5} = 10\) minutes: a single extreme value pulled the mean up by more than 3 minutes, while the median changed less (from 11 to 10).
Tools for Data Analysis
Data Analysis can be done with different tools.
| Tool | Common use |
|---|---|
| Excel or Google Sheets | Quick analyses, tables, filters and simple charts. |
| SQL | Querying, organizing and extracting data from relational databases. |
| Python | Automation, cleaning, modeling, data science and machine learning. |
| R | Statistical analysis, charts and modeling. |
| Julia | Scientific computing, Statistics, simulations and high-performance visualizations. |
| Power BI | Dashboards, reports and visual analysis of indicators. |
Good practices
A reliable analysis requires technical care and intellectual honesty.
Some good practices are:
- document the steps of the analysis;
- keep the original data;
- explain the cleaning criteria;
- check units of measurement;
- analyze missing data;
- avoid conclusions beyond what the data allow;
- use clear charts;
- compare the mean and the median;
- validate models on separate data;
- communicate uncertainty when it exists.
Care in interpretation
Data Analysis can lead to wrong conclusions when it is done without context.
Some important precautions:
| Common mistake | Why is it dangerous? |
|---|---|
| Confusing correlation with causation | Two variables can be associated without one causing the other. |
| Ignoring missing values | Missingness can distort results or reveal collection problems. |
| Removing outliers without justification | Extreme values can represent important real phenomena. |
| Choosing only favorable charts | This can create a biased narrative. |
| Using the mean for everything | In skewed data, the median may represent the center better. |
| Ignoring context | Numbers without context can lead to wrong interpretations. |
Overall summary
| Stage | Goal |
|---|---|
| Define the question | Establish the problem to be investigated. |
| Collect data | Obtain the information needed for the analysis. |
| Clean data | Fix errors, handle missing values and standardize records. |
| Explore data | Understand distributions, patterns and relationships. |
| Visualize data | Communicate information with charts and tables. |
| Model data | Explain, predict or classify phenomena. |
| Evaluate results | Check quality, uncertainty and limitations. |
| Communicate conclusions | Turn the analysis into understanding and decisions. |
Data Analysis is a bridge between numbers and decisions. It requires technique, organization, interpretation and critical thinking. More than applying formulas or producing charts, analyzing data means understanding a problem, investigating evidence and communicating conclusions in a clear, honest and useful way.