# Multiple (smoothed) regression and portfolio exposure

Wednesday, in class, we’ve seen how to visualize a multiple regression model (with two continuous explanatory variables). Here, the goal is to predict the average cost of an insurance claim, using some covariates, e.g. the age of the driver, and the age of the car (recall that losses here are liability losses). The prediction obtained from a (standard) generalized linear model, with a log-link

`> reg1=glm(cout~ageconducteur+agevehicule,data=base,family=Gamma(link="log"))`

The code to visualize the predicted average cost is the following: first, we have to compute predictions for specific values,

```> pred=function(x,y){
+ predict(reg,newdata=data.frame(ageconducteur=x,
+ agevehicule=y),type="response")```

Then, we use this function to compute values on a grid,

```> X=seq(20,80,by=5)
> Y=0:20
> Z=outer(X,Y,p)
> image(X,Y,Z,col=rev(heat.colors(101)))
+ levels=c(1400,1800,2000,2200,2400,2600,2800,3000,3200,4000,5000))```

If we use factors, and not continuous variates (cut versions of those two variates),

```> reg2=glm(cout~cut(ageconducteur,breaks=c(0,22,35,55,80,100))*
+               cut(agevehicule,breaks=c(-1,1,3,5,10,100)),

(note that we consider the Cartesian product, so values are computed for each product of factors, age of the driver and age of the car) we obtain

Obviously, we’re missing something here: the most expensive class with one model is the cheapeast for the other one! Of course, it might come from our classes (that were chosen a bit randomly), but it might be interesting to use nonlinear functions of the ages. So, let us use splines to smooth those two variables,

```> reg3=glm(cout~bs(ageconducteur)+bs(agevehicule),data=base,

With additive smoothed functions, we obtained a symmetric graph (due to the additive property)

while with a bivariate spline

```> library(mgcv)
+ reg4=gam(cout~s(ageconducteur,agevehicule),data=base,