Docsity

R Functions - Computing in Statistics - Notes | 22S 166, Study notes of Statistics

Toggle sidebar
bg1
1
22S:166
Computing in Statistics
More on R
Lecture 6
September 11, 2006
Kate Cowles
374 SH, 335-0727
2
Lists in R
•lists are R objects that contain other named
R objects
•often used to return results of functions when
different parts of the results are of different
types and lengths
3
Example: Using the glm function for
generalized linear models
> help(glm)
glm package:stats R Documentation
Fitting Generalized Linear Models
Description:
’glm’ is used to fit generalized linear models, specified by
giving a symbolic description of the linear predictor and a
description of the error distribution.
Usage:
glm(formula, family = gaussian, data, weights, subset,
na.action, start = NULL, etastart, mustart,
offset, control = glm.control(...), model = TRUE,
method = "glm.fit", x = FALSE, y = TRUE, contrasts = NULL, ...)
4
Arguments:
formula: a symbolic description of the model to be fit. The details of
model specification are given below.
family: a description of the error distribution and link function to
be used in the model. This can be a character string naming a
family function, a family function or the result of a call to
a family function. (See ’family’ for details of family
functions.)
data: an optional data frame, list or environment (or object
coercible by ’as.data.frame’ to a data frame) containing the
variables in the model. If not found in ’data’, the
variables are taken from ’environment(formula)’, typically
the environment from which ’glm’ is called.
weights: an optional vector of weights to be used in the fitting
process. Should be ’NULL’ or a numeric vector.
method: the method to be used in fitting the model. The default
method ’"glm.fit"’ uses iteratively reweighted least squares
(IWLS). The only current alternative is ’"model.frame"’
which returns the model frame and does no fitting.
intercept: logical. Should an intercept be included in the _null_
model?
...: further arguments passed to or from other methods.
pf3
pf4

Partial preview of the text

Download R Functions - Computing in Statistics - Notes | 22S 166 and more Study notes Statistics in PDF only on Docsity!

22S:

Computing in Statistics

More on R

Lecture 6

September 11, 2006

Kate Cowles

374 SH, 335-

[email protected]

Lists in R

• lists are R objects that contain other named

R objects

• often used to return results of functions when

different parts of the results are of different

types and lengths

3

Example: Using the glm function for

generalized linear models

help(glm)

glm package:stats R Documentation

Fitting Generalized Linear Models

Description:

’glm’ is used to fit generalized linear models, specified by giving a symbolic description of the linear predictor and a description of the error distribution.

Usage:

glm(formula, family = gaussian, data, weights, subset, na.action, start = NULL, etastart, mustart, offset, control = glm.control(...), model = TRUE, method = "glm.fit", x = FALSE, y = TRUE, contrasts = NULL, ...)

4

Arguments:

formula: a symbolic description of the model to be fit. The details of model specification are given below.

family: a description of the error distribution and link function to be used in the model. This can be a character string naming a family function, a family function or the result of a call to a family function. (See ’family’ for details of family functions.)

data: an optional data frame, list or environment (or object coercible by ’as.data.frame’ to a data frame) containing the variables in the model. If not found in ’data’, the variables are taken from ’environment(formula)’, typically the environment from which ’glm’ is called.

weights: an optional vector of weights to be used in the fitting process. Should be ’NULL’ or a numeric vector.

method: the method to be used in fitting the model. The default method ’"glm.fit"’ uses iteratively reweighted least squares (IWLS). The only current alternative is ’"model.frame"’ which returns the model frame and does no fitting.

intercept: logical. Should an intercept be included in the null model?

...: further arguments passed to or from other methods.

Details:

A typical predictor has the form ’response ~ terms’ where ’response’ is the (numeric) response vector and ’terms’ is a series of terms which specifies a linear predictor for ’response’. For ’binomial’ and ’quasibinomial’ families the response can also be specified as a ’factor’ (when the first level denotes failure and all others success) or as a two-column matrix with the columns giving the numbers of successes and failures. A terms specification of the form ’first + second’ indicates all the terms in ’first’ together with all the terms in ’second’ with duplicates removed. The terms in the formula will be re-ordered so that main effects come first, followed by the interactions, all second-order, all third-order and so on: to avoid this pass a ’terms’ object as the formula.

A specification of the form ’first:second’ indicates the the set of terms obtained by taking the interactions of all terms in ’first’ with all terms in ’second’. The specification ’first*second’ indicates the cross of ’first’ and ’second’. This is the same as ’first + second + first:second’.

Value:

’glm’ returns an object of class inheriting from ’"glm"’ which inherits from the class ’"lm"’. See later in this section.

The function ’summary’ (i.e., ’summary.glm’) can be used to obtain or print a summary of the results and the function ’anova’ (i.e., ’anova.glm’) to produce an analysis of variance table.

The generic accessor functions ’coefficients’, ’effects’, ’fitted.values’ and ’residuals’ can be used to extract various useful features of the value returned by ’glm’.

’weights’ extracts a vector of weights, one for each case in the fit (after subsetting and ’na.action’).

An object of class ’"glm"’ is a list containing at least the following components:

coefficients: a named vector of coefficients

residuals: the working residuals, that is the residuals in the final iteration of the IWLS fit. Since cases with zero weights are

omitted, their working residuals are ’NA’.

fitted.values: the fitted mean values, obtained by transforming the linear predictors by the inverse of the link function.

rank: the numeric rank of the fitted linear model.

family: the ’family’ object used.

linear.predictors: the linear fit on link scale.

7

deviance: up to a constant, minus twice the maximized log-likelihood. Where sensible, the constant is chosen so that a saturated model has deviance zero.

aic: Akaike’s An Information Criterion, minus twice the maximized log-likelihood plus twice the number of coefficients (so assuming that the dispersion is known).

null.deviance: The deviance for the null model, comparable with ’deviance’. The null model will include the offset, and an intercept if there is one in the model

iter: the number of iterations of IWLS used.

weights: the working weights, that is the weights in the final iteration of the IWLS fit.

prior.weights: the case weights initially supplied.

df.residual: the residual degrees of freedom.

df.null: the residual degrees of freedom for the null model.

y: the ’y’ vector used. (It is a vector even for a binomial model.)

converged: logical. Was the IWLS algorithm judged to have converged?

boundary: logical. Is the fitted value on the boundary of the attainable values?

call: the matched call.

8

formula: the formula supplied.

terms: the ’terms’ object used.

data: the ’data argument’.

offset: the offset vector used.

control: the value of the ’control’ argument used.

method: the name of the fitter function used, currently always ’"glm.fit"’.

contrasts: (where relevant) the contrasts used.

xlevels: (where relevant) a record of the levels of the factors used in fitting.

In addition, non-empty fits will have components ’qr’, ’R’ and ’effects’ relating to the final weighted linear fit.

example

quadratic <- function( a,b,c ){}

quadratic <- edit(quadratic)