3d programming library) interactive and ... If you show up in r-help asking about 3D plotting, many folks there ...... N
Overview
persp
scatter3d
Scatterplot3d
rockchalk
3 D Plotting Paul E. Johnson1 1 2
2
Department of Political Science
Center for Research Methods and Data Analysis, University of Kansas
2013
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Outline 1
Overview
2
persp
3
scatter3d
4
Scatterplot3d
5
rockchalk mcGraph plotPlane
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Outline 1
Overview
2
persp
3
scatter3d
4
Scatterplot3d
5
rockchalk mcGraph plotPlane
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Kinds of 3d Plot
Static “draw on the screen, like R plot” persp: in the R base graphics cloud in lattice package scatterplot3d
scatter3d: by John Fox for the car package, uses OpenGL (computer 3d programming library) interactive and easy to get started can be accessed from Fox’s Rcmdr package interface final output not as likely to be “publishable”
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Here’s what we usually want the 3d Plot For
Show the “cloud” of points scattered in space Show the “predicted plane” of a fitted regression model persp can do these things, although it is somewhat tough to grasp at first Why keep trying: persp is in the base of R, so if something is wrong with it, it is likely somebody will know how to fix it. If you show up in r-help asking about 3D plotting, many folks there will suggest you learn persp, since most other routines draw upon its concepts.
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Outline 1
Overview
2
persp
3
scatter3d
4
Scatterplot3d
5
rockchalk mcGraph plotPlane
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
persp is the Place to Start The key thing to understand: if your variables are x1, x2 (the inputs), and z (the output), persp does not “want” your variables like this p e r s p ( x1 , x2 , z ) persp requires “plotting sequences” for x1 and for x2. These are not observed values, but rather sequences from the minimum score to the maximum. For “real data,” x1, for example, get the range, then make a sequence: x 1 r ← r a n g e ( x1 ) x1seq ← seq ( x1r [ 1 ] , x1r [ 2 ] , l e n g t h . o u t = 30) ## or use the rockchalk short-cut x 1 s e q ← p l o t S e q ( x1 , l e n g t h . o u t = 3 0 )
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
For z, persp wants a matrix z has a value for each combination of x1seq and x2seq Various ways to create, but the “outer” function is often convenient.
x1seq
x11 x12 x13
x2seq x21 x22 z11 z12 z21 z22 z31 z32
x23 z13 z23 z33
z11 = f (x11 , x21 ), and so forth
z ← o u t e r ( x1 seq , x2 seq , FUN) FUN is a function that returns a value for each combination of values in the 2 sequences
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Why Does R Call it ”outer?” Remember, an “inner product” in e f a b c d g h An “outer product” is e f g a h
Descriptive
linear algebra = ae + bf + cg + dh =??
b
c
ea fa d = ga ha
eb fb gb hb
ec fc gc hc
ed fd gd hd
(1)
(2)
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Create Some Data for a Regression x1 ← rnorm ( 1 0 0 ) ; x2 ← −4 + r p o i s ( 1 0 0 , lambda =4) ; y = 0 . 1 * x1 + 0 . 2 * x2 + rnorm ( 1 0 0 ) ; d a t ← d a t a . f r a m e ( x1 , x2 , y ) ; rm ( x1 , x2 , y ) m1 ← lm ( y ∼ x1 + x2 , d a t a=d a t ) summary (m1) Call : lm ( f o r m u l a = y ∼ x1 + x2 , d a t a = d a t ) Residuals : Min 1Q −2.29298 −0.55317
Median 0 .02582
3Q 0 .56164
Max 2 .73118
Coefficients : E s t i m a t e S t d . E r r o r t v a l u e Pr ( >| t | ) ( I n t e r c e p t ) 0 .05245 0 .09697 0 .541 0 .5898 Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Create Some Data for a Regression ... x1 x2
0 .20560 0 .20855
0 .08538 0 .04473
2 .408 4 .662
0 .0179 1e−05
* **
*
−−− S i g n i f . codes : ' 0 .1 ' ' 1
0
' *** '
0 .001
' ** '
0 .01
'* '
0 .05
'
.
R e s i d u a l s t a n d a r d e r r o r : 0 . 9 4 2 on 97 d e g r e e s o f freedom M u l t i p l e R2 : 0 .2082 , A d j u s t e d R2 : 0 . 1 9 1 9 F − s t a t i s t i c : 12 . 7 6 on 2 and 97 DF , p−value : 1 .207e−05
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Create the predictor sequences
x 1 r ← r a n g e ( d a t $ x1 ) x1seq ← seq ( x1r [ 1 ] , x1r [ 2 ] , l e n g t h = 30) x 2 r ← r a n g e ( d a t $ x2 ) x2seq ← seq ( x2r [ 1 ] , x2r [ 2 ] , l e n g t h = 30)
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Create the z matrix
z ← o u t e r ( x1 seq , x2 seq , f u n c t i o n ( a , b ) p r e d i c t (m1 , newdata = d a t a . f r a m e ( x1 = a , x2 = b ) ) )
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Persp with No Special Settings p e r s p ( x = x1seq , y = x 2seq , z = z )
x2s
eq
z
x1seq
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Many Opportunities for Beautification xlim,ylim,zlim play same role as in ordinary R plots xlab, ylab, zlab same theta and phi control the viewing angle. theta moves the viewing angle left and right phi moves it up and down.
Example, this “raises” one’s viewing angle (a negative value would lower it) p e r s p ( x=x1se q , y=x 2seq , z=z , p h i =40) Example, this “rotates” one’s viewing angle to the left p e r s p ( x=x1se q , y=x 2seq , z=z , t h e t a=−20 )
Descriptive
K.U.
Overview
persp
scatter3d
p e r s p ( x = x1seq , y = x2seq , z = z , z l i m = c (−3, 3 ) , t h e t a = 40)
Scatterplot3d
rockchalk
p e r s p ( x = x1seq , y = x2seq , z = z , z l i m = c (−3, 3 ) , t h e t a = 4 0 , p h i = −20 )
x2
seq
se
q
z
z x1
x2seq
Descriptive
x1seq
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Everything Else We Draw Has to be ”Perspective Adjusted”
This “looks” 3-dimensional, but it is really a flat two dimensional screen Thus, a point to be inserted at (x1 = 0.3, x2 = −2, z = 2) has to be translated into a position in the 2-dimensional screen
To do that, we use A “Viewing Transformation Matrix” that persp creates trans3d, a function that converts a 3 dimensional coordinate into a 2 dimensional coordinate
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Add Points on a perspective plot
r e s ← p e r s p ( x = x1se q , y = x 2seq , z = z , z l i m = c (−3 , 3 ) , t h e t a = 4 0 , p h i = −15 ) m y p o i n t s ← t r a n s 3 d ( d a t $ x1 , d a t $ x2 , d a t $y , pmat = res ) p o i n t s ( m y p o i n t s , pch = 1 , c o l = ”r e d ”) persp generates “res” as a plot by-product res is the perspective transformation matrix (used by trans3d) mypoints is a 2 dimensional value in the “surface of the computer screen” displaying the 3d plot.
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Points overlaid on persp plot via trans3d
● ● ● ● ● ● ● ● ● ● ●● ● ●●● ● ●● ● ● ●● ● ● ●● ● ●● ●●●● ● ●● ●● ● ● ● ● ● ● ● ● ●● ●●● ● ● ● ●● ●● ● ●●● ● ● ●● ● ● ● ● ● ●● ● ●● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●
z
●
x1seq
Descriptive
● ●
x2seq
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Remember the Fitted Regression model? x1 ← rnorm ( 1 0 0 ) ; x2 ← −4 + r p o i s ( 1 0 0 , lambda =4) ; y = 0 . 1 * x1 + 0 . 2 * x2 + rnorm ( 1 0 0 ) ; d a t ← d a t a . f r a m e ( x1 , x2 , y ) ; rm ( x1 , x2 , y ) m1 ← lm ( y ∼ x1 + x2 , d a t a=d a t ) summary (m1) Call : lm ( f o r m u l a = y ∼ x1 + x2 , d a t a = d a t ) Residuals : Min 1Q −2.29298 −0.55317
Median 0 .02582
3Q 0 .56164
Max 2 .73118
Coefficients : E s t i m a t e S t d . E r r o r t v a l u e Pr ( >| t | ) ( I n t e r c e p t ) 0 .05245 0 .09697 0 .541 0 .5898 x1 0 .20560 0 .08538 2 .408 0 .0179 x2 0 .20855 0 .04473 4 .662 1e−05 −−− S i g n i f . c o d e s : 0 ' *** ' 0 . 0 0 1 ' ** ' 0 . 0 1 ' * ' 0 . 0 5
* *** '
.
'
0 .1
'
'
1
R e s i d u a l s t a n d a r d e r r o r : 0 . 9 4 2 on 97 d e g r e e s o f f r e e d o m M u l t i p l e R2 : 0 . 2 0 8 2 , A d j u s t e d R2 : 0 . 1 9 1 9 F − s t a t i s t i c : 12 . 7 6 on 2 and 97 DF , p−value : 1 .207e−05
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Now draw dotted lines from Predicted to Observed Values This took 8-10 tries Calculate predicted (vpred) and observed values (vobs) Use segments to draw r e s ← p e r s p ( x = x1se q , y = x 2seq , z = z , z l i m = c (−3 , 3 ) , t h e t a = 4 0 , p h i = −15 ) m y p o i n t s ← t r a n s 3 d ( d a t $ x1 , d a t $ x2 , d a t $y , pmat = res ) p o i n t s ( m y p o i n t s , pch = 1 , c o l = ”r e d ”) v p r e d ← t r a n s 3 d ( d a t $ x1 , d a t $ x2 , f i t t e d (m1) , pmat = r e s ) v o b s ← t r a n s 3 d ( d a t $ x1 , d a t $ x2 , d a t $y , pmat = res ) s e g m e n t s ( v p r e d $x , v p r e d $y , v o b s $x , v o b s $y , c o l = ” red ” , l t y = 2)
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
This Makes Me Happy
● ● ● ● ● ● ● ● ● ● ●● ● ●●● ● ●● ● ● ●● ● ● ●● ● ●● ●●●● ● ●● ●● ● ● ● ● ● ● ● ● ●● ●●● ● ● ● ●● ●● ● ●●● ● ● ●● ● ● ● ● ● ●● ● ●● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●
z
●
x1seq
Descriptive
● ●
x2seq
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Plotting Response Surfaces
People who fit nonlinear models often want to see the graceful curvature of their result Often nice to have some color for drama
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Plotting Response Surfaces: Surprisingly Easy
x1 ← rnorm ( 1 0 0 ) ; x2 ← r p o i s ( 1 0 0 , lambda =4) l o g i s t ← f u n c t i o n ( x1 , x2 ) { y ← 1 / ( 1 + exp ( ( −1) * (−3 + 0 . 6 * x1 + . 5 * x2 ) ) ) } p a r ( bg = ”w h i t e ”) x 1 r ← r a n g e ( x1 ) ; x 1 s e q ← s e q ( x 1 r [ 1 ] , x 1 r [ 2 ] , l e n g t h = 30) x 2 r ← r a n g e ( x2 ) ; x 2 s e q ← s e q ( x 2 r [ 1 ] , x 2 r [ 2 ] , l e n g t h = 30) z ← o u t e r ( x1 seq , x2 seq , l o g i s t ) p e r s p ( x = x1seq , y = x 2seq , z = z , t h e t a = −30 , z l i m = c ( −0.2 , 1 . 2 ) )
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
A Curved Surface, but No Color (yet)
z x2s eq
Descriptive
eq
x1s
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
n r z ← nrow ( z ) ncz ← n c o l ( z ) # Create a function interpolating colors in the range of specified colors j e t . c o l o r s ← c o l o r R a m p P a l e t t e ( c ( ”b l u e ” , ”g r e e n ”) ) # Generate the desired number of colors from this palette n b c o l ← 100 color ← j e t . c o l o r s ( nbcol ) # Compute the z-value at the facet centres z f a c e t ← z [ −1 , −1 ] + z [ −1 , −ncz ] + z [ −nrz , −1 ] + z [ −nrz , −ncz ] # Recode facet z-values into color indices f a c e t c o l ← cut ( zfacet , nbcol ) p e r s p ( x = x1seq , y = x 2seq , z = z , c o l = c o l o r [ f a c e t c o l ] , t h e t a = −30 , z l i m = c ( −0.2 , 1 . 2 ) )
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
A Curved Colored Surface
z x2s eq
Descriptive
eq
x1s
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Outline 1
Overview
2
persp
3
scatter3d
4
Scatterplot3d
5
rockchalk mcGraph plotPlane
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Now try scatter3d and the OpenGL Library Framework
OpenGL is an “open standards” 3-D software library (most platforms, newer video cards) “rgl” is an R package that uses OpenGL routines scatter3d is John Fox’s R function (in “car”) that uses rgl functions in a very convenient way
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
A Scatterplot with a Regression Surface
s c a t t e r 3 d ( y ∼ x1 + x2 , d a t a=d a t ) r g l . s n a p s h o t ( f i l e n a m e=”s c a t 1 . p n g ” , fmt=”png ”) Note: a “formula interface” scatter3d handles the creation of “plotting sequences” and the perspective/trans3d work is hidden left-button mouse click rotates middle-button mouse click “zooms” the image
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
A Scatterplot with a Regression Surface
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Just the Scatter, No Plane
s c a t t e r 3 d ( y ∼ x1 + x2 , d a t a=dat , s u r f a c e=FALSE ) r g l . s n a p s h o t ( f i l e n a m e=”s c a t 2 . p n g ” , fmt=”png ”)
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Just the Scatter, No Plane
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Ask For an Ordinary and a Smoothed Regression Surface
s c a t t e r 3 d ( y ∼ x1 + x2 , d a t a=dat , f i t =c ( ” l i n e a r ” , ” a d d i t i v e ”) ) r g l . s n a p s h o t ( f i l e n a m e = ”s c a t 3 . p n g ” , fmt = ”png ”)
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Ask For an Ordinary and a Smoothed Regression Surface
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Evaluation
scatter3d makes it very easy to get started The GUI in Rcmdr makes it even easier! Great for quick & dirty data exploration Disadvantages Output quality not suitable for presentation (labels not “sharp”) png only workable output format at current time (others generate HUGE files)
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Outline 1
Overview
2
persp
3
scatter3d
4
Scatterplot3d
5
rockchalk mcGraph plotPlane
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Confessions
I have tested, but not mastered, these 3d plotting frameworks cloud (in lattice) scatterplot3d (package same name)
These try to hide the “trans3d” problem from the user as much as possible IF you enjoy the plot interface in R, then consider scatterplot3d lattice and the xyplot interface, then you should consider trying to master “cloud”
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
scatterplot3d Works Quite a Bit Like Plot
library ( scatterplot3d ) x ← rnorm ( 8 0 ) ; y ← r p o i s ( 8 0 , l =7) ; z ← 3 + 1 . 1 * x + 0 . 4 * y + 15 * rnorm ( 8 0 ) s3d ← s c a t t e r p l o t 3 d ( x , y , z )
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
scatterplot3d: Quite a Bit like plot
● ●
20
●●
●
0
●
● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ●● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●
−20
z
●
●●
●
20
y
40
●
15
●
10
−40
5 0 −3
−2
−1
0
1
2
3
x
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
scatterplot3d: Quite a Bit like plot
Note: Not necessary to construct a z matrix (scatterplot3d handles that) Many options same name as plot: xlab, ylab, type, etc. angle: viewpoint specifier quite unlike other 3d packages
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Use More Arguments: labels, plot character
library ( scatterplot3d ) s3d ← s c a t t e r p l o t 3 d ( x , y , z , t y p e = ”p ” , c o l o r = ”b l u e ” , a n g l e = 4 5 , pch = 1 8 , main = ” ” , x l a b = ”n o r m a l x ” , y l a b = ”p o i s s o n y ” , z l a b = ” l i n e a r z ”)
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
poisson y
0
20 15
−20
linear z
20
40
scatterplot3d: Quite a Bit like plot
10
−40
5 0 −3
−2
−1
0
1
2
3
normal x
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
scatterplot3d: Also Accepts a ”Data Frame” for x
library ( scatterplot3d ) s3d ← s c a t t e r p l o t 3 d ( dat , t y p e = ”p ” , c o l o r = ” b l u e ” , a n g l e = 5 5 , pch = 1 6 , main = ” ” , x l a b = ”x1 ” , y l a b = ”x2 ” , z l a b = ”y ”)
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
scatterplot3d
●
● ●
● ●
●
● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●●●● ●●●●● ●● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ●
●
●
−3
−2
−1
0
1
2
3
−4
−2
0
2
4
6
x2
−3 −2 −1 0 1 2 3 4
y
●●
x1
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Add a plane from a fitted model! library ( scatterplot3d ) s3d ← s c a t t e r p l o t 3 d ( dat , t y p e = ”p ” , c o l o r = ” b l u e ” , a n g l e = 5 5 , pch = 1 6 , main = ” s c a t t e r p l o t 3 d ”) s3d $ p l a n e 3 d (m1) Note s3d is the 3d plot object, it is told to draw plane corresponding to model m1 That “internalizes” the “translate to 3d coordinates” works that persp required us to do explicitly supplies function “xyz.convert” when explicit translation from 3d to 2d is required (in placing text or lines)
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
scatterplot3d scatterplot3d
●
● ●
● ●
●
● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●●●● ●●●●● ●● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ●
●
●
−3
−2
−1
0
1
2
3
−4
−2
0
2
4
6
x2
−3 −2 −1 0 1 2 3 4
y
●●
x1
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Add Residual Lines: Quite a Bit Like Using persp
s3d ← s c a t t e r p l o t 3 d ( dat , t y p e = ”p ” , c o l o r = ” b l u e ” , a n g l e = 5 5 , pch = 1 6 , main = ” s c a t t e r p l o t 3 d ”) s3d $ p l a n e 3 d (m1 , l t y = ”d o t t e d ” , lwd = 0 . 7 ) o b s e r 2 d ← s 3 d $ x y z . c o n v e r t ( d a t $ x1 , d a t $ x2 , d a t $ y ) p r e d 2 d ← s 3 d $ x y z . c o n v e r t ( d a t $ x1 , d a t $ x2 , f i t t e d ( m1) ) s e g m e n t s ( o b s e r 2 d $x , o b s e r 2 d $y , p r e d 2 d $x , p r e d 2 d $y , l t y = 4)
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
scatterplot3d scatterplot3d
●
● ●
● ●
●
● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●●●● ●●●●● ●● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ●
●
●
−3
−2
−1
0
1
2
3
−4
−2
0
2
4
6
x2
−3 −2 −1 0 1 2 3 4
y
●●
x1
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
scatterplot3d: Syntax Closer ”Object Oriented” Ideal” scatterplot3d creates an output object a t t r i b u t e s ( s3d ) $ names [ 1 ] ” x y z . c o n v e r t ” ”p o i n t s 3 d ” box3d ”
”p l a n e 3 d ”
”
Which can then be told to add points, a plane, etc: s3d $ p l a n e 3 d ( mod1 ) s3d $ p o i n t s 3 d ( x , y , z , pch =18 , c o l=”p i n k ”) Also works well with plotmath functions
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Consider the Bivariate Normal Example from s3d Vignette Bivariate normal distribution
f(x) =
1
(2π)n det(ΣX)
exp− 12 (x − µ)TΣ−1 X (x − µ)
5 0.04
f(x1, x2)
0.06
10
x2
0.08
0 3 2 with µ = , ΣX = 0 2 3
0.02
0
0.00
−5
−10 −10
−5
0
5
10
x1
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
The first step is the empty box
x1 ← x2 ← s e q ( −10 , 1 0 , l e n g t h = 5 1 ) d e n s ← m a t r i x ( dmvnorm ( e x p a n d . g r i d ( x1 , x2 ) , s i g m a = r b i n d ( c ( 3 , 2 ) , c ( 2 , 3 ) ) ) , n c o l = l e n g t h ( x1 ) ) s3d ← s c a t t e r p l o t 3 d ( x1 , x2 , s e q ( min ( d e n s ) , max ( d e n s ) , l e n g t h = l e n g t h ( x1 ) ) , t y p e = ”n ” , g r i d = FALSE , a n g l e = 7 0 , z l a b = e x p r e s s i o n ( f ( x [ 1 ] , x [ 2 ] ) ) , xlab = expression (x [ 1 ] ) , ylab = e x p r e s s i o n ( x [ 2 ] ) , main = ” B i v a r i a t e n o r m a l d i s t r i b u t i o n ”) Note: type=”n”, just like 2D plot function
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
5 0.04
f(x1, x2)
0.06
10
x2
0.08
Bivariate normal distribution
0.02
0
0.00
−5
−10 −10
−5
0
5
10
x1
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Draw the lines from One End to the Other f o r ( i i n l e n g t h ( x1 ) : 1 ) { s3 d $ p o i n t s 3 d ( r e p ( x1 [ i ] , l e n g t h ( x2 ) ) , x2 , d e n s [ i , ] , t y p e = ” l ”) } in English: for each value of x1, draw a line from “front to back” that traces out the density at (x1,x2). The for loop goes to each value of x1 f o r ( i i n l e n g t h ( x1 ) : 1 ) { . . . inserts points from lowest x2 to highest x2 and connects them by a line s3d $ p o i n t s 3 d ( . . . t y p e=l )
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Lines in One Direction
5 0.04
f(x1, x2)
0.06
10
x2
0.08
Bivariate normal distribution
0.02
0
0.00
−5
−10 −10
−5
0
5
10
x1
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Draw Lines the Other Way
for each x2, draw a line from lowest to highest x1 line traces out density at (x1,x2) f o r ( i i n l e n g t h ( x2 ) : 1 ) { s3 d $ p o i n t s 3 d ( x1 , r e p ( x2 [ i ] , l e n g t h ( x1 ) ) , d e n s [ , i ] , t y p e = ” l ”) }
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Draw Lines the Other Way
5 0.04
f(x1, x2)
0.06
10
x2
0.08
Bivariate normal distribution
0.02
0
0.00
−5
−10 −10
−5
0
5
10
x1
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Use R’s text function with plotmath to Write Equation
t e x t ( s 3 d $ x y z . c o n v e r t (−1, 1 0 , 0 . 0 7 ) , l a b e l s = e x p r e s s i o n ( f ( x ) == f r a c ( 1 , s q r t ( ( 2 * p i ) ∧ n * phantom ( ” . ”) * d e t ( Sigma [ X ] ) ) ) * phantom ( ” . ”) * exp * { b g r o u p ( ”( ” , − s c r i p t s t y l e ( f r a c ( 1 , 2 ) * phantom ( ” . ”) ) * ( x − mu) ∧T * Sigma [ X ] ∧−1 * ( x − mu) , ”) ”) } ) ) # ## fix. insert {} around Sigma [ X ] == ... ## t e x t ( s 3 d $ x y z . c o n v e r t ( 1 . 5 , 1 0 , 0 . 0 5 ) , l a b e l s = e x p r e s s i o n ( ”w i t h ” * phantom ( ”m”) * mu == b g r o u p ( ”( ” , a t o p ( 0 , 0 ) , ”) ”) * phantom ( ” . ” ) * ” , ” * phantom ( 0 ) * { Sigma [ X ] == b g r o u p ( ”( ” , a t o p ( 3 * phantom ( 0 ) * 2 , 2 * phantom ( 0 ) * 3 ) , ”) ”) } ) )
The first one is the probability density function (PDF) The second one is the Expected Value and Variance matrix
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Use Plotmath Bivariate normal distribution
f(x) =
1
(2π)n det(ΣX)
exp− 12 (x − µ)TΣ−1 X (x − µ)
5 0.04
f(x1, x2)
0.06
10
x2
0.08
0 3 2 with µ = , ΣX = 0 2 3
0.02
0
0.00
−5
−10 −10
−5
0
5
10
x1
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
About cloud
Like other procedures in the lattice package (tremendous result for small effort) More difficult to customize plots (my humble opinion) Convenient presentation of plots “by group” or “by sex” or such.
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Outline 1
Overview
2
persp
3
scatter3d
4
Scatterplot3d
5
rockchalk mcGraph plotPlane
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
3D Tools in rockchalk
The lack of adjust-ability of scatterplot3d caused me to not rely on it too heavily I don’t want to interactively point-and-click the way rgl requires. I could not make lattice output combine different components in the way I wanted to. But I could make persp work, sometimes. So I kept track of thinks I could succeed with and boiled them down to functions in rockchalk.
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Depicting Multicollinearity: My first 3d functions
mcGraph1(x1, x2, y): Creates an “empty box” showing the footprint of the (x1,x2) pairs in the bottom of the display. mcGraph2(x1, x2, y, rescaley=0.5): Shows points “rising above” footprint mcGraph3(x1, x2, y): fits a regression of y on x1 and x2, and plots it. Includes optional interaction term.
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
mcGraph1 No values drawn yet for dependent variable y
Please notice dispersion in the x1-x2 plane
●
x2
●● ● ● ●● ● ●● ● ●●● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ●● ●●●● ● ●●●● ● ●●● ● ● ●●● ●● ●● ● ● ●●● ● ●
x1
mod1 ← mcGraph1 ( d a t $ x1 , d a t $ x2 , d a t $y , t h e t a=−30 , p h i =8)
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
mcGraph uses rescaley argument The true relationship is y
yi = .2 x1i +.2 x2i +ei , ei ∼ N(0, 72 ) ρx1,x2 = 0.1
x2
●● ● ● ●● ●●●●●●● ● ● ●●● ● ●● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ●●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ●●● ●●● ●● ● ● ● ● ● ●● ● ●●●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ●● ● ●● ● ● ● ● ● ● ●
x1
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 0 . 1 , t h e t a = −30 ) Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Step up rescaley bit by bit, its almost a movie! The true relationship is y
yi = .2 x1i +.2 x2i +ei , ei ∼ N(0, 72 )
x2
●● ● ●●● ●● ●● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●●● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●●●● ● ● ● ● ● ●●●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ●●● ● ●●●● ● ● ● ●● ●● ●●●● ● ● ● ● ● ●● ●● ● ● ●●● ● ● ●
x1
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 0 . 2 , t h e t a = −30 ) Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Step up rescaley bit by bit, its almost a movie! The true relationship is y
yi = .2 x1i +.2 x2i +ei , ei ∼ N(0, 72 )
x2
●●● ● ● ●● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ●●● ● ●● ● ● ● ● ●● ● ● ●● ● ● ●● ● ● ● ●●● ● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ● ●●● ● ● ● ● ●● ● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●●● ●● ● ● ● ●● ●●●● ● ● ●●● ●● ● ● ●● ●●●● ●●● ● ●● ● ●● ●● ● ● ● ● ● ●
x1
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 0 . 3 , t h e t a = −30 ) Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Step up rescaley bit by bit, its almost a movie! The true relationship is yi = .2 x1i +.2 x2i +ei , ei ∼ N(0, 72 ) y x2
Descriptive
●●● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●●● ● ●● ● ●● ● ● ●● ● ● ● ●● ● ● ●● ●● ●● ● ● ●● ●●● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ●●● ● ● ●● ●● ●● ● ● ● ● ●●● ● ● ● ● ● ●●●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ● ● ●● ●● ●● ● ●● ● ●●●● ● ● ●● ●●●● ● ● ●● ●● ● ●● ●● ● ● ● ● ●
x1
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 0 . 4 , t h e t a = −30 )
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Step up rescaley bit by bit, its almost a movie! The true relationship is y
yi = .2 x1i +.2 x2i +ei , ei ∼ N(0, 72 )
x2
●●● ● ● ●●● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ●● ● ● ●● ● ●●●● ● ●● ● ● ● ●● ● ● ● ●●● ● ● ●●● ● ● ●●● ● ● ● ● ● ●● ●● ● ●● ● ● ●● ●● ● ● ●●●●● ● ●●● ● ●● ● ●● ● ● ● ● ● ● ● ●● ● ● ●● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ●● ●●● ●● ● ● ●●● ● ● ● ●● ●●●● ● ● ●● ●● ● ●● ●● ● ● ● ● ●
x1
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 0 . 5 , t h e t a = −30 ) Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Step up rescaley bit by bit, its almost a movie! The true relationship is y
yi = .2 x1i +.2 x2i +ei , ei ∼ N(0, 72 )
x2
●●● ●● ● ●● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ●● ● ●● ● ●● ● ●● ●●●● ● ● ● ● ● ● ● ●●● ● ●●● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ● ● ●● ●●●● ● ● ●●● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ●● ●●● ●● ● ● ●●● ● ● ● ●● ●●●● ● ● ●● ●● ● ●● ●● ● ● ● ● ●
x1
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 0 . 6 , t h e t a = −30 ) Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Step up rescaley bit by bit, its almost a movie! The true relationship is yi = .2 x1i +.2 x2i +ei , ei ∼ N(0, 72 ) y x2
Descriptive
●● ● ●● ● ● ●●● ● ● ●● ● ●● ● ●●●● ● ● ● ● ●● ● ● ● ●● ●● ● ●● ● ● ●●●● ●● ● ● ● ● ● ●●● ● ● ●● ●● ● ● ●● ● ●● ● ●●●● ● ● ●● ● ● ● ●●●●● ●● ● ●●● ● ●● ●●● ● ●● ● ● ●●● ● ● ● ● ●● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ●●● ●● ● ●● ● ●●●● ● ● ●● ●●●● ● ● ●● ●● ● ●● ●● ● ● ● ● ●
x1
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 0 . 7 , t h e t a = −30 )
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Step up rescaley bit by bit, its almost a movie! The true relationship is yi = .2 x1i +.2 x2i +ei , ei ∼ N(0, 72 ) y x2
Descriptive
● ●● ●● ● ● ● ●●● ● ●●● ● ●● ●●●● ● ●●● ● ●● ● ●● ●●● ● ● ●● ● ●● ● ●●● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ●● ●●●●● ● ● ●● ● ● ●● ●● ● ● ● ● ● ● ●● ●● ● ● ●● ● ●● ● ●●● ●●● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ●● ●●● ●● ● ●● ● ●●●● ● ● ●● ●●●● ● ● ●● ●● ● ●● ●● ● ● ● ● ●
x1
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 0 . 8 0 , t h e t a = −30 )
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Step up rescaley bit by bit, its almost a movie! The true relationship is yi = .2 x1i +.2 x2i +ei , ei ∼ N(0, 72 ) y x2
Descriptive
● ●● ● ● ● ● ● ●● ●●● ● ● ● ●● ●● ● ●●● ●● ●● ● ●● ● ● ●● ● ● ● ●● ●●● ●● ● ● ● ● ●● ●● ● ● ● ● ● ● ●● ●● ● ● ● ●● ●●●●● ● ● ●● ● ●● ● ● ● ●●● ● ● ● ● ● ●● ● ● ● ●● ● ●● ● ● ● ● ●●● ● ● ● ● ●● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ●●● ●● ● ●● ● ●●●● ● ● ●● ●●●● ● ● ●● ●● ● ●● ●● ● ● ● ● ●
x1
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 0 . 9 0 , t h e t a = −30 )
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Step up rescaley bit by bit, its almost a movie! The true relationship is y
yi = .2 x1i +.2 x2i +ei , ei ∼ N(0, 72 )
x2
● ● ● ● ● ●● ●●● ●● ● ● ● ●● ●● ● ● ● ● ● ●● ●● ●● ● ● ● ● ● ● ● ● ● ● ●●● ● ●● ● ● ● ●● ●● ● ● ● ● ● ● ● ●● ● ●● ● ● ●● ● ●●●●● ● ● ●● ● ● ● ●●●● ● ● ● ●● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ●● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ●● ●●● ●● ● ● ●●● ● ● ● ●● ●●●● ● ● ●● ●● ● ●● ●● ● ● ● ● ●
x1
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 1 . 0 , t h e t a = −30 ) Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
y
● ● ● ● ● ● ● ●● ● ●● ● ● ●● ● ● ●●●● ●● ● ●●●● ● ● ● ● ● ●● ●● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ●●● ● ● ●● ●● ●●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ●● ●● ●● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ●● ●● ● ● ● ● ● ● ● ●● ●●● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ●●●● ● ● ●● ●● ●● ● ● ● ● ●●● ●● ● ● ● ● ● ●● ●
x2
Can Spin the Cloud (Just Showing Off)
x1
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 1 .0 , t h e t a = 20) Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Can Spin the Cloud (Just Showing Off)
x1
x2
y
● ● ●● ●● ● ● ●● ● ● ●● ●● ●●● ● ●● ● ● ● ●●● ● ●● ● ●● ●● ● ●● ●● ● ● ●● ● ● ● ●● ●●●● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ●● ●● ● ●● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ● ● ● ● ● ●●●● ● ●● ●● ● ● ●● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ●● ●● ● ●● ●● ●● ● ●● ● ●●● ● ●●● ● ● ●● ● ●● ● ●●● ●● ● ●● ●●● ●● ● ● ●● ● ● ●●
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 1 .0 , t h e t a = 40) Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Can Spin the Cloud (Just Showing Off)
y x1
●● ● ● ●● ● ●● ●● ●● ● ● ●●●● ● ● ● ● ●● ●● ●● ●● ●● ● ●● ● ●● ● ●● ●● ●●● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ●●● ● ● ● ● ●● ●● ●●●● ●● ● ●●●● ● ● ● ● ● ● ●●● ● ● ● ● ● ● ● ●● ●●● ●●●● ●● ● ●● ●● ●●● ● ●●● ● ● ● ● ●● ●● ● ● ●● ● ● ● ●● ● ● ● ●● ● ●●●● ● ●● ● ●●● ●● ●● ● ●● ●● ●● ● ●●
x2
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 1 .0 , t h e t a = 60) Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Can Spin the Cloud (Just Showing Off)
x1
y
● ● ●● ● ● ● ● ●●● ● ●●● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ●●● ●● ● ● ● ●● ●●● ● ●●● ●● ●● ● ●● ● ●● ● ●● ● ●● ● ●●● ● ● ● ● ● ● ● ●● ● ●● ● ●● ● ● ●● ● ●●●● ● ● ● ● ● ● ●● ● ● ● ●● ● ●● ● ●● ● ●●● ● ● ● ●● ●● ● ●● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ●●● ● ● ●● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ●●
x2
mod ← mcGraph2 ( d a t $ x1 , d a t $ x2 , d a t $y , r e s c a l e y = 1 .0 , t h e t a = 80) Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Regression Plane Sits Nicely in the Data Cloud M1 Estimate (S.E.) -0.678 0.18* 0.229* 100 6.717 0.194 0.178
(4.345) (0.066) (0.069)
y
(Intercept) x1 x2 N RMSE R2 adj R 2 ∗p ≤ 0.05
x2
Descriptive
● ● ● ● ● ●● ●●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ●● ●● ● ● ● ● ●● ● ●●● ● ●● ● ●● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●●●●● ● ● ●● ● ● ● ●●●● ● ● ● ●● ● ● ● ● ●● ● ●● ●● ●●● ●● ● ● ● ● ●● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ●●● ●● ● ●● ● ●●●● ● ● ●● ●●●● ● ● ● ●● ●● ● ●● ● ●● ● ● ●
x1
mod1 ← mcGraph3 ( d a t $ x1 , d a t $ x2 , d a t $y , t h e t a = −30 )
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Regression Plane Sits Nicely in the Data Cloud M1 Estimate (S.E.) -0.678 0.18* 0.229* 100 6.717 0.194 0.178
(4.345) (0.066) (0.069)
● ● ● ● ● ●● ●● ● ● ●● ● ● ● ● ●●● ● ● ● ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ●●● ● ● ●● ● ●● ●● ●● ● ● ●●●● ●● ●● ●● ● ● ● ● ●● ● ● ●● ● ●● ● ●●● ●● ● ● ● ● ● ● ●●●
y
(Intercept) x1 x2 N RMSE R2 adj R 2
x2
∗p ≤ 0.05
●● ● ●●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ●● ● ● ● ● ●●● ● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ●●● ● ● ● ● ●● ● ● ● ● ● ●
x1
Descriptive
mod1 ← mcGraph3 ( d a t $ x1 , d a t $ x2 , d a t $y , t h e t a = −10 , p h i =0)
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Regression Plane Sits Nicely in the Data Cloud M1 Estimate (S.E.) -0.678 0.18* 0.229* 100 6.717 0.194 0.178
(4.345) (0.066) (0.069) y
(Intercept) x1 x2 N RMSE R2 adj R 2 ∗p ≤ 0.05
x2
Descriptive
● ● ● ●● ● ●● ● ● ● ● ●●● ●● ● ●●● ● ●● ● ● ● ● ● ● ● ● ●●● ● ●● ● ● ●● ●● ●● ● ●● ● ●● ● ● ● ● ● ● ●●● ● ● ● ● ●● ● ●● ● ● ●● ● ● ● ●● ● ●● ● ● ●● ●
●●● ● ● ● ●● ●● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ● ● ●●
x1
mod1 ← mcGraph3 ( d a t $ x1 , d a t $ x2 , d a t $y , t h e t a = −10 , p h i=−10 )
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Severe Collinearity: r(x1,x2)=0.9 Nearly linear dispersion in the x1-x2 plane
x2
y ●● ● ● ● ● ●● ● ●●●● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ●● ●●●● ●●●● ● ● ● ●
x1
Descriptive
mod2 ← mcGraph1 ( d a t 2 $ x1 , d a t 2 $ x2 , d a t 2 $y , t h e t a =20 , p h i =8)
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Cloud Is More like Data Tube mod ← mcGraph2 ( d a t 2 $ x1 , d a t 2 $ x2 , d a t 2 $y , t h e t a = −30 ) y x2
● ● ● ●●● ● ● ●● ● ●● ● ●● ● ●● ● ● ● ● ● ●● ●● ● ● ● ● ●● ● ● ● ● ●● ● ● ●● ● ● ● ●● ●● ●●● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ●● ● ●● ● ● ● ● ●● ●● ● ● ● ●● ●● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ●● ● ● ● ● ● ●●●● ●●● ● ●●●
x1
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Cloud Is More like Data Tube M1 Estimate (S.E.) 2.975 0.365* -0.017 100 7.162 0.165 0.148
(4.128) (0.179) (0.173)
plane does not sit “comfortably”
x2
∗p ≤ 0.05
y
(Intercept) x1 x2 N RMSE R2 adj R 2
● ● ● ●●● ● ● ●● ● ●● ● ●● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ●●● ● ●● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ●● ● ● ● ● ● ● ●● ●● ● ● ●● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ●● ● ● ●● ● ● ● ● ● ● ●●● ● ●●● ● ●●●
x1
greater standard errors
Descriptive
mod ← mcGraph3 ( d a t 2 $ x1 , d a t 2 $ x2 , d a t 2 $y , t h e t a = −30 )
K.U.
Overview
persp
scatter3d
Fit Interaction lm(y ∼
Scatterplot3d
rockchalk
x1 * x2)
M1 Estimate (S.E.) (17.977) (0.394) (0.4) (0.007)
x2
4.997 0.324 -0.058 0.001 100 7.199 0.166 0.14
y
(Intercept) x1 x2 x1:x2 N RMSE R2 adj R 2
● ● ● ●●● ● ● ●● ● ●● ● ●● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ●● ● ●● ● ● ●● ● ● ● ● ●● ● ●●● ● ● ● ● ● ● ●● ● ● ● ● ●● ●● ● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ● ●● ● ●● ● ● ● ●● ● ●● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ●● ●● ●● ● ● ●●● ● ● ●●● ● ●●●
x1
∗p ≤ 0.05
mod ← mcGraph3 ( d a t 2 $ x1 , d a t 2 $ x2 , d a t 2 $y , i n t e r a c t i o n=TRUE, t h e t a = −30 ) Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Next Step: Plot any Fitted Regression
After mcGraph worked, I was encouraged (because I could fill up a whole lecture on multicollinearity) But the mcGraph interface was too limiting had to specify and provide variables could not work with larger regression models
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
plotPlane: quick regression tool for presentations
Generate data, fit a model with 4 predictors, d a t 3 ← g e n C o r r e l a t e d D a t a (N =150 , b e t a = c ( 0 , 0 . 1 5 , 0 .25 , 0 . 1 ) , s t d e = 150) d a t 3 $ x3 ← r p o i s ( 1 5 0 , lambda = 7 ) d a t 3 $ x4 ← rgamma ( 1 5 0 , 2 , 1) m1 ← lm ( y ∼ x1 + x2 + x3 + x4 , d a t a=d a t 3 )
Descriptive
M1 Estimate (S.E.) (Intercept) x1 x2 x3 x4 N RMSE R2 adj R 2
-269.01* 5.308* 5.094* -0.371 10.649 150 152.068 0.198 0.176
(95.835) (1.161) (1.332) (5.05) (9.45)
∗p ≤ 0.05
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
plotPlane: choose x1 and x2
●
●
y
p l o t P l a n e (m1 , p l o t x 1 = ”x1 ” , p l o t x 2 = ”x2 ” , t h e t a = −40 , npp = 1 5 , drawArrows = TRUE)
●
● ● ●● ● ● ●●● ● ● ● ●● ● ● ● ● ● ● ●● ● ●● ● ●● ● ●●● ● ● ● ● ●● ●● ●● ● ● ●●●●●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●●●● ● ● ●● ● ● ●● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ●● ● ● ● ●● ●● ● ●● ● ● ● ●
x2
Descriptive
x1
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
plotPlane: choose x1 and x2
500 ●
0
● ● ●● ● ● ●●● ● ● ● ●● ● ● ● ● ● ● ●● ● ●● ● ●● ● ●●● ● ● ● ● ●● ●● ●● ● ● ●●●●●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●●●● ● ● ●● ● ● ●● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ●● ● ● ● ●● ●● ● ●● ● ● ● ●
80
80
60 x2
60 40 x1
40 20
Descriptive
●
●
y
p l o t P l a n e (m1 , p l o t x 1 = ”x1 ” , p l o t x 2 = ”x2 ” , t h e t a = −40 , npp = 8 , l l w d = 0 .105 , drawArrows = TRUE, t i c k t y p e = ” d e t a i l e d ”)
20
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Interchange Information between 2D and 3D plots Putting x3 and x4 at their means, plot the predicted values for several values of x2 with plotSlopes
600
800
(m−sd) (m) (m+sd)
400
●
●
●
● ●
● ●
● ● ● ● ●
● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ●●● ●● ● ● ●● ● ● ● ● ●● ● ● ●● ●● ● ● ● ●● ● ●● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ●● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●
●
0
200
●
●
●
●
●
●
●
●
●
● ●
●
●● ●● ● ●
● ●
−200
The output object ps30 has information in it that can be used to supplement a 3D graph.
y
ps3 0 ← p l o t S l o p e s (m1 , p l o t x = ”x1 ” , modx = ”x2 ” , modxVals = ” s t d . d e v . ” , l l w d = 3)
Moderator: x2
●
20
30
40
50
60
70
80
x1
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Compare 3D and 2D depictions
Moderator: x2 800 600 400
● ●
●
●
●
0
200
● ●
● ●
●
●
●
●
●
● ●
● ●
● ● ● ● ●
● ● ●
●
●
●
●
●
●
●● ●● ●
●
● ● ●● ● ● ● ● ●● ● ●● ● ● ● ● ●● ●● ● ●● ● ●● ●● ● ● ●● ●● ●●● ●● ●● ● ● ● ● ●●●● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●●●● ● ● ●● ● ●●● ●●● ●● ● ● ●● ● ●● ● ● ●● ● ●● ● ● ●●●● ●●●● ● ● ● ● ●● ● ● ● ●● ● ● ● ●● ● ● ●● ● ●●● ● ● ● ●
●
●
x2
● ● ● ●● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ●●● ●● ● ● ●● ● ● ● ● ●● ● ● ●● ●● ● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●
y
y
●
(m−sd) (m) (m+sd)
●
−200
● ●
x1
20
30
40
50
60
70
80
x1
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Next Step: Visualization of Factor Predictors
Suppose x2 is a categorical variable. Shouldn’t force that to a numeric scale and 3D plot with an ordinary plane, should we? lattice package tools can draw one plot per level of the factor (maybe that’s best) But I’ve wrestled trying to find a more informative view
Descriptive
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Group 1
● ●
● ●
● ● ● ● ● ● ● ●
5
y
● ● ● ● ●
ric
−2 ● −1 ● 0
x1 nume
0
1
2 1.0
Descriptive
1.5
2.5 3.0 2.0 cal ca x2 tegori
3.5
4.0
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Group 1, Group 4
● ●
● ●
● ●●
● ● ● ● ● ● ● ●
5
●
●
●
y
● ●
●
●
●● ● ● ● ●
●
● ● ● ●
●
●●
ric
−2 ● −1 ● 0
x1 nume
0
●
1
2 1.0
Descriptive
1.5
2.5 3.0 2.0 cal ca x2 tegori
3.5
4.0
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Groups 1-4 ● ● ●
● ●
●
● ●
● ● ● ● ● ● ● ●
5
●●
y
ric
● ●
●
●●
●
x1 nume
0
−2 ● −1 ● 0
● ●
●● ● ● ● ●
● ● ● ● ●
●
●
●
●
●
●
● ● ● ● ● ● ●● ● ●
● ● ● ●● ● ● ●● ● ● ● ● ●
● ● ● ●
● ●●
● ●
1
2 1.0
Descriptive
1.5
2.5 3.0 2.0 cal ca x2 tegori
3.5
4.0
K.U.
Overview
persp
scatter3d
Scatterplot3d
rockchalk
Conclusion
If you can “sketch” what you want with a pencil, you can probably get R to draw you a good example. Search (AGGRESSIVELY) for working example code from problems like yours. Accumulate them whenever you find them. If you want a quick view of a regression model–either linear or not linear–I’d suggest plotPlane
Descriptive
K.U.