multivariatePredictGlm: Function that predicts the responses from the covariates for...

Description Usage Arguments Value Examples

View source: R/multivariatePredictGlm.r

Description

Function that predicts the responses from the covariates for a new sample

Usage

1

Arguments

Xnew

a data frame containing the values of the covariates for the new sample.

family

a vector of character specifying the distributions of the responses.

beta

the matrix of coefficients estimated from the calibration sample.

offset

used for the poisson dependent variables. A vector or a matrix of size: number of observations * number of Poisson dependent variables is expected.

Value

a matrix of predicted values.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## Not run: 
library(SCGLR)

# load sample data
data(genus)

# get variable names from dataset
n <- names(genus)
ny <- n[grep("^gen",n)]    # Y <- names that begins with "gen"
nx <- n[-grep("^gen",n)]   # X <- remaining names

# remove "geology" and "surface" from nx as surface is
# offset and we want to use geology as additional covariate
nx <-nx[!nx%in%c("geology","surface")]

# build multivariate formula
# we also add "lat*lon" as computed covariate
form <- multivariateFormula(ny,c(nx,"I(lat*lon)"),c("geology"))

# split genus dataset
sub <- sample(1:nrow(genus),100,replace=FALSE)
sub_fit <- (1:nrow(genus))[-sub]

# define family
fam <- rep("poisson",length(ny))

# fit the model
genus.scglr <- scglr(formula=form, data=genus, family=fam, K=4,
 offset=genus$surface, subset=sub_fit)

# xnew, the design matrix associated to sub-sample used for prediction
# rhs parameters is introduced to take into account that the covariate
# of the formula is composed of two differents sets
xnew <- model.matrix(form, data=genus[sub,], rhs=1:2)[,-1]

# prediction based on the scglr approch
pred.scglr <- multivariatePredictGlm(xnew,family=fam,
 beta=genus.scglr$beta,offset=genus$surface[sub])
cor.scglr <-diag(cor(pred.scglr,genus[sub,ny]))
plot(cor.scglr, col="red",ylim=c(-1,1))

# prediction based on classical poisson glm
X <- model.matrix(form, data=genus)[,-1]
Y <- genus[,ny]
genus.glm <- multivariateGlm.fit(Y[sub_fit,,drop=FALSE],X[sub_fit,,drop=FALSE],
                              family=fam, offset=matrix(genus$surface[sub_fit],
                              length(sub_fit),length(ny)),size=NULL)
coefs <- sapply(genus.glm,coef)

# rhs parameters is introduced to take into account that the covariate
# part of the formula is composed of two differents sets
pred.glm <- multivariatePredictGlm(xnew,family=fam,beta=coefs,
 offset=genus$surface[sub])
cor.glm <- diag(cor(pred.glm,genus[sub,ny]))

points(cor.glm, col="blue")

## End(Not run)

SCGLR documentation built on May 1, 2019, 8 p.m.