Programming Paradigms

Similar documents
3. The object system(s)

R: Learning by Exapmle Data Management and Analysis

Lecture 27: Nov 26, S3 Programming. OOP S3 Objects Unpaired (Two Sample) t-test. James Balamuta STAT UIUC

Practice in R. 1 Sivan s practice. 2 Hetroskadasticity. January 28, (pdf version)

A (very) brief introduction to R

EPIB Four Lecture Overview of R

Week 8 Lecture: Programming

S3 Class. Andrew Robinson ACERA & University of Melbourne. August 15, 2012

Section 2.2: Covariance, Correlation, and Least Squares

References R's single biggest strenght is it online community. There are tons of free tutorials on R.

Gelman-Hill Chapter 3

that is, Data Science Hello World.

S CHAPTER return.data S CHAPTER.Data S CHAPTER

Exercise 2.23 Villanova MAT 8406 September 7, 2015

Introduction to R, Github and Gitlab

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Lab #13 - Resampling Methods Econ 224 October 23rd, 2018

Handling Missing Values

Applied Statistics and Econometrics Lecture 6

36-402/608 HW #1 Solutions 1/21/2010

OOPs: The Harsh Realities of Programming

Section 2.3: Simple Linear Regression: Predictions and Inference

Getting started with simulating data in R: some helpful functions and how to use them Ariel Muldoon August 28, 2018

Object-Oriented Modeling Using UML. CS151 Chris Pollett Aug. 29, 2005.

COURSE OUTLINE. Faculty of Computing, Universiti Teknologi Malaysia

CMSC201 Computer Science I for Majors

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Predictive Checking. Readings GH Chapter 6-8. February 8, 2017

Programming for Engineers in Python

Administrivia. IBM Info Session Date: Wed,, Jan 13 Time: 5:30 7 pm Location: Wesbrook 100

Getting Started in R

Poisson Regression and Model Checking

Contrasts. 1 An experiment with two factors. Chapter 5

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Statistics 251: Statistical Methods

Getting Started in R

1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE

Structured Programming

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

STAT Statistical Learning. Predictive Modeling. Statistical Learning. Overview. Predictive Modeling. Classification Methods.

610 R12 Prof Colleen F. Moore Analysis of variance for Unbalanced Between Groups designs in R For Psychology 610 University of Wisconsin--Madison

Among those 14 potential explanatory variables,non-dummy variables are:

Statistical Programming with R

Object Oriented Programming. Michał Bereta

Chapter 1: Principles of Programming and Software Engineering

CS121/IS223. Object Reference Variables. Dr Olly Gotel

S T R U C T U R A L M O D E L I N G ( M O D E L I N G A S Y S T E M ' S L O G I C A L S T R U C T U R E U S I N G C L A S S E S A N D C L A S S D I A

Inheritance and Polymorphism

Week 8 Lecture: Getting Things Done

Comparing Fitted Models with the fit.models Package

Stat 5303 (Oehlert): Response Surfaces 1

Section 2.1: Intro to Simple Linear Regression & Least Squares

An Introductory Guide to R

Lecture Notes on Programming Languages

CSC207 Week 2. Larry Zhang

Introduction to Java and OOP. Hendrik Speleers

Regression on the trees data with R

Introduction to Computers and Programming Languages. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Object Oriented Programming

Object Orientated Analysis and Design. Benjamin Kenwright

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Building custom components IAT351

SEEM4570 System Design and Implementation. Lecture 11 From Design to Implementation

1 Lab 1. Graphics and Checking Residuals

1 State, objects, and abstraction

CS 320: Concepts of Programming Languages

Risk Management Using R, SoSe 2013

Some issues with R It is command-driven, and learning to use it to its full extent takes some time and effort. The documentation is comprehensive,

Week. Lecture Topic day (including assignment/test) 1 st 1 st Introduction to Module 1 st. Practical

S Basics. Statistics 135. Autumn Copyright c 2005 by Mark E. Irwin

Regression III: Lab 4

Class diagrams and architectural design

Object-Oriented Design

DOWNLOAD OR READ : OBJECT ORIENTED PROGRAMMING IN C BY ROBERT LAFORE 3RD EDITION PDF EBOOK EPUB MOBI

DSCI 325: Handout 24 Introduction to Writing Functions in R Spring 2017

CSSS 510: Lab 2. Introduction to Maximum Likelihood Estimation

EL2310 Scientific Programming

Lecture Chapter 2 Software Development

Python Basics. Lecture and Lab 5 Day Course. Python Basics

Stat 5303 (Oehlert): Unbalanced Factorial Examples 1

JAVA: A Primer. By: Amrita Rajagopal

ES-2 Lecture: Fitting models to data

Functional Programming. Biostatistics

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

CS 200 More Classes Jim Williams, PhD

The Statistical Sleuth in R: Chapter 10

Concepts of Programming Languages

Regression Analysis and Linear Regression Models

1. Write two major differences between Object-oriented programming and procedural programming?

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

Object Fundamentals. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 2 08/30/2007

LECTURE 3: SOFTWARE DESIGN. Software Engineering Mike Wooldridge

Bernt Arne Ødegaard. 15 November 2018

Overview. Elements of Programming Languages. Objects. Self-Reference

Experiments in Imputation

Programming Paradigms in Python

Data Analysis in Paleontology Using R. Looping Basics

Assignment 0. Nothing here to hand in

Regression Lab 1. The data set cholesterol.txt available on your thumb drive contains the following variables:

NEURAL NETWORKS. Cement. Blast Furnace Slag. Fly Ash. Water. Superplasticizer. Coarse Aggregate. Fine Aggregate. Age

Transcription:

Week 10 Lecture: Programming Paradigms, Introduction to Object Oriented Programming Introduction to Programming and Geoprocessing Using R GEO6938 4172 GEO4938 4166 Programming Paradigms Procedural Programming Also known as imperative Functional Programming Similar to procedural, but no reliance on shared state Object Oriented Programming Everything is an object 1

Programming Paradigms Object Oriented Programming (OOP) Everything is an object Examples: Primarily OO: Java, C# (.NET) Can be use as OO: C++, Python, Ruby, R Objects have: Properties Methods Inheritance It s all about nouns and verbs but really about the nouns, and abstraction, polymorphism, and inheritance. it 2

Abstraction and inheritance via Is A relationships Unified Modeling Language (UML) Class Diagram Has A Relationships Engine Car -Color -VIN -... +steer() 1 1..* Wheel Seat 4 3

Inheritance From Parent/Super Class UML Specifcs: Two Types of Has A Relationships Aggregation: Car -Color -VIN -... +steer() 1 1 Engine 1..* Wheel Seat 4 4

UML Specifcs: Two Types of Has A Relationships Composition: Bank Account 1 0..* Object Oriented ness Object Oriented Programming puts the Nouns first and foremost. Why would you go to such lengths to put one part of speech on a pedestal? Why should one kind of concept take precedence over another? It's not as if OOP has suddenly made verbs less important in the way we actually think. It's a strangely skewed perspective. Steve Yegge, 2006 5

Why use OOP at all? In R, OOP is Optional But exists behind even the most basic functionality Implemented through the use of generic functions 6

Basic Concepts and Vocabulary in R Class: A description of a thing Object: An instance of a class Generic function: A function which dispatches methods Encapsulates a generic concept (e.g. print(), summary(), predict()) Does not actually do anything Method: Implementation of a generic function for an object of a particular class S3 vs. S4 Systems of Objects in R The S language (and therefore R) has two systems S3 and S4, may be used independently S3 objects, classes and methods have been available in R since 1992 More informal and interactive S4 system is newer (1998), more formal and rigorous from an OOP standpoing Provided by the methods package (part of base since 1.7.0) 7

S3 vs. S4 generic functions > summary # - S3 generic function function (x,...) UseMethod("summary") <environment: namespace:base> > show # - S4 generic function standardgeneric for "show" defined from package "methods" function (object) standardgeneric("show") <environment: 0x8d7cdc8> Methods may be defined for arguments: object Following are based on an OOP in R lecture by David Perlman, 2009 The summary() function: > summary function (object,...) UseMethod("summary") <environment: namespace:base> Defined as: > summary <- function (object,...) UseMethod("summary") 8

How does it work? > x <- as.factor(rep(c("a","b"),c(7,13))) > class(x) [1] "factor" > summary(x) a b 7 13 y <- rnorm(20) > class(y) [1] "numeric" > summary(y) Min. 1st Qu. Median Mean 3rd Qu. Max. -1.78300-0.55330 0.06459 0.01969 0.66960 1.72900 How does it work? > lm.demo <- lm(y~x) > class(lm.demo) [1] "lm" > summary(lm.demo) Call: lm(formula = y ~ x) Residuals: Min 1Q Median 3Q Max -1.7500-0.6665-0.1008 0.5537 1.5156 Coefficients: Estimate Std. Error t value Pr(> t ) (Intercept) 0 3769 0 3609 1 044 0 310 (Intercept) -0.3769 0.3609-1.044 0.310 xb 0.6102 0.4477 1.363 0.190 Residual standard error: 0.9549 on 18 degrees of freedom Multiple R-squared: 0.09355, Adjusted Rsquared: 0.04319 F-statistic: 1.858 on 1 and 18 DF, p-value: 0.1897 9

Method Dispatch: summary() > methods(summary) [1] summary.aov summary.aovlist summary.aspell* [4] summary.connection summary.data.frame summary.date [7] summary.default summary.ecdf* summary.factor [10] summary.glm summary.infl summary.lm [13] summary.loess* summary.manova summary.matrix [16] summary.mlm summary.nls* summary.packagestatus* [19] summary.posixct summary.posixlt summary.ppr* [22] summary.prcomp* summary.princomp* summary.rxcrosstabs* [25] summary.rxcube* summary.rxlinmod* summary.rxlogit* [28] summary.shingle* summary.srcfile summary.srcref [31] summary.stepfun summary.stl* summary.table [34] summary.trellis* summary.tukeysmooth* Non-visible functions are asterisked Method: summary.factor() > summary.factor function (object, maxsum = 100,...) { nas <- is.na(object) ll <- levels(object) if (any(nas)) } if (any(nas)) c(tt, `NA's` = sum(nas)) else tt } <environment: namespace:base> 10

Why bother with OOP in statistical programming? Calculate mean and sd for binomial data: E(x) = np; Var(x) = np(1 p) stats <- function(x) { n = length(x) p = mean(x) mu = n*p sigma = sqrt(n*p*(1-p)) return( list(mu = mu, sigma = sigma, n = n) ) } > y1 <- rbinom(100, size=1, p=.3) > stats(y1) $mu [1] 33 $sigma [1] 4.702127 $n [1] 100 But what if we use it with the wrong type of distribution? > y2<-rnorm(100,mean=0.3,sd=1) True mean and standard deviation: > stats(y2) $mu [1] 42.52809 $sigma [1] 4.943855 $n [1] 100 > mean(y2) [1] 0.4252809 > sd(y2) [1] 1.005184 11

How do we prevent that from happening? We can use OOP of course At creation, we can define a class for our object We can then create a generic stats() function This stats() function dispatches functionality to appropriate methods based on the class of the object Define a class constructor (S3) > as.binomial <- function(x) { + class(x) <- "binomial" + return( x ) + } > as.normal <- function(x) { + class(x) <- "normal" + return( x ) + }a > y1 < as.binomial(y1) > class(y1) [1] "binomial" > y2 < as.normal(y2) > class(y2) [1] "normal" 12

Generic Function and Methods stats <- function(x) UseMethod("stats") stats.binomial <- function(x) { n = length(x) p = mean(x) mu = n*p sigma = sqrt(n*p*(1-p)) return( list(mu = mu, sigma = sigma, n = n) ) } Generic Function and Methods stats.normal <- function(x) { n = length(x) mu = mean(x) sigma = sd(x) return( list(mu = mu, sigma = sigma, n = n) ) } stats.default <- function(x) { stop("unknown data type!") } 13

Using Our stats() Function With a known class: > stats(y1) t > stats(y2) $mu $mu [1] 33 [1] 0.4252809 $sigma $sigma [1] 1.005184 [1] 4.702127 $n $n [1] 100 [1] 100 With an unknown class: > y3 <- rnorm(100,mean=2,sd=1) > stats(y3) Error in stats.default(y3) : Unknown data type! Inheritance in R A way to form new classes using classes that have already been dfi defined. d By the mechanism of inheritance, a new class (known as a derived class) takes over or inherits attributes and behavior of pre existing classes (referred to as base classes or ancestor classes) 14

Creating a Standard Normal Class as.standardnormal <- function(x){ x <- as.normal(x) class(x) <- c("standardnormal", class(x)) return( x ) } stats.standardnormal <- function(x){ object <- stats.normal(x) object$sigma <- 1 return(object) } Inheriting Methods > y4 <- rnorm(100) > y4 <- as.standardnormal(y4) > stats(y4) $mu [1] 0.03292102 $sigma [1] 1 $n [1] 100 > class(y4) [1] "standardnormal" "normal" 15

This Week in Lab: Work week for your class project development I ll post project guidelines and assignment on Sakai Assignment will be to turn in a short (less than one page) synopsis of what you intend to do I won t be there! Not required to attend 16