Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Writing Functions In R
Necessity Really is a Mother
Paul E. Johnson12
1
University of Kansas, Department of Political Science 2 Center for Research Methods and Data Analysis
2012
Writing Functions I
1 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Outline
1 2 3 4 5 6 7 8 9
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetition Bootstrapping
Writing Functions I
2 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
If Yes: Rs dierent than that. If No: Its not like other things youve done. In either case, dont worry about it :)
Writing Functions I
3 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
4 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
The S Language John Chambers, et al. at Bell Labs, mid 1970s. See Richard Beckers Brief History of S about the AT&T years There have been 4 generations of the S language. Now we are in a transition between S3 and S4 Current rumor a new programming approach dubbed R5 will supercede S4 S3: The New S Language 1988
Writing Functions I
5 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Is R a Branch from S?
Or is S coming back to R?
Is R a branch from S? Ross Ihaka and Robert Gentleman. 1996. R: A language for data analysis and graphics. Journal of Computational and Graphical Statistics, 5(3):299-314. Open Source, Open Community S4: John Chambers,Software for Data Analysis: Programming with R, Springer, 2008
Writing Functions I
6 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Do NOT write a giant sequence of 1000 commands Many novices knit together a giant sequence of commands. Just Dont! Problem No other human can comprehend that mess Solution Write functions to calculate separate parts
Writing Functions I
7 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
myfn1 < f u n c t i o n ( parms ) { l i n e s here } myfn2 < f u n c t i o n ( parms ) { l i n e s here } # # perfect above , don ' t edit it again # # At end , the part you revise while working a < 7 b < c ( 4 , 4 , 4 , 4 , 2 ) g r e a t 1 < myfn1 ( a , b , parm3 = TRUE) g r e a t 2 < myfn2 ( b , g r e a t 1 , parm9 = FALSE )
Writing Functions I
8 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Instead of this:
f o r ( i i n 1 : 1000}{ 1000 s o f l i n e s h e r e } full o f x [ i ] , z [ i ] , and s o f o r t h
We Want:
f n 1 < f u n c t i o n ( params ) { . . . } f n 2 < f u n c t i o n ( params ) { . . . } f o r ( i i n 1 : 1000}{ fn1 ( x [ i ] , . . . ) }
Writing Functions I
9 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
We should organize our information here in the current environment We send it over there to a function We get back something we can work with The function DOES NOT change variables we give to the function The super assignment << allows an exception to this, but R Core recommends we avoid it when possible.
Writing Functions I
10 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
doubleMe < f u n c t i o n ( i n p u t = 0 ) { n e w v a l < 2 * i n p u t } The functions name is doubleMe I am allowed to name the incoming variable input anything I want Could as well have used: doubleMe < f u n c t i o n ( x ) { o u t < 2 * x } Note, explicit use of a return function is NOT REQUIRED.
Writing Functions I
11 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
doubleMe < f u n c t i o n ( i n p u t = 0 ) { n e w v a l < 2 * i n p u t } input is a name to be used INTERNALLY while making calculations = 0 An optional but recommended default value newval Last calculation is returned.
doubleMe a name with which to access this function. Because of my background in Objective C, I like this style of name. C++ programmers prefer double.me or such.
Writing Functions I
12 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
What is 2 * 7? ( doubleMe ( 7 ) ) [ 1 ] 14
Writing Functions I
13 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
This works doubleMe ( 1 0 ) But wouldnt you rather be clear? doubleMe ( i n p u t =10)
Writing Functions I
15 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Function Calls
But if you name it wrong, it breaks > doubleMe ( myInput =7) E r r o r i n doubleMe ( myInput = 7 ) : u nu s e d argument ( s ) ( myInput = 7 ) What if you feed it something unsuitable? > doubleMe ( lm ( rnorm ( 1 0 0 )rnorm ( 1 0 0 ) ) ) E r r o r i n 2 * i n p u t : nonnumeric argument t o binary operator I n a d d i t i o n : Warning m e s s a g e s : 1 : I n m o d e l . m a t r i x . d e f a u l t ( mt , mf , c o n t r a s t s ) : t h e r e s p o n s e a p p e a r e d on t h e r i g h t h a n d s i d e and was d r o p p e d 2 : I n m o d e l . m a t r i x . d e f a u l t ( mt , mf , c o n t r a s t s ) : p r o b l e m w i t h term 1 i n m o d e l . m a t r i x : no columns a r e a s s i g n e d
Writing Functions I 16 / 205 University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
But it wont allow you to specify too many inputs: > doubleMe ( 1 , 2 , 3 , 4 , 5 ) E r r o r i n doubleMe ( 1 , 2 , 3 , 4 , 5 ) : u n u s e d argument ( s ) ( 2 , 3 , 4 , 5 ) Oops. I forgot the input doubleMe ( ) Gives the default value.
Writing Functions I
17 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Function Calls
Oops. I forgot the parentheses doubleMe f u n c t i o n ( i n p u t = 0) { n e w v a l < 2 * i n p u t } Tip: Code for most functions can be reviewed by typing its name. R will beautify the format and show you what it sees in your function. Type lm if you dont believe it. Note, after all this, that input does not exist in the current environment. > l s () [ 1 ] doubleMe op > input Error : object ' input
Writing Functions I
p j m a r
'
not found
University of Kansas
18 / 205
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Write a function myGreatFunction that takes a vector and returns a vector of 3 values: the maximum, the minimum, and the median. Generate some data, for testing, and run x1 < rnorm ( 1 0 0 0 0 , m=7, s d =19) m y G r e a t F u n c t i o n ( x1 ) Now stress test your function by changing x1 x1 [ c ( 1 3 , 4 4 , 9 9 , 3 4 3 , 5 5 5 ) ] m y G r e a t F u n c t i o n ( x1 ) < NA
Writing Functions I
19 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Entropy in Physics means disorganization Sometimes called Shannons Information Index Basic idea. List the possible outcomes and their probabilities The amount of diversity in a collection of observations depends on the equality of the proportions of cases observed within each type.
Writing Functions I
20 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
This distribution is less outcome name t1 prob(outcome) 0.1 than this distribution: outcome name t1 prob(outcome) 0.2
t4 0.55 t4 0.2
t5 0.0 t5 0.2
Writing Functions I
21 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
(1)
pt log2 (pt )
t=1
(2)
Writing Functions I
22 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
log2 ( p ) , 0)
Writing Functions I
23 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
24 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Diversity: plog2(p)
Diversity Contribution of One Observation 0.1 0.2 0.3 0.4 0.5 0.0 0.0
0.2
0.4 p
0.6
0.8
1.0
Writing Functions I
25 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Diversity Function
Dene an Entropy function that sums those values e n t r o p y < f u n c t i o n ( p ) { sum ( d i v r ( p ) ) } Calculate some test cases e n t r o p y ( c ( 1/ 5 , 1/ 5 , 1/ 5 , 1/ 5 , 1/5 ) ) [ 1 ] 2 .321928 e n t r o p y ( c ( 3/ 5 , 1/ 5 , 1/ 5 , 0/ 5 , 0/5 ) ) [ 1 ] 1 .370951
Writing Functions I
26 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
(3)
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
maximumEntropy < f u n c t i o n (N) log2 ( 1 /N) Nmax < 15 M < 2 : Nmax p l o t (M, maximumEntropy (M) , x l a b=N o f P o s s i b l e Types , y l a b=Maximum P o s s i b l e D i v e r s i t y , main=Maximum P o s s i b l e E n t r o p y For N C a t e g o r i e s , t y p e=h , a x e s=F ) axis (1) axis (2) p o i n t s (M, maximumEntropy (M) , pch =19)
Writing Functions I
28 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
3.5
q q q
1.0
1.5
8 10 N of Possible Types
12
14
Writing Functions I
29 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
30 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
31 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
0.4
0.3
0.3
0.4
0.4
0.4
0.25
0.21
0.2
0.2
0.2
0.2
0.18
0.14
0.1
0.1
0.1
0.11
0.07
0.04
0.0
0.0
9 10
0.0
Writing Functions I
32 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
testcase3v 7 : 7 .0000 6 : 6 .0000 5 : 5 .0000 4 : 4 .0000 ( A l l Others ) : 6 .0000 NA ' s : 0 .0000 entropy : 2 .6100 norme dEn tro py : 0 . 9 2 9 7 N :28 .0000
Writing Functions I
34 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Function Anatomy
what1, what2, and what3 become local variables inside the function.
Writing Functions I
35 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
36 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
someWork <
In R, everything is an object what1, what2, what3 can be . ANYTHING! That is a blessing and a curse
Blessing: Flexibility! Let an argument be a vector, matrix, list, whatever. The function declaration does not care. Curse: Diculty managing the innite array of possible ways users might break your functions.
Writing Functions I
37 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
38 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
I dont know, but . . . I used to be very worried that R functions would reach outward and nd values for underned variables A conservative strategy in that case is to set all defaults at NULL or someother value.
f u n c t i o n ( what1 = NULL , what2 = NULL , what3 = NULL , what4 = NULL)
That way, if a user forgets to provide what3 then the system will , not go looking for it.
Writing Functions I
39 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
missing, stop, and stopifnot missing. Inside a function, missing is a way to ask if an argument is undened.
d o S o m e t h i n g < f u n c t i o n ( what1 , what2 , what3 , what4 ){ i f ( m i s s i n g ( what1 ) ) s t o p ( ` ` you f o r g o t t o s p e c i f y what1 ' ' ) }
Writing Functions I
40 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
42 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
A list may include numbers, characters, vectors ,etc or data frames or other lists Read code for function lm
Writing Functions I
43 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
This function returns an object aRegMod < f u n c t i o n ( x1 , x2 , y ) { lm ( yx1+x2 ) } That is a single object but it is a very complicated structure , Inside aRegMod there are parameter estimates, whole data matrices, and such.
Writing Functions I
44 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
47 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Ill throw together a funtion that runs a regression and returns a bunch of characteristics: aRegThing returns a regression model (and everything in it) a regression summary object a separate numeric estimate of b2 the estimated standard error of b2 a vector of t values and the Rsquare.
Writing Functions I
48 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
aRegThing < f u n c t i o n ( x1 , x2 , y ) { mymod < lm ( yx1+x2 ) mysum < summary (mymod) b 2 h a t < c o e f (mymod) [ 3 ] b 2 h a t s e < c o e f ( mysum ) [ 2 , 3 ] a l l t v a l s < c o e f ( mysum ) [ , 3 ] r s q < mysum$ r . s q u a r e l i s t ( t h e M o d e l = mymod , theSummary = mysum , e s t b 2 = b2hat , s e b 2 h a t = b 2 h a t s e , a l l t = a l l t v a l s , rsquare = rsq ) }
Writing Functions I
49 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
x1 < rnorm ( 1 0 0 0 ) x2 < rnorm ( 1 0 0 0 ,m=44 , s d =7) y < 4 + 5 * x1 + 0 . 2 * x2 + rnorm ( 1 0 0 0 , m=0, s d =63) aRegThing ( x1 , x2 , y )
Writing Functions I
50 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
m y l i s t < v e c t o r ( mode = ` ` l i s t ' ' , l e n g t h = 1 0 0 0 ) for ( i in 1:1000) { x1 < rnorm ( 1 0 0 0 ) x2 < rnorm ( 1 0 0 0 ,m=44 , s d =7) y < 4 + 5 * x1 + 0 . 2 * x2 + rnorm ( 1 0 0 0 , m=0, s d =63) m y L i s t [ [ i ] ] < aRegThing ( x1 , x2 , y )
Writing Functions I
51 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
If you nd ourself using copy and paste to repeat stanzas with slight variations, you are almost certainly doing the wrong thing. Re-conceptualize, write a function that does the right thing Use the function over and over Why is this important: AVOIDING mistakes due to editing mistakes
Writing Functions I
52 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
A Bad Example
I see a lot of user code that needs to be tightened up.
# # Writes Mplus code to g en er a te the data ## # # then runs the Mplus code it g e n e r a t e s ## # ------------------Clear All-------------------------- # rm ( l i s t = l s ( ) ) # ---------------SPECIFICATIONS------------------------ # # Specify root d i r e c t o r y and lo c at i on of mplus ; use double slashes ; leave end open d i r r o o t < D: \ \ U s e r s \\ u s e r n a m e \\ D e s k t o p \\ s i m u l a t i o n # where to place data , etc. i t e r =1000 # how many i t e r a t i o n s per condition s e t . s e e d (7913025) # set random seed # --------------END SPECIFICATIONS--------------------- # # n per cluster sample size f o r ( p e r c l u s t in c (100) ) { # number of c lu s te r s - later for MLM a p p l i c a t i o n for ( nclust in c (1) ){ # common c o r r e l a t i o n for ( setcorr in c (1:8) ){
Writing Functions I 53 / 205 University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
61 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
65 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
} c a t ( ' SEED = ' , r o u n d ( r u n i f ( 1 ) * 1 0 0 0 0 0 0 0 ) , ' ; \n ' , f i l e = s m c a r m i s s , append=T) c a t ( ' RUN ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%end ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%mend ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%s a m p l e s i z e \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' \n ' , f i l e =s m c a r m i s s , append=T)
Writing Functions I
71 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
72 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
74 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
75 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
78 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
79 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
81 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
82 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
84 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
86 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
87 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
-----------------------------------------------------------# # - - - - - - - - - - - - - C o n v e r t . to 999 for MPLUS--------------------- # # -----------------------------------------------------------# c a t ( '%macro c o d e m i s s i n g ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%do j =1 %t o ' , f i l e =s m c a r m i s s , append=T) c a t ( p a s t e ( i t e r ) , f i l e =s m c a r m i s s , append=T) c a t ( ' ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' / * r e c o d e m i s s i n g number t o 999 * / \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' d a t a p r i n&j ; s e t p r i n&j ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' a r r a y v a r s { * } n u m e r i c ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' do i = 1 t o dim ( v a r s ) ; \n ' , f i l e =s m c a r m i s s , append=T)
Writing Functions I 90 / 205 University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
c a t ( ' i f v a r s { i } = . t h e n v a r s { i } = 9 9 9 ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' end ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' d r o p i ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' r u n ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%end ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%mend ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%c o d e m i s s i n g \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' \n ' , f i l e =s m c a r m i s s , append=T)
Writing Functions I
91 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
c a t ( '%do i =1 %t o ' , f i l e =s m c a r m i s s , append=T) c a t ( p a s t e ( i t e r ) , f i l e =s m c a r m i s s , append=T) c a t ( ' ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' p r o c p r i n c o m p d a t a=MNAR& i o u t=w o r k . p r i n& i N=7; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' v a r q1 q2 q3 q4 q5 q6 q7 q8 ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' r u n ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%end ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%mend ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%PCA \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' \n ' , f i l e =s m c a r m i s s , append=T)
Writing Functions I
96 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
-----------------------------------------------------------# # - - - - - - - - - - - - - C o n v e r t . to 999 for MPLUS--------------------- # # -----------------------------------------------------------# c a t ( '%macro c o d e m i s s i n g ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%do j =1 %t o ' , f i l e =s m c a r m i s s , append=T) c a t ( p a s t e ( i t e r ) , f i l e =s m c a r m i s s , append=T) c a t ( ' ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' / * r e c o d e m i s s i n g number t o 999 * / \n ' , f i l e =s m c a r m i s s , append=T)
Writing Functions I 98 / 205 University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
-----------------------------------------------------------# # - - - - - - - - - E x p o r t data with P r i n c i p a l Components-------------- # # -----------------------------------------------------------# c a t ( '%macro e x p o r t p c a ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%do i =1 %t o ' , f i l e =s m c a r m i s s , append=T)
Writing Functions I 99 / 205 University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
100 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
102 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
105 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
c a t ( '%end ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%mend ; \n ' , f i l e =s m c a r m i s s , append=T) c a t ( '%c o d e m i s s i n g \n ' , f i l e =s m c a r m i s s , append=T) c a t ( ' \n ' , f i l e =s m c a r m i s s , append=T)
Writing Functions I
107 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
f i l e =ana ,
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
c a t ( ' ; \n ' , f i l e =ana , append=T) c a t ( 'MODEL: \n ' , f i l e =ana , append=T) c a t ( ' [ x * 0 y * 0 ] ; \n ' , f i l e =ana , append=T) c a t ( ' x * 1 ; y * 1 ; \n ' , f i l e =ana , append=T) c a t ( ' x w i t h y * . 5 0 ; \n ' , f i l e =ana , append=T) c a t ( ' OUTPUT: \n ' , f i l e =ana , append=T) # ######################################### # RUN AN A LY S IS CODE # # ######################################### s h e l l ( p a s t e ( m p l u s . e x e , ana , p a s t e ( path , s a v e o u t . o u t , s e p= \\ ) , s e p= ) ) # -------------------------------------------------------------#
Writing Functions I
112 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
113 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Weird indentation Use /, not backslash even on Windows , Use vectors Prolic copying and pasting of cat lines. Avoid system except where truly necessary. R has OS neutral functions to create directorys and such.
Writing Functions I
114 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Heres a danger I perceive: 1 student writes a program that runs , and passes it on to the others. My suspicion: Students like complicated, unreadable code because they think nobody will ever check their work.
Writing Functions I
115 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
f i l e =gen , append=T)
116 / 205 University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
f i l e =gen , append=T)
c a t ( '%WITHIN% \n ' , f i l e =gen , append=T) # b a se l in e l oa d in g s are all .3 , add .4 if ' within ' = 1 , but change based on mod / strong c a t ( 'FW BY y1y2 * ' , . 3 +( w i t h i n * ( . 4+s t r o n g * . 1 ) ) , ' \n ' , f i l e = gen , append=T , s e p= ) c a t ( ' y3y4 * ' , . 3 +( w i t h i n * ( .4mod * . 3 ) ) , ' \n ' , f i l e =gen , append=T , s e p= ) c a t ( ' y5y6 * ' , . 3 +( w i t h i n * ( . 4 s t r o n g * . 1 ) ) , ' ; \n ' , f i l e =gen , append=T , s e p= ) c a t ( ' FW@1 ; \n ' , f i l e =gen , append=T) # r e s i d u a l s are just 1 - l oa d in g 2 c a t ( ' y1y2 * ' , 1( . 3 +( w i t h i n * ( . 4+s t r o n g * . 1 ) ) ) 2 , ' ; \n ' , f i l e =gen , append=T , s e p= )
Writing Functions I
117 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
c a t ( ' y3y4 * ' , 1+( i c c * ( .0531 ) ) s q r t ( ( . 3 +( b e t w e e n * ( .4mod * . 3 ) ) ) 2 * (1+( i c c * ( .0531 ) ) ) ) 2 , ' ; \n ' , f i l e =gen , append=T , s e p= ) c a t ( ' y5y6 * ' , 1+( i c c * ( .0531 ) ) s q r t ( ( . 3 +( b e t w e e n * ( . 4 s t r o n g * . 1 ) ) ) 2 * (1+( i c c * ( .0531 ) ) ) ) 2 , ' ; \n ' , f i l e =gen , append =T , s e p= ) c a t ( ' \n ' , f i l e =gen , append=T , s e p= ) # run the above syntax using Mplus # shell ( paste (" cd " , mplus , sep =" ") ) s h e l l ( p a s t e ( m p l u s . e x e , gen , p a s t e ( path , s a v e . o u t , s e p=\\ ) , s e p= ) )
Writing Functions I
119 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
121 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
y1y2 * , 1+( i c c * ( .0531 ) ) sqrt ( ( . 3 +( b e t w e e n * ( . 4+s t r o n g * . 1 ) ) ) 2 * (1+( i c c * ( .0531 ) ) ) ) 2 , ; , y3y4 * , 1+( i c c * ( .0531 ) ) sqrt ( ( . 3 +( b e t w e e n * ( .4mod * . 3 ) ) ) 2 * (1+( i c c * ( .0531 ) ) ) ) 2 , ; , y5y6 * , 1+( i c c * ( .0531 ) ) sqrt ( ( . 3 +( b e t w e e n * ( . 4 s t r o n g * . 1 ) ) ) 2 * (1+( i c c * ( .0531 ) ) ) ) 2 , ; , \n , f i l e =gen , append=T , s e p= ) }
Writing Functions I
122 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Critique that!
Benet of re-write is isolation of code writing into a separate function We need to work on cleaning up use of cat to write les.
Writing Functions I
123 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
This is discussed in rockchalk vignette Rchaeology There is not much ocial style guidance from the R Core Team Dont mistake that as permission to write however you want. There ARE very widely accepted standards for the way that code should look
Writing Functions I
124 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
if ( ret.y ) z $ y < y i f ( ! qr ) z $ q r < NULL z } <bytec ode : 0 x1948b88> < e n v i r o n m e n t : namespace : s t a t s >
Writing Functions I
128 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
You can write messy code, but you cant make anybody read it. Finding bugs in a giant undierentiated mass of commands is dicult Chance of error rises as clarity decreases If you want people to help you, or use your code, you should write neatly!
Writing Functions I
129 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Style Fundamentals
WHITE SPACE:
indentation. R Core Team recommends 4 spaces one space around operators like <- = *
< should be used for assignments. = was used by mistake so often by novices that the R interpreter was re-written to allow =. However, it may still fail in some cases. Use helpful variable names Separate calculations into functions. Sage advice from one of my programming mentors: Dont allow a calculation to grow longer than the space on one screen. Break it down into smaller, well dened pieces.
Writing Functions I
130 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
133 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
134 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Dont use names for functions that are already in widespread use, like lm, seq, rep, etc. I like Objective C style variable and function names that smash words together, as in myRegression myCode R uses periods in function names to represent object orientation or subclassing, thus I avoid periods for simple punctuation. Ex: doSomething() is better than do.something Underscores are now allowed in function names. Ex: do something() would be OK
Writing Functions I
135 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Use clear names always Use short names where possible Never use names of common functions for variables Never name a variable T or F (doing so tramples R symbols) Name by sux strategy Im using now: m1 < m1sum m1vif m1inf lm ( y x , d a t a=d a t ) < summary (m1) < v i f (m1) < i n f l u e n c e . m e a s u r e s (m1)
Writing Functions I
136 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
A re-conceptualization of programming to reduce programmer error OO includes a broad set of ideas, only a few of which are directly applicable to R programming The rise to pre-eminance of OO indicated by the
introduction of object frameworks in existing languages (C++, Objective-C) growth of wholly new object-oriented languages (Java)
Writing Functions I
138 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Decipher R OO by Intuition
Run the command methods ( p r i n t ) What do you see? I see 170 lines, like so:
[1] [2] [3] [4] ... [167] [168] [169] [170] print.acf* print.anova print.aov* print.aovlist* print.warnings print.xgettext* print.xngettext* print.xtabs*
Writing Functions I
139 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Yes: there are really 170 print methods No: the R team does not expect or want you to know all of them. Users just run print (x) Try not to worry about how the printing is achieved. Yes: R team wants package writers to create specialized print methods to control presentation of their output for the object they create.
Writing Functions I
140 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
notices that x is of a certain type, say classOfx and then the runtime system uses print.classOfX to handle the users request
Writing Functions I
141 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
142 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Example: the plot generic function from R source (graphics package). p l o t < f u n c t i o n ( x , y , ... ) UseMethod ( p l o t )
Writing Functions I
143 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
The generic is just a place holder. User runs print(x), then R knows it is supposed to ask x for its class and then the appropriate thing is supposed to happen. No Big Deal. But the statisticians in the S & R projects saw enormous simplifying potential in developing a battery is standard generic accessor functions
summary() anova() predict() plot() aic()
Writing Functions I
144 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Object
Object: self-contained thing A container for data. . Operationally, in R: just about anything on the left hand side in an assignment <- Each thing in R carries with it enough information so that generic functions know what to do. . If there is no function specic to an object, the work is sent to a default method (see print.default).
Writing Functions I
145 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Class
Denition: As far as R with S3 is concerned, class is a characteristic label assigned to an object (an object can have a vector of classes, such as c( lm, glm)). The class information is used by R do decide which method should be used to fulll the request. Run class(x), ask x what class it inherits from. In R, the principal importance of the class of an object is that it is used to decide which function should be used to carry out the required work when a generic function is used. Classes called numeric integer character are all vector classes , ,
Writing Functions I
146 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Class ...
> y < c ( 1 , 1 0 , 2 3 ) > class (y) [ 1 ] n u m e r i c > x < c ( a , b , c ) > x [ 1 ] a b c > class (x) [ 1 ] c h a r a c t e r > x < f a c t o r ( x ) > class (x) [ 1 ] f a c t o r > m1 < lm ( y x ) > c l a s s (m1) [ 1 ] lm > m2 < glm ( y x , f a m i l y=Gamma) > c l a s s (m2) [ 1 ] glm lm
Writing Functions I 147 / 205 University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Class ...
Writing Functions I
148 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
effe c ts q r call
r a n k df.residual t e r m s
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Honestly, Im baed, I thought everything in R is an object. (And I still do.) If the object does not have a class attribute, it has an implicit class, matrix array or the result of mode(x) (except , that integer vectors have the implicit class integer (from ). ?class in R-2.15.1)
Writing Functions I
151 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
That would say xs class includes lm and glm as new classes, and also would keep xs old classes as well. The class is an attribute, can be set thusly attr (x , ')
``
class
' '
``
whateverISay
'
When a generic method run is called with x, the R runtime will rst try to use run.lm. If run.lm is not found, then run.whateverISay will be tried, and if that fails, it falls back to run.default.
Writing Functions I
152 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
153 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
154 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
L e v e l s : hamburger S l o t age : [ 1 ] 21 jane and william are instances of class pjfriend The variables inside an S4 object are called slots in R slot would be called instance variable in most OO-languages) values in slots can be retrieved with symbol @, not $
Writing Functions I
156 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Implement an S4 method
Step 1. Write a function that can receive a function of type pjfriend and do something with it. Step 2. Use setMethod to tell the R system that the function implements the method that is called for. setMethod wraps a function. set M e t ho d ( somegenericfuncitonname , pjfriend , function (x) { #do something with x } )
Writing Functions I
157 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
158 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
for loop: process by i or by element apply: process rows and/or columns in a matrix lapply: process each element in a list sapply: attempts to simplify output from lapply replicate: shorthand for sapply for simple simulations
Writing Functions I
159 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
for looping
loop over elements in a sequence x1 < v e c t o r ( ) f o r ( i i n 1 : 5 7 ) { x1 [ i ] < doubleMe ( i ) } create an empty vector integers i from 1 to 57 are sent to double me Note, it is not necessary to actually do this for loop in R, because R is vectorized. x2 < doubleMe ( 1 : 5 7 ) a l l . e q u a l ( x1 , x2 ) [ 1 ] TRUE Using vectorized code is much faster.
Writing Functions I
160 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
apply()
useRs are urged to avoid for loops when possible Why? Accessing particular values with [ (vector or matrix indexes) is SLOW. Better to exploit Rs vectorization apply() is one of a family of functions that can replace a for loop. apply() takes a matrix, and does the same FUN to all of its rows or columns (or both). Denition: MARGIN=1 means work row by row MARGIN=2 , means column by column
Writing Functions I
161 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
If there is no built in function that does what you want, then you have to write your own.
Writing Functions I 162 / 205 University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
[1 [2 [3 [4 [5 [6
,] ,] ,] ,] ,] ,]
Writing Functions I
164 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
apply normed Entropy to each Row with apply a p p l y ( xmat , MARGIN=1, FUN=no rmed Ent ro py ) [ 1 ] 0 .8295351 0 .9921503 0 .9156704 0 .7759110 0 .8888583 0 .9003158
Writing Functions I
165 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
lapply() will take a list of things and apply a given function to each item, returning a new list. Generally, aNewList <- lapply( someList, FUN = someFunction ) someFunction MUST accept the elements from someList as the rst argument Additional arguments to someFunction are allowed
Writing Functions I
166 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
167 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Sometimes our data is not an even set of columns that ts in a data.frame or matrix x l i s t < l i s t ( x1=c ( 1 , 1 , 1 , 2 , 3 , 3 ) , x2=r p o i s ( 1 0 , lambda =3) , x3= r o u n d ( rnorm ( 2 0 ,m=100 , s =1) , 0 ) ) e l i s t < l a p p l y ( x l i s t , f u n c t i o n ( x ) { y < t a b l e ( x ) / l e n g t h ( x ) ; no rm edE nt rop y ( y ) } )
Writing Functions I
169 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
172 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
The question:
Create 100 regression models from 100 data sets Study the sampling distribution of the R 2 statistic from those regressions.
Writing Functions I
173 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Step 1.
The following generates 100 data frames in a list mydatasets . e x s < 10 exq < 0 . 3 4 5 e x s t d e < 20 createOneDF < f u n c t i o n ( run , s=NA, q=NA, s t d e=NA ){ x < 18 + 43 * r u n i f ( 1 0 0 0 ) y < s + q * x + rnorm ( 1 0 0 0 , mean=0, s d=s t d e ) mydf < d a t a . f r a m e ( run , x , y ) } m y d a t a s e t s < l a p p l y ( 1 : 1 0 0 , createOneDF , e x s , exq , e x s t d e ) Here the list is just a sequence 1,2,3,... lapply automatically gives each list element to function as rst argument. (In this case, run number).
Writing Functions I 174 / 205 University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Step 2.
Now apply a function to each data frame, make list myregressions m y r e g r e s s i o n s < l a p p l y ( m y d a t a s e t s , FUN = f u n c t i o n ( mydf ) lm ( yx , d a t a=mydf ) ) Note: small functions can be written inline Could as well have written c a l c R e g < f u n c t i o n ( a d f=NULL) { mod < lm ( yx , d a t a=a d f ) } m y r e g r e s s i o n s < l a p p l y ( m y d a t a s e t s , FUN = calcReg )
Writing Functions I
175 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
1 2 3 4 5 6
Writing Functions I
176 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
x 0 .3371
Writing Functions I
177 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Median 0 .873
3Q 12 . 4 6 2
Max 57 . 8 5 4
**
x
*
0 .33713
0 .04737
7 . 1 1 7 2 .10e12
**
0 .001
' ** '
0 .01
'* '
0 .05
'
Writing Functions I
178 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
179 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Step 3.
Grab the R 2 from each regression in the list. The estimate of the R 2 is an element in the returned object from summary. One strategy: create an R list of summary objects mysummaries < l a p p l y ( m y r e g r e s s i o n s , FUN= summary ) Getting the R 2 out of each one of those requires some tedious grabbing, such as myrsq < l a p p l y ( mysummaries , FUN = f u n c t i o n ( mr ) {mr$ r . s q u a r e } ) myrsq [ 1 : 5 ]
Writing Functions I
180 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Step 3. ...
[[1]] [ 1 ] 0 .03758218 [[2]] [ 1 ] 0 .03746384 [[3]] [ 1 ] 0 .02569663 [[4]] [ 1 ] 0 .03390325 [[5]] [ 1 ] 0 .04059477 myrsq < u n l i s t ( myrsq ) s t r ( myrsq )
Writing Functions I 181 / 205 University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Step 3. ...
num [ 1 : 1 0 0 ] 0 . 0 3 7 6 0 . 0 3 7 5 0 . 0 2 5 7 0 . 0 3 3 9 0 . 0 4 0 6 ...
Writing Functions I
182 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
0 0.00
10
Density 20
30
0.02
0.08
0.10
Writing Functions I
184 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Last year I wondered (while auditing the categorical class), what if we run a logistic regression comparing men and women and there are not very many men? Write functions to
manufacture data analyze data summarize & plot data
Writing Functions I
185 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
eta is input, the proclivity to vote democratic s i m L o g i t < f u n c t i o n ( myeta ) { mypi < exp ( myeta ) /(1+ exp ( myeta ) ) ## SAME AS 1/ (1+ exp( -myeta )) m y u n i f < r u n i f ( l e n g t h ( myeta ) ) y < i f e l s e ( m y u n i f < mypi , 1 , 0 ) }
Writing Functions I
186 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
N < 1000 A < 1 B < 0 . 3 x < 1 + 10 * rnorm (N) myeta < A + B * x y < s i m L o g i t ( myeta )
Writing Functions I
187 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
0.6
0.8
Prob(yi = 1) = 0.4
1 1 + exp( i)
y 0.0 0.2
q
qq q
q q qq q q q q qqqq q qqq qqqqqqqq qqq qqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqq q qq qqqq q qq q q q q qq qq q q q q q q qqqqqqqq qq qq qqqqqqqqqqqqq qqq qqqqqqqqqq qq qq qqq q q q q q q qq q q qqqq q q q qqq qqqqqqqqqqq qqqqqq qqqq qqqqqqqq q q q qq qq q q qqq qqqqqqqq qqqqqqqqqq qq qqqqq qq q q q q q q q qq qqqqq q q q qq q q q qq q q q q q q
30
20
10
0 x
10
20
30
Writing Functions I
188 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
0.6
0.8
Prob(yi = 1) = 0.4
1 1 + exp( i)
y 0.0 0.2
q
qq q
q q qq q q q q qqqq q qqq qqqqqqqq qqq qqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqq q qq qqqq q qq q q q q qq qq q q q q q q qqqqqqqq qq qq qqqqqqqqqqqqq qqq qqqqqqqqqq qq qq qqq q q q q q q qq q q qqqq q q q qqq qqqqqqqqqqq qqqqqq qqqq qqqqqqqq q q q qq qq q q qqq qqqqqqqq qqqqqqqqqq qq qqqqq qq q q q q q q q qq qqqqq q q q qq q q q qq q q q q q q
30
20
10
0 x
10
20
30
Writing Functions I
189 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
0.6
0.8
Prob(yi = 1) = 0.4
1 1 + exp( i)
y 0.0 0.2
q
qq q
q q qq q q q q qqqq q qqq qqqqqqqq qqq qqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqq q qq qqqq q qq q q q q qq qq q q q q q q qqqqqqqq qq qq qqqqqqqqqqqqq qqq qqqqqqqqqq qq qq qqq q q q q q q qq q q qqqq q q q qqq qqqqqqqqqqq qqqqqq qqqq qqqqqqqq q q q qq qq q q qqq qqqqqqqq qqqqqqqqqq qq qqqqq qq q q q q q q q qq qqqqq q q q qq q q q qq q q q q q q
30
20
10
0 x
10
20
30
Writing Functions I
190 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
p < l i s t ( ) p$A < 1 ; p$B < 0 . 3 ; p$C < 0 . 4 p$PrFem < 0 . 5 r e s u l t 4 5 < l i s t ( s a p p l y ( 1 : 1 0 0 0 , s i m U n b a l a n c e d , parm=p ) , parm=p ) Note: Im combining the sapply result, along with p for record-keeping , p$PrFem < 0 . 9 r e s u l t 4 9 < l i s t ( s a p p l y ( 1 : 1 0 0 0 , s i m U n b a l a n c e d , parm=p ) , parm=p )
Writing Functions I
192 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
193 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
q q
0.8
q q q
q q
q q
0.2
0.0
0.2
1.0
Writing Functions I
0.5
1.0
1.5
2.0
2.5
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
0.8
0.2
q q q
0.0
0.5
1.5
0.0 0.625
0.2
q q q
0.4
0.6
0.8
1.0
1.2
1.4
Writing Functions I
196 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Many R functions require users to write little functions that do little things. In many cases (like lapply or apply), look for FUN as an argument. Sometimes no builtin-exists. useR must create!
Writing Functions I
197 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
l i b r a r y ( boot ) ? boot
Bootstrap Resampling Description : G e n e r a t e 'R ' b o o t s t r a p r e p l i c a t e s o f a s t a t i s t i c a p p l i e d t o d a t a . Both p a r a m e t r i c and n o n p a r a m e t r i c r e s a m p l i n g a r e p o s s i b l e . . . . b o o t ( da ta , s t a t i s t i c , R , s i m= ' ' o r d i n a r y ' ' , s t y p e= ' ' i ' ' , s t r a t a=r e p ( 1 , n ) , L=NULL , m=0 , w e i g h t s=NULL , r a n . g e n=f u n c t i o n ( d , p ) d , mle=NULL , s i m p l e=FALSE , . . . ) s t a t i s t i c : A f u n c t i o n w h i c h when a p p l i e d t o d a t a r e t u r n s a v e c t o r containing the s t a t i s t i c ( s ) of i n t e r e s t . . .
Writing Functions I
198 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Bootstrap: draw samples repeatedly and re-estimate Resulting values approximate a sampling distribution The boot package asks for a data frame and a special function statistic. statistic must
accept a data frame as the rst argument accept an index vector as the second argument
Writing Functions I
199 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Example usage b o o t ( data , s t a t i s t i c =y o u r F u n c t i o n , R=1000) boot will iterate 1000 times, and yourFunction will provide the statistic of interest. You write yourFunction to make required calculation. boot will tell yourFunction which lines to use in the data frame, over-and-over.
Writing Functions I
200 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Suppose you have a sample from a Poisson Process: samp < r p o i s ( 2 0 , lambda =3) And you calculate the median: median ( samp ) [1] 4 How condent are you in that estimate of the median?
Writing Functions I
201 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Here is yourFunction: calcMed < f u n c t i o n ( dat , i n d ) { median ( d a t [ i n d ] ) } dat[ind] has the eect of pulling rows that match ind from dat The boot function will send 1000 case indexes to your function. l i b r a r y ( boot ) b p o i s < b o o t ( samp , calcMed , R=1000) bpois
Writing Functions I
202 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
Writing Functions I
203 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
4.0
3.5
Density 4
t* 3.0
2.5
Writing Functions I
204 / 205
University of Kansas
Introduction Use Functions! Example: Calculate Entropy Parameters and Returns Cut and Paste: Wrong! R Style Object Oriented Programming Repetit
More ecient to keep 1 data frame, but 1000s of vectors of row numbers.
Writing Functions I
205 / 205
University of Kansas