Material to accompany this classDownload the spreadsheet found here on Google Sheets. We will use this spreadsheet for the class. Download it as CSV, the most common method for storing a database that can be read by any language (Comma-Separated Values).Create a python3 development environment or use ready-made online environments such as JupyterLab online and import the database.Pandas in PythonYour folder will look like this if you use the online version:Folder in JupyterLab onlineAs you can see, we will use pandas to understand our database. To import pandas, use:import pandas as pdAnd we will open our database using read_csv:df = pd.read_csv("Trabalho de Estatística 1 - Set de dados.csv", decimal=',')Remember to use decimal="," or it will not understand numbers separated by commas as numbers, since Python generally assumes that we write like people in the United States and not in Brazil.Now use .info() or .describe() on the database so it provides us with quick data about its distribution:Example with .describe()To calculate the mean, mode, and median, use .mean(), .mode(), and .median(), respectively:Calculating the mean, mode, and median in pandasNotice that it tells you if there is more than 1 mode.Standard deviation and Coefficient of Variation (CV) can be calculated in the following ways:Standard deviation and CV in pandasNow let's move on to calculating Skewness:Skewness in PandasThe formula used by Pandas to calculate skewness is:\[ G_1 = \frac{n}{(n-1)(n-2)} \sum_{i=1}^n \left( \frac{x_i - \bar{x}}{s} \right)^3 \]Where:- \( n \) = number of observations- \( x_i \) = each individual value- \( \bar{x} \) = sample mean- \( s \) = sample standard deviation (with \( n-1 \) degrees of freedom)The `.skew()` function was implemented following Fisher's approach and is a different formula for calculating skewness compared to Pearson, with:- Correction for bias in small samples- Consistency with other popular implementations- Proper handling of missing values (NaN)Now the calculation of kurtosis:Kurtosis in pandasNow let's make the curve using a histogram:Use matplotlib to create the histogram in pythonThe final chart:Chart built with matplotlibWhat curve can you see? Here I see a J-shaped curve.You can also make a boxplot to see your data:Boxplot in matplotlibAlthough boxplot is not the most recommended with J-shaped curves.Transforming your dataSince the curve looks J-shaped, we can make transformations so that the curve becomes bell-shaped, a curve that is easier to work with. In my case, I will transform it using the logarithm:Logarithm transformation in pandasNote that the transformation can be done in several ways: we can take the logarithm, we can take the square root. In this case, I found the logarithm appropriate because it has a very pronounced right tail and no zeros. We generally transform financial values (salaries, stock prices), population sizes, system response times, and sales data with positive outliers using logarithms. And we generally transform using the square root when there are: number of website visits, accident counts, number of products sold, and event frequency data.Histogram of the log-transformed distributionLooking at the histogram, it seems we got the transformation right!Boxplot of the Log-Transformed DataIf we analyze the mean, standard deviation, skewness, and kurtosis, we can be more certain that we did well to transform it!Assignment: do it yourself!Choose a number to work with within the provided database and answer the following questions:1) What is the mean of the distribution?2) What is the median of the distribution?3) Does the distribution have a mode?4) What is the standard deviation of the distribution?5) What is the coefficient of variation of your dataset?6) Create the frequency distribution graph7) What is the shape of the curve? (bell-shaped curve, J-shaped curve, U-shaped curve, uniform curve, bimodal, trimodal, multimodal……)8) Analyzing the values of the variables, what possible data could they represent?9) Make the boxplot of your distribution
— Comments 0
, Reactions 1
Be the first to comment