-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLM.R
More file actions
70 lines (45 loc) · 1.09 KB
/
GLM.R
File metadata and controls
70 lines (45 loc) · 1.09 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
###Generalized linear model(GLM)
##applying the Gaussian model
library(car)
data(SLID)
lmfit1=glm(wages~age+sex+education,data = SLID,family=gaussian())
summary(lmfit1)
lmfit2=lm(wages~age+sex+education,data=SLID)
summary(lmfit2)
#compare the two fitted models
library(lmtest)
anova(lmfit1,lmfit2)
##applying the Poisson model
data(warpbreaks)
head(warpbreaks)
rsl=glm(breaks~tension,data=warpbreaks,family="poisson")
summary(rsl)
##applying the Binomial model
data(mtcars)
head(mtcars)
lm1=glm(vs~hp+mpg+gear,data=mtcars,family=binomial)
summary(lm1)
##fitting a generalized additive model
install.packages("mgcv")
library(mgcv)
install.packages("MASS")
library(MASS)
attach(Boston)
str(Boston)
fit=gam(dis~s(nox))
summary(fit)
##visualizing a generalized additive model
#generate a scatter plot
plot(nox,dis)
#add the regression
x=seq(0,1,length=500)
y=predict(fit,data.frame(nox=x))
lines(x,y,col="red",lwd=2)
#plot the fit model
plot(fit)
#a contour plot
fit2=gam(medv~crim+zn+crim:zn,data=Boston)
vis.gam(fit2)
##Diagnosing a generalized additive model
par(mfrow=c(2,2))
gam.check(fit)