forked from mitujadhav/Data-Science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglm.R
58 lines (42 loc) · 1.64 KB
/
glm.R
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
#--------------------------------------------------------------------
# Used mtcars dataset for predicting type of car automatic or manual
# variable "mtcars$am" as dependent variable
# variables mtcars$wd and mtcars$hp as independent variables
#--------------------------------------------------------------------
y<-mtcars$am
x<-mtcars$hp
z<-mtcars$wt
newdata = data.frame(hp=160, wt=3.8)
#------------------------------------------------------
# Basic Formula of LM function
# y = beta0 + beta1*x
#------------------------------------------------------
# coefficients of lm function :
fit<-lm(am~hp,data=mtcars)
coef(fit)
# Maths behind lm function :
# beta1 (here hp)
beta1<-cor(y,x)*sd(y)/sd(x)
beta1
# Intercept i.e. beta0 :
intercept<-mean(y)-beta1*mean(x)
intercept
#------------------------------------------------------
#predicted value by predict function :
predict(fit, newdata, type="response")
# Formula behind predict function:
intercept+beta1*newdata$hp
#------------------------------------------------------
# Basic Formula of GLM function
# bx = beta0 + beta1*x + ....
# p = 1/(1+exp(-bx))
# Note : GLM Uses log of odds fuction rather than probability.
#------------------------------------------------------
am.glm = glm(formula=am ~ hp + wt,data=mtcars,family=binomial)
coef(am.glm)
# Prediction by using predict() function :
predict(am.glm, newdata, type="response")
# Maths behind GLM predict function :
bx<-coef(am.glm)[1] + (coef(am.glm)[2]*newdata$hp) + (coef(am.glm)[3]*newdata$wt)
predictedval2<-1/(1+exp(-bx))
predictedval2