{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pylab inline\n", "import matplotlib.pyplot as plt\n", "\n", "#import matplotlib\n", "#matplotlib.style.use('ggplot') ## gnuplot style\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "#size\n", "width, height=12,8\n", "plt.rcParams['figure.figsize'] = width, height #aggiunta pt\n", "\n", "#precision in tables\n", "pd.set_option('precision',2)\n", "#rows in tables\n", "pd.set_option('display.max_rows', 1000)\n", "\n", "# this is used for regression below; install (via pip) statsmodels and patsy\n", "import statsmodels.formula.api as sm" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# to chose a csv file in the current folder\n", "import os\n", "filesHere=os.listdir(\"./\")\n", "selected=[]\n", "for i in range(len(filesHere)):\n", " if filesHere[i].find('_ts.csv')>0: selected.append(filesHere[i])\n", "for i in range(len(selected)):\n", " print (i, selected[i])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "num=int(input(\"Choose a file via its number (>=0;<=\"+str(len(selected)-1)+\" \"))\n", "\n", "par_df = pd.read_csv(selected[num][:17]+'_par.csv')\n", "par_df.index += 1 \n", "\n", "ts_df = pd.read_csv(selected[num])\n", "#set index to start from 1, data are collected at the end of each period\n", "ts_df.index += 1 \n", "\n", "str_df = pd.read_csv(selected[num][:17]+'_str.csv')\n", "#leave index to start from 0, data are collected at the beginning of each period" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***Parameters***" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "par_df.astype(str,errors='ignore')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***Time series, data collected at the end of each period***" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ts_df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ts_df.describe()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "ts_df.corr(method=\"pearson\").style.format(\"{:.2}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = sm.ols(formula=\"totalProfit ~ price + wage + totalProduction + unemployed\", \\\n", " data=ts_df).fit()\n", "\n", "print (result.summary())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***Structural infos, data collected at the beginning of each period***" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str_df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "myPlot = ts_df.plot(secondary_y=['price','wage'],\n", "style=['2-','+:','^-','.:','|-','4:'],color=[\"OrangeRed\",\"LawnGreen\",\"Blue\",\"Violet\",\"Gray\",\"Brown\"])\n", "myPlot.set_ylabel('unemployed, totalProfit, totalProduction, plannedProduction')\n", "myPlot.right_ax.set_ylabel('price, wage')\n", "myPlot.legend(loc='upper left') #, bbox_to_anchor=(-0.35, 0.5)\n", "myPlot.axes.right_ax.legend(loc='lower right') #, bbox_to_anchor=(1.1, 0.5)\n", "\n", "# within style the sequence is marker+style as codes at \n", "# http://matplotlib.org/1.4.1/api/axes_api.html#matplotlib.axes.Axes.plot\n", "# looking for\n", "# 'The following format string characters are accepted to control the line style or marker:'\n", "# look also to \n", "# https://matplotlib.org/examples/lines_bars_and_markers/marker_reference.html\n", "# but not all the combination are working" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str_df.plot(secondary_y='workers',style=['^-','.-.'],color=[\"r\",\"b\"])\n", "# within style the sequence is marker+style as codes at \n", "# http://matplotlib.org/1.4.1/api/axes_api.html#matplotlib.axes.Axes.plot\n", "# looking for\n", "# 'The following format string characters are accepted to control the line style or marker:'\n", "# look also to \n", "# https://matplotlib.org/examples/lines_bars_and_markers/marker_reference.html\n", "# but not all the combination are working" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Best solutions to produce a LaTeX table from these data (the example is related to ts_df.corr table):\n", "\n", " corr=ts_df.corr(method='pearson')\n", " print corr.to_latex()\n", " \n", " \"print\" to have the output nicely formatted; copy and paste it to LaTeX and the \n", " result works.\n", " \n", "To use caption and label with tabular simply include that output within:\n", "\n", " \\begin{table}\n", " \n", " output above\n", " \n", " \\label{a label}\n", " \\caption{a caption}\n", " \\end{table}\n", "\n", " You can add the usual [htbp] specification as \\begin{table}[htbp]\n", "\n", "\n", "Other solutions:\n", "1. online [http://www.tablesgenerator.com](http://www.tablesgenerator.com), reading the csv file;\n", "2. using a converter as [http://html2latex.sourceforge.net](http://html2latex.sourceforge.net).\n", "\n", "## The first method is applied in the cell below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "corr=ts_df.corr(method='pearson')\n", "print (corr.to_latex())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }