-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit4.R
117 lines (78 loc) · 2.51 KB
/
unit4.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# plot of x verus probability in logistic regression
beta0 = -3.5
beta1 = 2.5
x = seq(0,3,by = 0.01)
plot(x, exp(beta0+beta1*x)/(1+exp(beta0+beta1*x)),xlab="x", ylab="probability")
library(MASS)
############# Data SAheart#######################################################
# read data
SAheart<-read.table('SAheart.txt',header=T)
attach(SAheart)
####logistic regression
## model fitting
model_logit = glm(chd ~ . , family = binomial(link='logit'), data=SAheart)
require(nnet)
model_logit2 <- multinom(chd~.,data=SAheart)
## model outputs
summary(model_logit)
str(model_logit)
model_logit$coef
model_logit$fit
as.numeric(model_logit$fit>0.5)
#sum(c(1,as.numeric(SAheart[1,1:8]))*model_logit$coef)
#exp(0.4972209)/(1+exp(0.4972209))
#model_logit$fit[1]
summary(model_logit2)
str(model_logit2)
model_logit2
model_logit2$fit
as.numeric(model_logit2$fit>0.5)
#### test data
Test_saheart = SAheart[1:50,1:8]
## prediction
pred_logit= predict(model_logit,Test_saheart, type="response")
as.numeric(pred_logit>=0.5)
pred_logit2= predict(model_logit2,Test_saheart)
###### linear discriminant analaysis
model_lda = lda(chd~sbp+tobacco+ldl+adiposity+
typea+obesity+alcohol+age,data=SAheart)
model_lda
str(model_lda)
mean(SAheart$chd)
mean(SAheart$sbp[SAheart$chd==0])
# prediction
pred_lda = predict(model_lda,Test_saheart )
# quadratic discriminant analysis
model_qda = qda(chd~sbp+tobacco+ldl+adiposity+
typea+obesity+alcohol+age,data=SAheart)
model_qda
pred_qda = predict(model_qda,Test_saheart)
# comparison
cbind(pred_lda$class, pred_qda$class, pred_logit2,chd[1:50]+1)
table(pred_lda$class, chd[1:50])
table(pred_qda$class, chd[1:50])
table(pred_logit2, chd[1:50])
detach(SAheart)
############## Data iris#######################################################
# read data
iris<-read.table('iris.txt',header=T)
attach(iris)
n= dim(iris)[1]
# randomly row-permutate data
iris2 = iris[sample(1:n),]
# linear discriminant analaysis
training = iris2[1:100,]
model_lda = lda( Species ~ SepalLength+ SepalWidth +
PetalLength+ PetalWidth,data= training)
# test data
Test = iris2[101:150,1:4]
# prediction
pred_lda = predict(model_lda,Test)
# quadratic discriminant analysis
model_qda = qda(Species ~ .,data=training)
pred_qda = predict(model_qda,Test)
# comparison
data.frame(pred_lda$class, pred_qda$class,iris2[101:150,5])
table(pred_lda$class, iris2[101:150,5])
table(pred_qda$class, iris2[101:150,5])
detach(iris)