-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_based_methods.R
More file actions
154 lines (125 loc) · 3.01 KB
/
kernel_based_methods.R
File metadata and controls
154 lines (125 loc) · 3.01 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
############### Non-Parametric Statistics: Kernel Based Methods ################
### This code examines kernel based regression and kernel density estimation ###
################################################################################
k<-function(x, type="gaussian"){
if(type=="gaussian"){
temp<-dnorm(x)
}
if(type=="logistic"){
temp<-(2/pi)*exp(x)/(1+exp(2*x))
}
if(type=="epanechnikov"){
temp<-.75*(1-x^2)
}
return(temp)
}
# Non-parametric regression
w<-function(x, h, X, type){
num<-k((X-x)/h, type)/h
denom<-sum(k((X-x)/h, type)/h)
temp<-num/denom
return(temp)
}
r_x<-function(x, h, X, Y, type){
weight<-w(x, h, X, type)
temp<-sum(weight*Y)
return(temp)
}
r<-function(X, Y, n=100, h=1/10, type="gaussian"){
n<-length(X)
x<-seq(min(X), max(X), length.out=n)
y_hat<-c()
for(point in x){
y_hat<-c(y_hat, r_x(point, h, X, Y, type))
}
return(list(x=x, y_hat=y_hat))
}
# Density estimation
p_x<-function(x, h, X, type){
n<-length(X)
temp<-sum(k((x-X)/h, type)/h)/n
}
p<-function(X, n=1000, h=1/10, type="gaussian"){
x<-seq(-10*max(abs(X)),10*max(abs(X)), length.out=n)
p_hat<-c()
for(point in x){
p_hat<-c(p_hat, p_x(point, h, X, type))
}
return(list(x=x, p_hat=p_hat))
}
# Normal Example
## Non-parametric regression
n<-100
X<-seq(1, 20, length.out=n)
Y<-sqrt(X)+X*exp(-X)*sin(X)+rnorm(n, 0, .1)
Y_hat<-r(X, Y, h=1/2)
plot(X, Y)
points(Y_hat$x, Y_hat$y_hat, type="l")
## KDE
p_hat<-p(Y, h=1/2)
plot(density(Y), col="orange")
points(p_hat$x, p_hat$p_hat, type="l", col="blue")
# Logistic Example
## Non-parametric regression
n<-100
h<-1/10
b<-5.01
x<-seq(-2, 2, length.out=n)
pi<-exp(x*b)/(1+exp(x*b))
Y<-c()
for(i in 1:n){
Y<-c(Y, rbinom(1,1,pi[i]))
}
Y_hat<-r(x, Y, h=1/2)
plot(x, Y)
points(Y_hat$x, Y_hat$y_hat, type="l")
## KDE
p_hat<-p(Y, h=1/5)
plot(density(Y), col="orange")
points(p_hat$x, p_hat$p_hat, type="l", col="blue")
# Poisson Example
## Non-parametric regression
n<-100
x<-seq(-2,2,length.out=n)
X<-matrix(c(rep(1,n), x),ncol=2)
b<-c(.922,-.501)
lam<-exp(X%*%b)
Y<-c()
for(i in 1:n){
Y<-c(Y, rpois(1, lam[i]))
}
Y_hat<-r(x, Y)
plot(x, Y)
points(Y_hat$x, Y_hat$y_hat, type="l")
# KDE
p_hat<-p(Y, h=1/2)
plot(density(Y), col="orange")
points(p_hat$x, p_hat$p_hat, type="l", col="blue")
# Alcohol Example
## Read data
mvalc <- read.csv("/home/pat-mellady/Desktop/Tidy gark/alc_data.csv", header=T)
## Tidy Data
rownames(mvalc) <- mvalc$Wedding
mvalc<-t(mvalc[,-1])
## Define Variables
X<-mvalc[,1]
X2<-X^2
Y<-mvalc[,62]
Y_w<-mvalc[,63]
high<-mvalc[c(2,6,7,9,12,13),c(1,62)]
low<-mvalc[-c(2,6,7,9,12,13), c(1,62)]
Y_h<-high[,2]
X_h<-high[,1]
X_h2<-X_h^2
Y_l<-low[,2]
X_l<-low[,1]
X_l2<-X_l^2
## Non-parametric regression
x_h<-seq(min(X_h), max(X_h), length.out=10*length(X_h))
Y_h_hat<-r(X_h, Y_h, h=10)
plot(X_h, Y_h, col="blue")
points(Y_h_hat$x, Y_h_hat$y_hat, type="l", col="blue")
x_l<-seq(min(X_l), max(X_l), length.out=10*length(X_l))
Y_l_hat<-r(X_l, Y_l, h=1)
points(X_l, Y_l, col="orange")
points(Y_l_hat$x, Y_l_hat$y_hat, type="l", col="orange")