cva.aglm {aglm} | R Documentation |

## Fit an AGLM model with cross-validation for both `\alpha`

and `\lambda`

### Description

A fitting function with cross-validation for both `\alpha`

and `\lambda`

.
See aglm-package for more details on `\alpha`

and `\lambda`

.

### Usage

```
cva.aglm(
x,
y,
alpha = seq(0, 1, len = 11)^3,
nfolds = 10,
foldid = NULL,
parallel.alpha = FALSE,
...
)
```

### Arguments

`x` |
A design matrix. See aglm for more details. |

`y` |
A response variable. |

`alpha` |
A numeric vector representing |

`nfolds` |
An integer value representing the number of folds. |

`foldid` |
An integer vector with the same length as observations.
Each element should take a value from 1 to |

`parallel.alpha` |
(not used yet) |

`...` |
Other arguments are passed directly to |

### Value

An object storing fitted models and information of cross-validation. See CVA_AccurateGLM-class for more details.

### Author(s)

Kenji Kondo,

Kazuhisa Takahashi and Hikari Banno (worked on L-Variable related features)

### References

Suguru Fujita, Toyoto Tanaka, Kenji Kondo and Hirokazu Iwasawa. (2020)
*AGLM: A Hybrid Modeling Method of GLM and Data Science Techniques*,

https://www.institutdesactuaires.com/global/gene/link.php?doc_id=16273&fg=1

*Actuarial Colloquium Paris 2020*

### Examples

```
#################### Cross-validation for alpha and lambda ####################
library(aglm)
library(faraway)
## Read data
xy <- nes96
## Split data into train and test
n <- nrow(xy) # Sample size.
set.seed(2018) # For reproducibility.
test.id <- sample(n, round(n/5)) # ID numbders for test data.
test <- xy[test.id,] # test is the data.frame for testing.
train <- xy[-test.id,] # train is the data.frame for training.
x <- train[, c("popul", "TVnews", "selfLR", "ClinLR", "DoleLR", "PID", "age", "educ", "income")]
y <- train$vote
newx <- test[, c("popul", "TVnews", "selfLR", "ClinLR", "DoleLR", "PID", "age", "educ", "income")]
# NOTE: Codes bellow will take considerable time, so run it when you have time.
## Fit the model
cva_result <- cva.aglm(x, y, family="binomial")
alpha <- cva_result@alpha.min
lambda <- cva_result@lambda.min
mod_idx <- cva_result@alpha.min.index
model <- cva_result@models_list[[mod_idx]]
## Make the confusion matrix
y_true <- test$vote
y_pred <- levels(y_true)[as.integer(predict(model, newx, s=lambda, type="class"))]
cat(sprintf("Confusion matrix for alpha=%.5f and lambda=%.5f:\n", alpha, lambda))
print(table(y_true, y_pred))
```

*aglm*version 0.4.0 Index]