Actuarial Outpost
 
Go Back   Actuarial Outpost > Actuarial Discussion Forum > Software & Technology
FlashChat Actuarial Discussion Preliminary Exams CAS/SOA Exams Cyberchat Around the World Suggestions


Upload your resume securely at https://www.dwsimpson.com
to be contacted when new jobs meet your skills and objectives.


Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-10-2019, 04:34 AM
bigalxyz bigalxyz is offline
 
Join Date: Aug 2013
Location: London
Posts: 20
Default Graphing GLMs in R

Hi - I'm using R to fit GLMs to client data, which is working quite well (using the RevoAnalytics package). I'd like to put some code together to produce some of the usual graphical output that a proprietary package like Emblem can spit out. Things like:
* Plots of linear predictor/predicted value vs variable level (1 variable at a time)
* Residual plots

Rather than reinventing the wheel, I'm wondering if anyone reading this has any pre-written code to do this kind of thing - something they might be prepared to share?

(cheeky request)

Thanks!
Reply With Quote
  #2  
Old 05-10-2019, 11:14 AM
DjPim's Avatar
DjPim DjPim is online now
Member
SOA
 
Join Date: Nov 2015
Location: SoCal
Posts: 432
Default

Quote:
Originally Posted by bigalxyz View Post
Hi - I'm using R to fit GLMs to client data, which is working quite well (using the RevoAnalytics package). I'd like to put some code together to produce some of the usual graphical output that a proprietary package like Emblem can spit out. Things like:
* Plots of linear predictor/predicted value vs variable level (1 variable at a time)
* Residual plots

Rather than reinventing the wheel, I'm wondering if anyone reading this has any pre-written code to do this kind of thing - something they might be prepared to share?

(cheeky request)

Thanks!
Does RevoAnalytics package produce the same type of object as glm()? Not familiar with it.

Most common graphing package that I know of is ggplot2. I didn't quite understand your example of [* Plots of linear predictor/predicted value vs variable level (1 variable at a time)] -- are you saying you have a factor with different levels, and you want to see the distribution by each level? Like side-by-side boxplots? Or are you just looking to plot one predictor variable against target variable in a scatter / fitted plot? Might want to look into partial dependence plots for that.

Boxplots:
ggplot(data = yourdf, mapping = aes(x = factorvariable, y = responsevariable)) + geom_boxplot()

should get something basic--can tune or clean up, add labels, etc. as desired. Note that it's as easy as changing geom_boxplot() to geom_point() or geom_histogram() or whatever you want to plot, and you can change your x and y variables accordingly. There's a lot of customization and freedom, so I won't pretend to try to guess exactly what you want, but maybe you can clarify and we can get closer
__________________
Quote:
Originally Posted by Dr T Non-Fan View Post
"Cali" SMH.
Reply With Quote
  #3  
Old 05-10-2019, 12:17 PM
bigalxyz bigalxyz is offline
 
Join Date: Aug 2013
Location: London
Posts: 20
Default

Thank you.

The rxGlm() function within the RevoAnalytics package produces its own peculiar model objects which aren't the same as the objects produced by glm(). However there is a useful as.glm() function within the RevoAnalytics package which performs a conversion to the standard glm object.

On your question about the graphs...I've uploaded an example (I think) of the sort of graph I'm thinking of. So it's for a single predictor variable, showing the linear predictor from the GLM fit on the vertical axis and the levels of the predictor variable on the horizontal axis and also 2 standard errors either side (ie rough 95% CI type illustration). I had to construct this manually in Excel though, by copying and pasting from R and then creating the graph. Very tiresome!

Also shows exposure on a secondary axis.

[Also I've included a smoothed GLM fit in this graph (the green data series) although that's not essential. This is the result of a second, simplified GLM fit.]
Attached Images
 
Reply With Quote
  #4  
Old 05-10-2019, 12:23 PM
ALivelySedative's Avatar
ALivelySedative ALivelySedative is offline
Member
CAS
 
Join Date: Dec 2013
Location: Land of the Pine
College: UNC-Chapel Hill Alum
Favorite beer: Red Oak
Posts: 3,160
Default

Is a lift chart not what you're looking for?
__________________
Stuff | 6 | ACAS | FCAS stuff
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT -4. The time now is 04:55 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
*PLEASE NOTE: Posts are not checked for accuracy, and do not
represent the views of the Actuarial Outpost or its sponsors.
Page generated in 0.11407 seconds with 12 queries