Skip to content

mgcv : 3-way interaction and 3D-plots ?

4 messages · David Winsemius, Richard M. Heiberger, varin sacha

#
Dear R-experts,

I have fitted a model with 2-way and 3-way interactions. 
I would like, for the 3-way interaction (year,age,by=education), to obtain 3D-plots. How could I do that ?

Many thanks for your response.

Here is the reproducible example:

############# 
install.packages("ISLR")

library(ISLR)

install.packages("mgcv")

library(mgcv)

mod1<-gam(wage ~education+s(age,bs="ps")+year+te(age,year,bs="ps")+s(year,bs="ps",by=education,m=1)+te(year,age,by=education,bs=rep("ps",2)),data=Wage)

plot(mod1)
#############
#
On 1/7/19 3:35 PM, varin sacha via R-help wrote:
Forget ggplot2. It has ignored this sort of visualization effort. Use 
lattice or base plotting methods.


In order to plot a 2way interaction one needs a pseudo-3way plot 
(`wireframe`) or a single `levelplot`. For display of a 3way interaction 
in lattice (given the human minds inability to "see" in 4 dimensions) 
you will need to specify levels for one of the variables to display 
slices perhaps using multiple displays of 2way "sub-interactions" 
calculated ad meaningul levels of the variable you choose to slive 
with.? I'm not sure what the "native" plotting method for pkg:mgcv might 
be. I suspect it was base graphics,; if so, look at ?persp and ?contour.
#
## Here is an example using the 3-way interaction plot from the HH package


install.packages("HH") ## if necessary

## The HH package supports the book
## Statistical Analysis and Data Display
##    Richard M. Heiberger and Burt Holland
## http://www.springer.com/us/book/9781493921218

library(HH)
## find the pathname of the R script file dsgn.R containing this example
HHscriptnames(13)
## open the file dsgn.R in your favorite editor.


## Then run

## chunk 2,
## chunk 3, Figure 13.1
## chunk 4  Table 13.1
## chunk 7  Figure 13.3  three-way interactions

## Rows of the array of panels are                  current
## Columns of the array of panels are               n.treats
## differently colored boxes within each panel are  minutes
##
## In this example the 3-way interaction is not significant.
## For a hint of what one could see, compare the panel "60.cycle x 3"
## with "60.cycle x 6".  In "60.cycle x 3", the red box at minutes=5 is
## higher than the other three boxes.  In "60.cycle x 6", the red box at
## minutes=5 is lower than the other boxes.


## Illustrate a minimalist form of this call.
## Create a dataset with simple variable names Y, A, B, C
mydata <- data.frame(Y=cc176$y.adj,
                     A=unpositioned(cc176$minutes),
                     B=cc176$n.treats,
                     C=cc176$current)

## A is an ordinary factor, minutes is a positioned factor, see
?HH::position
## We use xyplot() here, not bwplot(), because bwplot() doesn't handle
"positioned" factors.

## The minimalist form of this call is
useOuterStrips(
xyplot(Y ~ A | B + C, data=mydata,
       groups=A,
       panel=panel.bwplot.superpose, ## take control of colors of the boxes
       horizontal=FALSE,
       between=list(x=1, y=1))
)


On Mon, Jan 7, 2019 at 7:59 PM David Winsemius <dwinsemius at comcast.net>
wrote:

  
  
4 days later
#
David, Richard,

Many thanks for your responses.




Le mardi 8 janvier 2019 ? 04:25:19 UTC+1, Richard M. Heiberger <rmh at temple.edu> a ?crit : 





## Here is an example using the 3-way interaction plot from the HH package


install.packages("HH") ## if necessary

## The HH package supports the book
## Statistical Analysis and Data Display
##? ? Richard M. Heiberger and Burt Holland
## http://www.springer.com/us/book/9781493921218

library(HH)
## find the pathname of the R script file dsgn.R containing this example
HHscriptnames(13)
## open the file dsgn.R in your favorite editor.


## Then run

## chunk 2,?
## chunk 3, Figure 13.1
## chunk 4? Table 13.1
## chunk 7? Figure 13.3? three-way interactions

## Rows of the array of panels are? ? ? ? ? ? ? ? ? current
## Columns of the array of panels are? ? ? ? ? ? ? ?n.treats
## differently colored boxes within each panel are? minutes
##?
## In this example the 3-way interaction is not significant.
## For a hint of what one could see, compare the panel "60.cycle x 3"
## with "60.cycle x 6".? In "60.cycle x 3", the red box at minutes=5 is
## higher than the other three boxes.? In "60.cycle x 6", the red box at
## minutes=5 is lower than the other boxes.


## Illustrate a minimalist form of this call.
## Create a dataset with simple variable names Y, A, B, C
mydata <- data.frame(Y=cc176$y.adj,
? ? ? ? ? ? ? ? ? ? ?A=unpositioned(cc176$minutes),
? ? ? ? ? ? ? ? ? ? ?B=cc176$n.treats,
? ? ? ? ? ? ? ? ? ? ?C=cc176$current)

## A is an ordinary factor, minutes is a positioned factor, see ?HH::position
## We use xyplot() here, not bwplot(), because bwplot() doesn't handle "positioned" factors.

## The minimalist form of this call is
useOuterStrips(
xyplot(Y ~ A | B + C, data=mydata,
? ? ? ?groups=A,
? ? ? ?panel=panel.bwplot.superpose, ## take control of colors of the boxes
? ? ? ?horizontal=FALSE,
? ? ? ?between=list(x=1, y=1))
)
On Mon, Jan 7, 2019 at 7:59 PM David Winsemius <dwinsemius at comcast.net> wrote: