




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































Series editor W.K. Härdle
An Introduction to Statistics
with Python
123
Thomas Haslwanter School of Applied Health and Social Sciences University of Applied Sciences Upper Austria Linz, Austria
Series Editor: W.K. Härdle C.A.S.E. Centre for Applied Statistics and Economics School of Business and Economics Humboldt-Universität zu Berlin Unter den Linden 6 10099 Berlin Germany
The Python code samples accompanying the book are available at www.quantlet.de. All Python programs and data sets can be found on GitHub: https://github.com/thomas- haslwanter/statsintro_python.git. Links to all material are available at http://www.springer. com/de/book/9783319283159. The Python solution codes in the appendix are published under the Creative Commons Attribution-ShareAlike 4.0 International License.
ISSN 1431-8784 ISSN 2197-1706 (electronic) Statistics and Computing ISBN 978-3-319-28315-9 ISBN 978-3-319-28316-6 (eBook) DOI 10.1007/978-3-319-28316-
Library of Congress Control Number: 2016939946
© Springer International Publishing Switzerland 2016 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, express or implied, with respect to the material contained herein or for any errors or omissions that may have been made.
Printed on acid-free paper
This Springer imprint is published by Springer Nature The registered company is Springer International Publishing AG Switzerland
In the data analysis for my own research work, I was often slowed down by two things: (1) I did not know enough statistics, and (2) the books available would provide a theoretical background, but no real practical help. The book you are holding in your hands (or on your tablet or laptop) is intended to be the book that will solve this very problem. It is designed to provide enough basic understanding so that you know what you are doing, and it should equip you with the tools you need. I believe that the Python solutions provided in this book for the most basic statistical problems address at least 90 % of the problems that most physicists, biologists, and medical doctors encounter in their work. So if you are the typical graduate student working on a degree, or a medical researcher analyzing the latest experiments, chances are that you will find the tools you require here—explanation and source-code included. This is the reason I have focused on statistical basics and hypothesis tests in this book and refer only briefly to other statistical approaches. I am well aware that most of the tests presented in this book can also be carried out using statistical modeling. But in many cases, this is not the methodology used in many life science journals. Advanced statistical analysis goes beyond the scope of this book and—to be frank— exceeds my own knowledge of statistics. My motivation for providing the solutions in Python is based on two considera- tions. One is that I would like them to be available to everyone. While commercial solutions like Matlab , SPSS , Minitab , etc., offer powerful tools, most can only use them legally in an academic setting. In contrast, Python is completely free (“as in free beer” is often heard in the Python community). The second reason is that Python is the most beautiful coding language that I have yet encountered; and around 2010 Python and its documentation matured to the point where one can use it without being a serious coder. Together, this book, Python , and the tools that the Python ecosystem offers today provide a beautiful, free package that covers all the statistics that most researchers will need in their lifetime.
vii
Preface ix
with a working example for each test presented in this book. They also include the code used to generate the pictures in this book, as well as the data used to run the programs. The Python code samples accompanying the book are available at http://www. quantlet.de. All Python programs and data sets can be found on GitHub: https:// github.com/thomas-haslwanter/statsintro_python.git. Links to all material are avail- able at http://www.springer.com/de/book/9783319283159.
Python is built on the contributions from the user community, and some of the sections in this book are based on some of the excellent information available on the web. (Permission has been granted by the authors to reprint their contributions here.) I especially want to thank the following people:
Linz, Austria Thomas Haslwanter December 2015
The first part of the book presents an introduction to statistics based on Python. It is impossible to cover the whole language in 30 or 40 pages, so if you are a beginner, please see one of the excellent Python introductions available in the internet for details. Links are given below. This part is a kick-start for Python ; it shows how to install Python under Windows, Linux, or MacOS, and goes step-by-step through documented programming examples. Tips are included to help avoid some of the problems frequently encountered while learning Python. Because most of the data for statistical analysis are commonly obtained from text files, Excel files, or data preprocessed by Matlab, the second chapter presents simple ways to import these types of data into Python. The last chapter of this part illustrates various ways of visualizing data in Python. Since the flexibility of Python for interactive data analysis has led to a certain complexity that can frustrate new Python programmers, the code samples presented in Chap. 3 for various types of interactive plots should help future Pythonistas avoid these problems.
Chapter 1
Statistics is the explanation of variance in the light of what remains unexplained. Every day we are confronted with situations with uncertain outcomes, and must make decisions based on incomplete data: “Should I run for the bus? Which stock should I buy? Which man should I marry? Should I take this medication? Should I have my children vaccinated?” Some of these questions are beyond the realm of statistics (“Which person should I marry?”), because they involve too many unknown variables. But in many situations, statistics can help extract maximum knowledge from information given, and clearly spell out what we know and what we don’t know. For example, it can turn a vague statement like “This medication may cause nausea,” or “You could die if you don’t take this medication” into a specific statement like “Three patients in one thousand experience nausea when taking this medication,” or “If you don’t take this medication, there is a 95 % chance that you will die.” Without statistics, the interpretation of data can quickly become massively flawed. Take, for example, the estimated number of German tanks produced during World War II, also known as the “German Tank Problem.” The estimate of the number of German tanks produced per month from standard intelligence data was 1,550 ; however, the statistical estimate based on the number of tanks observed was 327 , which was very close to the actual production number of 342 (http://en. wikipedia.org/wiki/German_tank_problem). Similarly, using the wrong tests can also lead to erroneous results. In general, statistics will help to
© Springer International Publishing Switzerland 2016 T. Haslwanter, An Introduction to Statistics with Python , Statistics and Computing, DOI 10.1007/978-3-319-28316-6_
3