-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlogistic.R
More file actions
27 lines (26 loc) · 817 Bytes
/
logistic.R
File metadata and controls
27 lines (26 loc) · 817 Bytes
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
logisticLR<-function(Y=NA, data=NA, alpha=0.05, maxIterNum=1000){
if(is.na(Y) || is.na(data)){
stop("input data should not be NA")
}
sigmoid <- function(z) { 1 / (1 + exp(-z))}
response<-as.numeric(data[[Y]])-1
X<-data
X[[Y]]<-NULL
X<-as.matrix(X)
X<-cbind(intercept=0,X)
W<-rep(0,ncol(X))
for(i in 1:maxIterNum){
W<- W - alpha * (t(X) %*% (sigmoid(X %*% W) - response))
}
return(list("weight"=W))
}
predict.logistic<-function(model=NA, new_data=NA){
if(is.na(model) || is.na(new_data)){
stop("input data should not be NA")
}
sigmoid <- function(z) { 1 / (1 + exp(-z))}
X<-cbind(intercept=0,new_data)
result.prob<-sigmoid(X %*% model$weight)
result.class<-ifelse(result.prob>=0.5,1,0)
return(list("prob"=result.prob,"class"=result.class))
}