Statistical Software
Study guides: Exam MAS-I, Exam PCPA, Exam ACIA-CAPSTONE
Statistical Software is a programming language for data analysis, such as R, Python or SAS, together with its packages, used to import data, fit models and print their output. MAS-I questions show output tables of the kind R prints and ask for them to be read, not for code to be written. The common tasks in R and in Python are:
| Task | R | Python (pandas, statsmodels) |
|---|---|---|
| Import a file | read.csv() |
pd.read_csv() |
| Report each column's type and summary | str(), summary() |
.dtypes, .describe() |
| Declare a variable categorical | factor() |
.astype("category") |
| Fit a linear regression | lm() |
smf.ols() |
| Fit a generalized linear model | glm() with a family |
smf.glm() with a family |
| Print the fitted model's output | summary(), anova() |
.summary() |
Model formulas
R and statsmodels describe a model by a formula with the response to the left of ~ and the predictors to the right. The formula y ~ x1 + x2, fitted with a link g, specifies the linear predictor
g(\mu_i) = \beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2}
\mu_iis the mean of the response for observationi.- The intercept
\beta_0is included unless the formula removes it.
In a formula, x1:x2 is the interaction of two predictors, and x1*x2 means both main effects and their interaction. The distribution and the link come from the family argument, such as family = poisson(link = "log"), not from the formula. In R, log() is the natural logarithm, so a term log(x) enters \ln x.
In R,
glm(claims ~ territory * age + offset(log(exposure)), family = poisson)is fitted withterritorydeclared a factor with levels A, B and C. Which terms does the linear predictor contain?
- The default link of the Poisson family is the log link.
territoryenters as two indicator variables, for levels B and C, because A is the base level.ageenters as one numeric term.territory * ageadds the two interaction terms of B and C withage.- The offset adds
\ln(\text{exposure})with its coefficient fixed at 1.
Defaults in R's model output
R's output depends on several defaults:
- The base level of a categorical predictor is its first level, which is alphabetical unless it is reset with
relevel(). The base level has no row in the coefficient table, because it is absorbed into the intercept. - The intercept is the linear predictor for the base level of every categorical predictor, with every numeric predictor equal to zero.
- A categorical variable stored as numbers is fitted as one numeric slope until it is declared a
factor. Once declared, it is treated as nominal, with a separate coefficient for each non-base level. - Observations with a missing value in any model variable are dropped, and the summary reports how many were deleted. The number of records fitted is then less than the number in the data file.
- The coefficient table labels the test statistic
z valuewhen the dispersion is fixed (Poisson, binomial) andt valuewhen it is estimated (normal, Gamma).
A Poisson GLM with a log link and an exposure offset is fitted in R with territory as its only predictor. The coefficient table shows
(Intercept)-2.3026,territoryB0.2231(standard error0.0600) andterritoryC-0.1054. What are the base frequency and the territory relativities?
- Territory A has no row, so it is the base level.
- The base frequency is
e^{-2.3026} = 0.100claims per unit of exposure.- The relativity of territory B is
e^{0.2231} = 1.250, and that of territory C ise^{-0.1054} = 0.900.- A 95% confidence interval for B's relativity is
e^{0.2231 \pm 1.96 \times 0.0600}, from 1.111 to 1.406.