Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using "gamplot" for generalized additive models (GAM)

    Dear Forum,

    I will much appreciate guidance about how to use the "gamplot" function for generalized additive models. I have created 2 generalized additive model:

    1. gam yvar1 xvar, f(gaussian) link(identity) df(n)

    2. gam yvar2 xvar, f(binomial) link(logit) df(n)


    Where y-variable yvar1 has normal distribution, and yvar2 has a binomial distribution. The x-variable is a continuous variable.


    I would like to use the GAM plotter, "gamplot xvar, nopres". What additional code should I use, or how should I change my yvar such that the y-axis plots the precited mean (for yvar1) and, separately, the predicted probability (for yvar2)?

    I am aware that gam stores "GAM_mu", the predicted yvar.

    Thanks in advance.

    Itai
    Last edited by Itai Magodoro; 12 Feb 2022, 14:49.

  • #2
    Originally posted by Itai Magodoro View Post
    I would like to use the GAM plotter . . . or how should I change my yvar such that the y-axis plots the precited mean (for yvar1) and, separately, the predicted probability (for yvar2)?
    I am aware that gam stores "GAM_mu", the predicted yvar.
    I believe that you cannot use -gamplot- to do what you're looking to do. Instead, just use Stata's regular graphing commands to plot the fitted versus the function of predictor, which is also left behind in the dataset by -gam-. Try something like that below. The first is for the Gaussian case and the second is for the binomial.

    .ÿ
    .ÿversionÿ17.0

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿquietlyÿsysuseÿauto

    .ÿ
    .ÿglobalÿGAMDIRÿ`c(pwd)'\

    .ÿ
    .ÿgamÿweightÿdisplacement,ÿfamily(gaussian)ÿlink(identity)ÿdf(3)

    74ÿrecordsÿmerged.

    GeneralizedÿAdditiveÿModelÿwithÿfamilyÿgauss,ÿlinkÿident.

    Modelÿdfÿÿÿÿÿ=ÿÿÿÿÿ4.000ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿNo.ÿofÿobsÿ=ÿÿÿÿÿÿÿÿ74
    Devianceÿÿÿÿÿ=ÿÿÿ6877280ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿDispersionÿ=ÿÿÿ98246.8
    -------------------------------------------------------------------------
    ÿÿÿÿÿÿweightÿ|ÿÿÿdfÿÿÿÿLin.ÿCoef.ÿÿStd.ÿErr.ÿÿÿÿÿÿzÿÿÿÿÿÿÿÿGainÿÿÿÿP>Gain
    -------------+-----------------------------------------------------------
    displacementÿ|ÿÿ3.000ÿÿÿ7.573259ÿÿÿ.3994655ÿÿÿÿ18.958ÿÿÿÿ19.386ÿÿÿÿ0.0001
    ÿÿÿÿÿÿÿ_consÿ|ÿÿÿÿÿÿ1ÿÿÿÿ3019.46ÿÿÿÿ36.4371ÿÿÿÿ82.868ÿÿÿÿÿÿÿÿÿ.ÿÿÿÿÿÿÿÿÿ.
    -------------------------------------------------------------------------
    Totalÿgainÿ(nonlinearityÿchisquare)ÿ=ÿÿÿÿ19.386ÿ(2.000ÿdf),ÿPÿ=ÿ0.0001

    .ÿ
    .ÿ//ÿresponseÿversusÿfunctionÿofÿpredictor
    .ÿgraphÿtwowayÿ///
    >ÿÿÿÿÿÿÿÿÿlineÿGAM_muÿs_displacement,ÿsortÿlcolor(black)ÿ||ÿ///
    >ÿÿÿÿÿÿÿÿÿscatterÿweightÿs_displacement,ÿmcolor(black)ÿmfcolor(white)ÿmsize(small)ÿ///
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿytitle(Weightÿ(lbs.))ÿylabel(ÿ,ÿangle(horizontal)ÿnogrid)ÿlegend(off)

    .ÿquietlyÿgraphÿexportÿGausGAM.png

    .ÿdropÿ*_displacementÿ*_mu

    .ÿ
    .ÿgamÿforeignÿmpg,ÿfamily(binomial)ÿlink(logit)ÿdf(3)

    74ÿrecordsÿmerged.

    GeneralizedÿAdditiveÿModelÿwithÿfamilyÿbinom,ÿlinkÿlogit.

    Modelÿdfÿÿÿÿÿ=ÿÿÿÿÿ3.793ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿNo.ÿofÿobsÿ=ÿÿÿÿÿÿÿÿ74
    Devianceÿÿÿÿÿ=ÿÿÿ77.7278ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿDispersionÿ=ÿÿÿÿÿÿÿÿÿ1
    -------------------------------------------------------------------------
    ÿÿÿÿÿforeignÿ|ÿÿÿdfÿÿÿÿLin.ÿCoef.ÿÿStd.ÿErr.ÿÿÿÿÿÿzÿÿÿÿÿÿÿÿGainÿÿÿÿP>Gain
    -------------+-----------------------------------------------------------
    ÿÿÿÿÿÿÿÿÿmpgÿ|ÿÿ2.793ÿÿÿ.1595858ÿÿÿ.0521688ÿÿÿÿÿ3.059ÿÿÿÿÿ0.849ÿÿÿÿ0.6016
    ÿÿÿÿÿÿÿ_consÿ|ÿÿÿÿÿÿ1ÿÿÿ-.976156ÿÿÿÿ.282028ÿÿÿÿ-3.461ÿÿÿÿÿÿÿÿÿ.ÿÿÿÿÿÿÿÿÿ.
    -------------------------------------------------------------------------
    Totalÿgainÿ(nonlinearityÿchisquare)ÿ=ÿÿÿÿÿ0.849ÿ(1.793ÿdf),ÿPÿ=ÿ0.6017

    .ÿ
    .ÿlowessÿforeignÿs_mpg,ÿlineopts(lcolor(black)ÿlpattern(dash))ÿ///
    >ÿÿÿÿÿÿÿÿÿmcolor(black)ÿmsize(vsmall)ÿjitter(3)ÿjitterseed(787217005)ÿ///
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿaddplot(lineÿGAM_muÿs_mpg,ÿsortÿlcolor(black)ÿlpattern(solid)ÿ///
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿtitle("")ÿytitle(ProbabilityÿofÿForeignÿOrigin)ÿ///
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿylabel(ÿ,ÿangle(horizontal)ÿnogrid)ÿlegend(off))

    .ÿquietlyÿgraphÿexportÿLogitGAM.png

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .


    For the binomial GLM case, you might also want to consider plotting the linear prediction (in the logit transform as estimated).
    Click image for larger version

Name:	GausGAM.png
Views:	1
Size:	34.2 KB
ID:	1649796

    Click image for larger version

Name:	LogitGAM.png
Views:	1
Size:	30.7 KB
ID:	1649797

    Comment


    • #3
      Thank you very much Joseph. Much appreciated.

      Comment

      Working...
      X