Sunteți pe pagina 1din 20

CURS 4

REGRESIE NEPARAMETRICA CU O COVARIATA


Observatii indep, id rep (A
i
, 1
i
) , i = 1, ..., :
Date statistice (r
i
, j
i
) , i = 1, ..., :
Plotare:
cause <- c (j
1
, ..., j
n
)
eect <- c (r
1
, ..., r
n
)
model <- data.frame(cause,eect)
plot(model)
Interpolare:
cu segmente de dreapta
cu polinoame de grad 2 sau 3
Netezire:
polinimoala
liniara pe portiuni (broken stick)
polinomiala pe portiuni
Media conditionata = functie cu proprietati de "netezime"
' (A [ 1 = j) = q (j) , cu q : [a, /] 1, q
0
(j) , q
00
(j) continue
Curba neparametrica de regresie
A = q (j) +
Masura a netezirii asperitatilor (respectiv "roughness")
b
_
a
(q
00
(j))
2
dj
Suma abaterilor patratice cu penalizare (cu parametrul de netezire c):
oo (q) =
n

i=1
(r
i
q (j
i
))
2
+ c
b
_
a
(q
00
(j))
2
dj, c 0
1
Comentariu:
Daca c este mic, curba de regresie ca "urmari" datele statistice
Daca c este mare, componenta de penalizare a asperitatilor va mare
Estimatorul prin metoda celor mai mici patrate cu penalizare este
solutia problemei de optimizare
inf
g2C
oo (q)
FUNCTII SPLINE CUBICE
Denitie
Fie a < j
1
< j
2
< ... < j
n
< / si q : [a, /] 1. Functia q se numeste
functie spline cubica (cubic spline) daca
q este polinom de gradul 3 pe oricare dintre subintervalele
(a, j
1
) , (j
1
, j
2
) , ..., (j
n
, /) ;
q este continua, cu q
0
si q
00
continue pe [a, /]
j
i
, i = 1, ..., : se numesc noduri
Conventie: j
0
= a, j
n+1
= /
Utilizand baza
_
j
3
, j
2
, j, 1
_
,o functie spline cubica este de forma
q (j) = d
i
(j j
i
)
3
+c
i
(j j
i
)
2
+/
i
(j j
i
)+a
i
pentru j
i
_ j _ j
i+1
, i = 0, ..., :
cu conditia de continuitate in j
i+1
, i = 0, ..., : 1,
d
i
(j
i+1
j
i
)
3
+ c
i
(j
i+1
j
i
)
2
+ /
i
(j
i+1
j
i
) + a
i
= a
i+1
, i = 0, ..., : 1
Denitie
O functie spline cubica pe [a, /] se numeste natural cubic spline (NCS)
daca derivatele sale de ordin 2 si 3 sunt nule in a si /.
Observatie: Daca q este NCS pe [a, /] , atunci d
0
= c
0
= d
n
= c
n
= 0,
adica q este liniara pe [a, j
1
] si [j
n
, /] .
2
Notatii
Fie q o functie NCS pe [a, /] cu nodurile j
1
< j
2
< ... < j
n
q
i
= q (j
i
) , i = 1, ..., :
q = (q
1
, ..., q
n
)
0

i
= q
00
(j
i
) , i = 1, ..., : (
1
=
n
= 0)
=
_

2
, ...,
n1
_
0
/
i
= j
i+1
j
i
, i = 1, ..., : 1
Q = |
ij
|
i=1;:::;n; j=2;:::;n1
1 = |r
ij
|
i=2;:::;n1; j=2;:::;n1

ij
=
_

_
1
hj1
, i = , 1

1
hj1

1
hj
, i = ,
1
hj
, i = , + 1
0, [i ,[ _ 2
r
ii
=
1
3
(/
i1
+ /
i
) , i = 2, ..., : 1
r
i;i+1
=
1
6
/
i
, i = 2, ..., : 1
r
i+1;i
=
1
6
/
i
, i = 2, ..., : 1
r
ij
= 0, [i ,[ _ 2
Proprietate
Matricea 1 este pozitiv denita si
[r
ii
[

j6=i
[r
ij
[ , i = 2, ..., : 1
In continuare cautam o reprezentare a lui q asa incat functia sa e specicata
prin vectorii q, .
Input: q, , /
i
, i = 1, ..., : 1
Output: q (j) , j [a, /]
3
Construim functia q (j) prin urmatoarele relatii:
q (j) = q
1
(j
1
j)
_
q
2
q
1
/
1

1
6
/
1

2
_
, a _ j _ j
1
q (j) =
1
/
i
[(j j
i
) q
i+1
+ (j
i+1
j) q
i
]

1
6
(j j
i
) (j
i+1
j)
__
1 +
j j
i
/
i
_

i+1
+
_
1 +
j
i+1
j
/
i
_

i
_
,
j
i
_ j _ j
i+1
, i = 1, ..., : 1
q (j) = q
n
+ (j j
n
)
_
q
n
q
n1
/
n1
+
1
6
/
n1

n1
_
, j
n
_ j _ /
Teorema
Date ind nodurile a < j
1
< j
2
< ... < j
n
< / si vectorii q, , functia q
construita mai sus este NCS daca si numai daca
Q
0
q = 1
Demonstratie:
Continuitatea in j
1
: Pentru continuitatea la stanga folosim
q (j) = q
1
(j
1
j)
_
q
2
q
1
/
1

1
6
/
1

2
_
, a _ j _ j
1
iar pentru continuitate la dreapta folosim
q (j) =
1
/
1
[(j j
1
) q
2
+ (j
2
j) q
1
]

1
6
(j j
1
) (j
2
j)
__
1 +
j j
1
/
1
_

2
+
_
1 +
j
2
j
/
1
_

1
_
, j
1
_ j _ j
2
q este continua in j
1:
q
_
j

1
_
= q
_
j
+
1
_
= q
1
q
0
este continua in j
1
q
0
_
j

1
_
=
q
2
q
1
/
1

1
6
/
1

2
q
0
_
j
+
1
_
=
q
2
q
1
/
1

1
6
/
1
(2
1
+
2
) =
q
2
q
1
/
1

1
6
/
1

2
caci
1
= 0
q
00
(j) = q
000
(j) = 0, j _ j
1
(liniaritate)
4
Continuitatea in j
n
: Pentru continuitate la stanga folosim
q (j) =
1
/
n1
[(j j
n1
) q
n
+ (j
n
j) q
n1
]

1
6
(j j
n1
) (j
n
j)
__
1 +
j j
n1
/
n1
_

n
+
_
1 +
j
n
j
/
n1
_

n1
_
,
j
n1
_ j _ j
n
iar pentru continuitate la dreapta folosim
q (j) = q
n
+ (j j
n
)
_
q
n
q
n1
/
n1
+
1
6
/
n1

n1
_
, j
n
_ j _ /
q este continua in j
n
q
_
j

n
_
= q
_
j
+
n
_
= q
n
q
0
este continua in j
n
q
0
_
j
+
n
_
=
q
n
q
n1
/
n1
+
1
6
/
n1

n1
q
0
_
j

n
_
=
q
n
q
n1
/
n1
+
1
6
/
n1
_

n1
+ 2
n
_
=
q
n
q
n1
/
n1
+
1
6
/
n1

n1
caci
n
= 0
q
00
(j) = q
000
(j) = 0, j _ j
n
(liniaritate)
Continuitatea in j
i
i = 2, ..., : 1 : . Pentru continuitate la dreapta
([j
i
, j
i+1
]) vom folosi
q (j) =
1
/
i
[(j j
i
) q
i+1
+ (j
i+1
j) q
i
]

1
6
(j j
i
) (j
i+1
j)
__
1 +
j j
i
/
i
_

i+1
+
_
1 +
j
i+1
j
/
i
_

i
_
,
j
i
_ j _ j
i+1
, i = 1, ..., : 1
iar pentru continuitate la stanga ([j
i1
, j
i
]) vom folosi
q (j) =
1
/
i1
[(j j
i1
) q
i
+ (j
i
j) q
i1
]

1
6
(j j
i1
) (j
i
j)
__
1 +
j j
i1
/
i1
_

i
+
_
1 +
j
i
j
/
i1
_

i1
_
,
j
i1
_ j _ j
i
, i = 2, ..., : 1
q este continua in j
i
q
_
j

i
_
= q
_
j
+
i
_
= q
i
conditia de continuitate a lui q
0
in j
i
este (dupa calculele explicite)
q
0
_
j

i
_
=
q
i
q
i1
/
i1
+
1
6
/
i1
_

i1
+ 2
i
_
q
0
_
j
+
i
_
=
q
i+1
q
i
/
i
+
1
6
/
i
_
2
i
+
i+1
_
5
q
0
_
j

i
_
= q
0
_
j
+
i
_
i = 2, ..., : 1
==
q
i+1
q
i
/
i

q
i
q
i1
/
i1
=
1
6
/
i1

i1
+
1
3
(/
i1
+ /
i
)
i
+
1
6
/
i

i+1
, i = 2, ..., : 1
q
0
_
j

i
_
= q
0
_
j
+
i
_
i = 2, ..., : 1
==
Q
0
q = 1
q
00
_
j

i
_
= q
00
_
j
+
i
_
=
i
i = 2, ..., : 1
Corolar (calculul componentei de penalizare a asperitatilor)
Daca Q
0
q = 1 , atunci
b
_
a
(q
00
(j))
2
dj =
_

_
0
1 =
_
q
_
0
1q, unde
1 = Q1
1
Q
0
Demonstratie:
Avem q
00
(a) = q
00
(/) = 0, q
000
constant pe orice subinterval (j
j
, j
j+1
) . Inte-
grand prin parti obtinem:
b
_
a
(q
00
(j))
2
dj =
b
_
a
q
000
(j) q
0
(j) dj =
=
n1

j=1
q
000
_
j
+
j
_
yj+1
_
yj
q
0
(j) dj =
=
n1

j=1
1
/
j
_

j+1

j
_
(q
j
q
j+1
)
In plus,
1
=
n
= 0. Rearanjand termenii sumei,
b
_
a
(q
00
(j))
2
dj =
n1

i=2

i
_
q
i+1
q
i
/
i

q
i
q
i1
/
i1
_
=
_

_
0
1
Folosind ipoteza,
_

_
0
1 =
_
q
_
0
Q1
1
Q
0
q =
_
q
_
0
1q
6
INTERPOLARE CU FUNCTII NCS
Datele statistice (r
i
, j
i
) , i = 1, ..., :
Noduri a < j
1
< ... < j
n
< /
Cautam o curba neteda care sa verice q (j
i
) = r
i
, i = 1, ..., :
Propozitie
Presupunem : _ 2, j
1
< j
2
< ... < j
n
. Date ind valorile r
1
, ..., r
n
, exista
o unica functie NCS q, cu nodurile j
1
, ..., j
n
, care satisface q (j
i
) = r
i
, i =
1, ..., :.
Demonstratie:
Conditia q (j
i
) = r
i
, i = 1, ..., : este satisfacuta daca luam q = x =
(r
1
, ..., r
n
) .
Cum 1 e pozitiv denita, rezulta ca (, !) asa incat = 1
1
Q
0
q
Deci e indeplinita C.N.S.
_
Q
0
q = 1
_
din teorema.
Observatii privind calculele:
Cum 1 este tridiagonala, sistemul 1 = x poate rezolvat fara calculul
lui 1
1
, intr-un numar "liniar" de operatii.
Cum Q este tridiagonala, Q
0
q poate calculat printr-un numar "liniar"
de operatii,
_
Q
0
q
_
i
=
q
i+1
q
i
/
i

q
i
q
i1
/
i1
, i = 2, ..., : 1
Algoritm de interpolare
Input: (r
i
, j
i
) , i = 1, ..., :
1. q
i
= r
i
, i = 1, ..., :
2.
_
Q
0
q
_
i
=
gi+1gi
hi

gigi1
hi1
, i = 2, ..., : 1
3. x = Q
0
q
4. Rezolvam sistemul 1 = x obtinand
5. Avand q, si nodurile, construim interpolantul NCS conform formulelor
de la pag.3.
7
Interpolantul NCS minimizeaza componenta de penalizare a asperitatilor (aratam
in continuare):
Notatie:
J
2
= q diferentiabila pe [a, /] , cu derivate de ordinul intai absolut continue
J = q cu primele doua derivate continue J
2
Propozitie
Presupunem : _ 2 si e q un interpolant NCS cu nodurile a < j
1
< ... <
j
n
< / si cu valorile r
1
, ..., r
n
. Fie q J
2
, pentru care q (j
i
) = r
i
, i = 1, ..., :.
Atunci
b
_
a
( q
00
(j))
2
dj _
b
_
a
(q
00
(j))
2
dj,
cu egalitate daca si numai daca q = q.
Demonstratie:
Fie c = q q, c J
2
, c (j
i
) = 0, i = 1, ..., :.
Stim ca q
00
(a) = q
00
(/) = 0 si ca q
000
(j) = 0 pentru j (a, j
1
) ' (j
n
, /) , iar
q
000
este constanta pe oricare dintre intervalele (j
j
, j
j+1
) .
Integrand prin parti, obtinem:
b
_
a
q
00
(j) c
00
(j) dj =
b
_
a
q
000
(j) c
0
(j) dj =
n1

j=1
q
000
_
j
+
j
_
yj+1
_
yj
c
0
(j) dj =
=
n1

j=1
q
000
_
j
+
j
_
(c (j
j+1
) c (j
j
)) = 0
b
_
a
( q
00
(j))
2
dj =
b
_
a
_
q
00
(j) + c
00
(j)
_
2
dj =
=
b
_
a
(q
00
(j))
2
dj + 2
b
_
a
q
00
(j) c
00
(j) dj +
b
_
a
_
c
00
(j)
_
2
dj _
b
_
a
(q
00
(j))
2
dj
Egalitatea are loc daca si numai daca
b
_
a
_
c
00
(j)
_
2
dj = 0, adica daca c este
functie liniara pe [a, /] . Dar c (j
i
) = 0, i = 1, ..., : si : _ 2, deci functia c trebuie
sa e identic nula pe [a, /] .
8
Functii in R
1) splinefun {stats} R Documentation
Interpolating Splines
Description: Perform cubic spline interpolation of given data points, return-
ing either a list of points obtained by the interpolation or a function performing
the interpolation.
Usage
spline(x, y = NULL, n = 3*length(x), method = "fmm", xmin = min(x),
xmax = max(x))
Arguments
x,y vectors giving the coordinates of the points to be interpolated. Al-
ternatively a single plotting structure can be specied: see xy.coords.
method species the type of spline to be used. Possible values are
"fmm", "natural" and "periodic".
n interpolation takes place at n equally spaced points spanning the
interval [xmin, xmax].
xmin left-hand endpoint of the interpolation interval.
xmax right-hand endpoint of the interpolation interval.
Details
If method = "fmm", the spline used is that of Forsythe, Malcolm and Moler
(an exact cubic is tted through the four points at each end of the data, and this
is used to determine the end conditions). Natural splines are used when
method = "natural", and periodic splines when method = "periodic".
Value
spline returns a list containing components x and y which give the ordinates
where interpolation took place and the interpolated values.
splinefun returns a function which will perform cubic spline inter-
polation of the given data points.
2) interpSpline {splines} R Documentation
Create an Interpolation Spline
Description: Create an interpolation spline, either from x and y vectors, or
from a formula/data.frame combination.
Usage
interpSpline(obj1, obj2, bSpline = FALSE, period = NULL, na.action =
na.fail)
Arguments
obj1 Either a numeric vector of x values or a formula.
obj2 If obj1 is numeric this should be a numeric vector of the same
length. If obj1 is a formula this can be an optional data frame in which to
evaluate the names in the formula.
bSpline If TRUE the b-spline representation is returned, otherwise the
piecewise polynomial representation is returned. Defaults to FALSE.
9
period An optional positive numeric value giving a period for a periodic
interpolation spline.
na.action a optional function which indicates what should happen when
the data contain NAs. The default action (na.omit) is to omit any incomplete
observations. The alternative action na.fail causes interpSpline to print an error
message and terminate if there are any incomplete observations.
Value
An object that inherits from class spline. The object can be in the B-spline
representation, in which case it will be of class nbSpline for natural B-spline,
or in the piecewise polynomial representation, in which case it will be of class
npolySpline.
10
NETEZIRE CU FUNCTII NCS
Consideram problema de regresie cu valorile (raspuns,covariata) = (r
i
, j
i
) ,
i = 1, ..., :, cu : _ 3 si
a < j
1
< ... < j
n
< /
Pentru curba neparametrica de regresie
A = q (j) +
suma abaterilor patratice cu penalizare (cu parametrul de netezire c) este
oo (q) =
n

i=1
(r
i
q (j
i
))
2
+ c
b
_
a
(q
00
(j))
2
dj, c 0
Propozitie
Daca q este solutie a problemei de optimizare
inf
g2I2
oo (q) ,
atunci q este functie NCS.
Demonstratie
Sa presupunem ca q, cu nodurile j
1
, ..., j
n
, nu este functie NCS.
Fie q interpolantul NCS cu aceleasi noduri si cu valorile q (j
1
) , ..., q (j
n
) .
Avem
q (j
i
) = q (j
i
) , i = 1, ..., :
n

i=1
(r
i
q (j
i
))
2
=
n

i=1
(r
i
q (j
i
))
2
b
_
a
( q
00
(j))
2
dj <
b
_
a
(q
00
(j))
2
dj
Deci oo ( q) < oo (q) , adica, daca q nu este NCS, exista o functie NCS care
realizeaza o valoare mai mica pentru suma abaterilor patratice cu penalizare.
Contradictie!
Rezulta ca, daca q minimizeaza oo (q) , atunci q trebuie sa e NCS.
Notam
x = (r
1
, ..., r
n
)
0
q = (q
1
, ..., q
n
)
0
11
Presupunem ca q este functie NCS denita de vectorii q, , cu matricele
tridiagonale Q si 1.
n

i=1
(r
i
q (j
i
))
2
=
_
x q
_
0
_
x q
_
b
_
a
(q
00
(j))
2
dj =
_
q
_
0
1q
oo (q) =
_
x q
_
0
_
x q
_
+ c
_
q
_
0
1q =
=
_
q
_
0
(1 + c1) q 2x
0
q +x
0
x
Dar matricea c1 este semipozitiv denita, deci 1 +c1 este pozitiv denita.
_
q (1 + c1)
1
x
_
0
(1 + c1)
_
q (1 + c1)
1
x
_
_ 0
iar egalitatea se obtine pentru
q = (1 + c1)
1
x
Deci inf
g2I2
oo (q) este atins pentru q. Am obtinut urmatorul rezultat:
Propozitie
Presupunem : _ 3, a < j
1
< ... < j
n
< /. Date ind valorile r
1
, ..., r
n

si un parametru de netezire c 0, consideram q functia NCS cu nodurile


j
1
, ..., j
n
, pentru care q = (1 + c1)
1
x. Atunci, pentru orice q J
2
, avem
oo ( q) _ oo (q) , cu egalitate daca q = q.
Observatii privind calculele
Prin constructie, 1 = Q1
1
Q
0
.
Din sistemul
_
1 + cQ1
1
Q
0
_
q = x,
tinand cont de Q
0
q = 1, rezulta
q = x cQ
Deci, pentru un algoritm de calcul, avem nevoie de o formula pentru calculul
.
Q
0
q = 1 == Q
0
xcQ
0
Q = 1 == (1 + cQ
0
Q) = Q
0
x
Matricea (1 + cQ
0
Q) este matrice "in banda 5dimensionala", simetrica si
pozitiv denita.. Rezulta ca admite o descompunere Cholesky de forma
1 + cQ
0
Q = 111
0
12
unde 1 este o matrice diagonala, pozitiv denita si o matrice 1 inferior tri-
unghiulara, in banda, cu
|
ii
= 1, \i
|
ij
= 0 pentru , < i 2 si , 2
Algoritmul Reinsch de netezire cu functii NCS
Input (r
i
, j
i
) , i = 1, .., :, : _ 3
1. Calculam Q
0
x
2. Identicam elementele nenule ale matricii 1+cQ
0
Q si matricile corespun-
zatoare 1 si 1.
3. Rezolvam sistemul 111
0
= Q
0
x si obtinem
4. Calculam q = x cQ
Eroarea netezirii
Curba de regresie
A = q (j) +
Datele
(r
i
, j
i
) , i = 1, ..., :
Functia de netezire
q (j)
Eroarea medie patratica
'o1 ( q (j)) = '
_
( q (j) q (j))
2
_
(medierea in raport cu repart. cond. )
= [' ( q (j)) q (j)]
2
+ 1
2
( q (j))
Eroarea medie patratica integrata
'1o1 ( q) =
_
'o1 ( q (j)) dj
Media sumei abaterilor patratice (calculabila din date)
'oo1 ( q) = '
_
n

i=1
( q (j
i
) q (j
i
))
2
_
13
Functii in R
smooth.spline............ package:stats............... R Documentation
Fit a Smoothing Spline
Description: Fits a cubic smoothing spline to the supplied data.
Usage:
smooth.spline(x, y = NULL, w = NULL, df, spar = NULL, cv = FALSE,
all.knots = FALSE, nknots = NULL, df.oset = 0, penalty = 1, control.spar =
list())
Arguments:
x: a vector giving the values of the predictor variable, or a list or a two-
column matrix specifying x and y.
y: responses. If y is missing, the responses are assumed to be specied by
x.
w: optional vector of weights of the same length as x; defaults to all 1.
df : the desired equivalent number of degrees of freedom (trace of the smoother
matrix).
spar: smoothing parameter, typically (but not necessarily) in (0,1].
cv: ordinary (TRUE) or "generalized" cross-validation (GCV) when FALSE.
all.knots: if TRUE, all distinct points in x are used as knots. IfFALSE
(default), a subset of x[] is used, specically x[j] where the nknots indices
are evenly spaced in 1:n.
nknots: integer giving the number of knots to use when all.knots=FALSE.
Per default, this is less than n, the number of unique x values for n 49.
df.oset: allows the degrees of freedom to be increased by df.osetin the
GCV criterion.
penalty: the coecient of the penalty for degrees of freedom in the GCV
criterion.
control.spar: optional list with named components controlling the root
nding when the smoothing parameter spar is computed, i.e., missing or NULL.
Value:
An object of class "smooth.spline" with components
x: the _distinct_ x values in increasing order,
y: the tted values corresponding to x.
w: the weights used at the unique values of x.
yin: the y values used at the unique y values.
lev: leverages, the diagonal values of the smoother matrix.
cv.crit: cross-validation score, "generalized" or true, depending on cv.
pen.crit: penalized criterion
crit: the criterion value minimized in the underlying .Fortran routine ss-
lvrg.
df : equivalent degrees of freedom used. Note that (currently) this value may
become quite unprecise when the true df is
between and 1 and 2.
spar: the value of spar computed or given.
lambda: the value of lambda corresponding to spar
14
LABORATOR 4
REGRESIE LINIARA GENERALIZATA
APLICATIE 1
1) birthwt {MASS} R Documentation
Risk Factors Associated with Low Infant Birth Weight = MODEL
CU DATE BINARE
Description
The birthwt data frame has 189 rows and 10 columns. The data were col-
lected at Baystate Medical Center, Springeld, Mass during 1986.
Usage: birthwt
Format: This data frame contains the following columns:
low: indicator of birth weight less than 2.5kg (VARAIBILA BI-
NARA)
age: mothers age in years
lwt: mothers weight in pounds at last menstrual period
race: mothers race (1 = white, 2 = black, 3 = other)
smoke: smoking status during pregnancy
ptl: number of previous premature labours
ht: history of hypertension
ui: presence of uterine irritability
ftv: number of physician visits during the rst trimester
bwt: birth weight in grams (SE POATE FACE SI lm PENTRU
ACEASTA VARIABILA)
library(MASS)
attach(birthwt)
bwt <- data.frame(low = factor(low), age, lwt)
glm.low<-glm(low ~., binomial, bwt)
summary(glm.low)
Call:
glm(formula = low ~., family = binomial, data = bwt)
Deviance Residuals:
Min ..........1Q ...........Median ..........3Q ............Max
-1.1352 ...-0.9088 ....-0.7480 .......1.3392 .....2.0595
15
Coecients:
....................Estimate ...................Std. Error ............z value ..........Pr([z[)
(Intercept) ....1.748773 .................0.997097 ............1.754 ...........0.0795 .
age .............-0.039788 .................0.032287 ...........-1.232 ...........0.2178
lwt ..............-0.012775 .................0.006211 ...........-2.057 ...........0.0397 *

Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1


(Dispersion parameter for binomial family taken to be 1)
Null deviance: 234.67 on 188 degrees of freedom
Residual deviance: 227.12 on 186 degrees of freedom
AIC: 233.12
Number of Fisher Scoring iterations: 4
APLICATIE 2
2) quakes {datasets} R Documentation
Locations of Earthquakes o Fiji = MODEL CU DATE POISSON
Description: The data set give the locations of 1000 seismic events of MB
4.0. The events occurred in a cube near Fiji since 1964.
Usage: quakes
Format: A data frame with 1000 observations on 5 variables.
[,1] lat numeric Latitude of event
[,2] long numeric Longitude
[,3] depth numeric Depth (km)
[,4] mag numeric Richter Magnitude
[,5] stations numeric Number of stations reporting
(rezultat al unui proc de numarare)
quakes
.....lat .........long ......depth ........mag ........stations
1 ..-20.42 ..181.62 ...562 .........4.8 .............41
2 .-20.62 ...181.03 ....650 .........4.2 .............15
3 -26.00 ...184.10 ......42 ..........5.4 .............43
4 -17.97 ...181.66 .....626 .........4.1 .............19
5 -20.42 ...181.96 .....649 .........4.0 .............11
.......................................................................
999 -17.40 187.80 ....40 ...........4.5 ............14
1000 -21.59 170.56 ..165 .........6.0 ..........119
model1<-glm(stations ~mag, data = quakes, family = poisson)
model1
16
Call: glm(formula = stations ~mag, family = poisson, data = quakes)
Coecients:
(Intercept).... mag
-1.966 ........1.158
Degrees of Freedom: 999 Total (i.e. Null); 998 Residual
Null Deviance: 12200
Residual Deviance: 3018
AIC: 8198
summary(model1)
Call:
glm(formula = stations ~mag, family = poisson, data = quakes)
Deviance Residuals:
Min ..........1Q ...........Median ...........3Q .............Max
-7.9669 ..-1.2540 ....-0.1196 ........0.9366 ........6.8971
Coecients:
.....................Estimate ...........Std. Error ..........z value .........Pr([z[)
(Intercept) ..-1.96624 ...........0.05584 ...........-35.22 ........<2e-16 ***
mag .............1.15849 ...........0.01147 ...........101.01 .........<2e-16 ***

(Dispersion parameter for poisson family taken to be 1)


Null deviance: 12198 on 999 degrees of freedom
Residual deviance: 3018 on 998 degrees of freedom
AIC: 8198.1
Number of Fisher Scoring iterations: 4
model2<-glm(stations ~depth , data = quakes, family = poisson)
model2
Call: glm(formula = stations ~depth, family = poisson, data = quakes)
Coecients:
(Intercept) ............depth
3.5779052 .........-0.0002247
Degrees of Freedom: 999 Total (i.e. Null); 998 Residual
Null Deviance: 12200
Residual Deviance: 12120
AIC: 17300
summary(model2)
Call:
glm(formula = stations ~depth, family = poisson, data = quakes)
Deviance Residuals:
Min ........1Q ........Median ..........3Q .............Max
-5.057 ..-2.983 ...-1.248 ..........1.364 .........12.754
17
Coecients:
.....................Estimate ..........Std. Error ..........z value ....................Pr([z[)
(Intercept) ...3.578e+00 ......9.435e-03 .........379.2 ................<2e-16 ***
depth .........-2.247e-04 ......2.554e-05 .........-8.8 ..................<2e-16 ***

(Dispersion parameter for poisson family taken to be 1)


Null deviance: 12198 on 999 degrees of freedom
Residual deviance: 12121 on 998 degrees of freedom
AIC: 17301
Number of Fisher Scoring iterations: 5
model<-glm(stations ~depth + mag, data = quakes, family =
poisson)
model
Call: glm(formula = stations ~depth + mag, family = poisson, data =
quakes)
Coecients:
(Intercept) ...............depth ..................mag
-2.2047597 ........0.0003109 ..........1.1888550
Degrees of Freedom: 999 Total (i.e. Null); 997 Residual
Null Deviance: 12200
Residual Deviance: 2871
AIC: 8053
summary(model)
Call:
glm(formula = stations ~depth + mag, family = poisson, data = quakes)
Deviance Residuals:
Min ..........1Q ...................Median ..........3Q ............Max
-7.9023 ..-1.1691 ............-0.1714 .......0.9030 ......6.0113
Coecients:
....................Estimate ............Std. Error ..........z value ...........Pr([z[)
(Intercept) -2.205e+00 ........5.909e-02 .........-37.31 ............<2e-16 ***
depth ........3.110e-04 .........2.552e-05 ..........12.18 .............<2e-16 ***
mag ..........1.189e+00 .........1.171e-02 .........101.55 ............<2e-16 ***

(Dispersion parameter for poisson family taken to be 1)


Null deviance: 12198.5 on 999 degrees of freedom
Residual deviance: 2870.6 on 997 degrees of freedom
AIC: 8052.7
Number of Fisher Scoring iterations: 4
18
COMENTARIU: daca s-ar utiliza un model liniar (cu ipoteza de normalitate
a datelor)
model.lin<-lm(stations ~depth + mag, data = quakes)
model.lin
Call:
lm(formula = stations ~depth + mag, data = quakes)
Coecients:
(Intercept) ..........depth ...........mag
-192.04290 .....0.01318 ......47.90872
summary(model.lin)
Call:
lm(formula = stations ~depth + mag, data = quakes)
Residuals:
Min ............1Q .........Median ............3Q ............Max
-46.5059 ..-6.9964 ..-0.4529 .........6.6428 .....47.2586
Coecients:
.....................Estimate .............Std. Error ............t value ...................Pr([t[)
(Intercept) ..-1.920e+02 ........4.332e+00 ..........-44.335 .............< 2e-16 ***
depth ...........1.318e-02 .........1.685e-03 ............7.822 ................1.32e-14 ***
mag ............4.791e+01 ..........9.016e-01 ..........53.135 ...............< 2e-16 ***

Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1


Residual standard error: 11.17 on 997 degrees of freedom
Multiple R-Squared: 0.7404, Adjusted R-squared: 0.7399
F-statistic: 1422 on 2 and 997 DF, p-value: < 2.2e-16
anova(model.lin)
Analysis of Variance Table
Response: stations
...........Df ..........Sum Sq ...............Mean Sq ............F value............ Pr(F)
depth ....1 ............2590 ...................2590 ................20.759 .........5.853e-06
***
mag ......1 ..........352190 ...............352190 ............2823.334 ..........< 2.2e-16
***
Residuals 997 ......124368 ...............125

plot(stations~depth, data=quakes)
Se obtine un nor de puncte, fara aspect de model liniar
plot(stations~mag, data=quakes)
Se obtine un aspect de model liniar
19
CORELATII
Consideram covariata 1 / covariatele ("cauze") de tip cantitativ.
(a) Daca variabila raspuns A ("efect") este cantitativa, cu repartitie discreta
(de ex Binomiala sau Poisson), atunci se poate calcula coecientul de corelatie
(Pearson) raspuns ~ covariata si acesta poate estimat din datele statistice
j =
co (A, 1 )
_
1
2
(A) 1
2
(1 )
(b) Daca variabila raspuns A ("efect") este categoriala (da / nu), atunci ea
nu are medie, dispersie, etc si atribuirea codicarii "da = 1", "nu = 0" nu are
sens cantitativ. Putem insa gasi o modalitate de a testa independenta pentru
A, 1.
Transformam covariata 1 intr-una categoriala, cu doua variante
(1 < j) , (1 _ j))
, sau cu patru variante
(1 < Q
1
) , (Q
1
_ 1 < 'c) , ('c _ 1 < Q
3
) , (1 _ Q
3
)
Alcatuim un tabel de contingenta pentru A si 1 modicat
X/Y (1 < Q
1
) (Q
1
_ 1 < 'c) ('c _ 1 < Q
3
) (1 _ Q
3
)
da :
11
:
12
:
13
:
14
:
1
nu :
21
:
22
:
23
:
24
:
2
:
1
:
2
:
3
:
4
n
Aplicam un test "CHI patrat" pentru ipoteza H : A si 1 sunt indepen-
dente , bazat pe statistica
o
2
=
2

i=1
4

j=1
(:
ij
:
i
:
j
,:)
2
:
i
:
j
,:
care are o repartitie
2
(2 1) (4 1) =
2
(3)
Regula de decizie, la pragul de semnicatie c:
o
2
calc
_ /
3 ; 1
== rc:ji:qc: H
o
2
calc
< /
3 ; 1
== acccjta: H
Astfel putem stabili ca A si 1 nu sunt independente, dar nu putem da o
masura cantitativa a corelarii lor.
20

S-ar putea să vă placă și