-
Notifications
You must be signed in to change notification settings - Fork 3
/
.Rhistory
512 lines (512 loc) · 15.8 KB
/
.Rhistory
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
glm(Z~X1+X2,family=binomial,data=largedb)
exp(ols(vY, cbind(vX1, vX2), intercept = TRUE))
1 / 1+ exp(-(ols(vY, cbind(vX1, vX2), intercept = TRUE)))
dat <- data.frame()
fix(dat)
View(dat)
hours <- c(0.50, 0.75, 1.00, 1.25, 1.50, 1.75, 1.75, 2.00, 2.25, 2.50, 2.75, 3.00, 3.25, 3.50, 4.00, 4.25, 4.50, 4.75, 5.00, 5.50)
Pass <- c( 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1)
glm(Pass ~ hours, family = binomial)
pass <- c( 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1)
pass <- c( 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1)
rm(Pass)
glm(pass ~ hours, family = binomial)
1 / 1+ exp(-(ols(pass, hours, intercept = TRUE)))
1 / (1 + exp(-(ols(pass, hours, intercept = TRUE)))
)
1 / 1 + exp(-(-4.0777+1.5046*2) )
1 / ( 1 + exp(-(-4.0777+1.5046*2) ) )
lm(log(pass) ~ hours)
ln(pass)
log(pass)
?log
lm(pass ~ log(hours))
lm(pass ~ log(1/hours))
lm(pass ~ log(1-(1/hours) ))
lm(pass ~ log(hours)/(1-log(hours) ) )
lm(pass ~ 1/(1+exp(hours) ) )
?mle
x <- 0:10
y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8)
nLL <- function(lambda) -sum(stats::dpois(y, lambda, log = TRUE))
fit0 <- mle(nLL, start = list(lambda = 5), nobs = NROW(y))
library(stats4)
fit0 <- mle(nLL, start = list(lambda = 5), nobs = NROW(y))
fit0
mydata = read.csv(url('http://www.ats.ucla.edu/stat/r/dae/binary.csv'))
library(haven)
?read_sas(url('binary.sas7bdat'))
sas<-read_sas(url('binary.sas7bdat'))
download.file('http://www.ats.ucla.edu/stat/sas/dae/binary.sas7bdat')
sas<-read_sas(url('http://www.ats.ucla.edu/stat/sas/dae/binary.sas7bdat'))
sas<-read_sas(url('http://www.ats.ucla.edu/stat/sas/dae/binary.sas7bdat'), path=".")
download.file('http://www.ats.ucla.edu/stat/sas/dae/binary.sas7bdat', destfile = "binary.sas7bdat")
binary <- read_sas('binary.sas7bdat')
binary <- read_sas('binary.sas7bdat')
glm(admit~gre+gpa+as.factor(rank), family=binomial, data=binary)
head(binary)
names(binary) <- tolower(names(binary))
head(binary)
glm(admit~gre+gpa+as.factor(rank), family=binomial, data=binary)
mle.logreg = function(fmla, data)
{
# Define the negative log likelihood function
logl <- function(theta,x,y){
y <- y
x <- as.matrix(x)
beta <- theta[1:ncol(x)]
# Use the log-likelihood of the Bernouilli distribution, where p is
# defined as the logistic transformation of a linear combination
# of predictors, according to logit(p)=(x%*%beta)
loglik <- sum(-y*log(1 + exp(-(x%*%beta))) - (1-y)*log(1 + exp(x%*%beta)))
return(-loglik)
}
# Prepare the data
outcome = rownames(attr(terms(fmla),"factors"))[1]
dfrTmp = model.frame(data)
x = as.matrix(model.matrix(fmla, data=dfrTmp))
y = as.numeric(as.matrix(data[,match(outcome,colnames(data))]))
# Define initial values for the parameters
theta.start = rep(0,(dim(x)[2]))
names(theta.start) = colnames(x)
# Calculate the maximum likelihood
mle = optim(theta.start,logl,x=x,y=y,hessian=T)
out = list(beta=mle$par,vcov=solve(mle$hessian),ll=2*mle$value)
}
mydata$rank = factor(mydata$rank) #Treat rank as a categorical variable
binary$rank = factor(binary$rank) #Treat rank as a categorical variable
fmla = as.formula("admit~gre+gpa+rank") #Create model formula
mylogit = mle.logreg(fmla, binary) #Estimate coefficients
mylogit
glm(admit~gre+gpa+as.factor(rank), family=binomial, data=binary)
mle.logreg = function(fmla, data)
{
# Define the negative log likelihood function
logl <- function(theta,x,y){
y <- y
x <- as.matrix(x)
beta <- theta[1:ncol(x)]
# Use the log-likelihood of the Bernouilli distribution, where p is
# defined as the logistic transformation of a linear combination
# of predictors, according to logit(p)=(x%*%beta)
loglik <- sum(-y*log(1 + exp(-(x%*%beta))) - (1-y)*log(1 + exp(x%*%beta)))
return(-loglik)
}
# Prepare the data
outcome = rownames(attr(terms(fmla),"factors"))[1]
dfrTmp = model.frame(data)
x = as.matrix(model.matrix(fmla, data=dfrTmp))
y = as.numeric(as.matrix(data[,match(outcome,colnames(data))]))
# Define initial values for the parameters
theta.start = rep(0,(dim(x)[2]))
names(theta.start) = colnames(x)
# Calculate the maximum likelihood
mle = optim(theta.start,logl,x=x,y=y,hessian=F)
# Obtain regression coefficients
beta = mle$par
# Calculate the Information matrix
# The variance of a Bernouilli distribution is given by p(1-p)
p = 1/(1+exp(-x%*%beta))
V = array(0,dim=c(dim(x)[1],dim(x)[1]))
diag(V) = p*(1-p)
IB = t(x)%*%V%*%x
# Return estimates
out = list(beta=beta,vcov=solve(IB),dev=2*mle$value)
}
mylogit = mle.logreg(fmla, binary) #Estimate coefficients
mylogit
glm(admit~gre+gpa+as.factor(rank), family=binomial, data=binary)
?optim
logitdata <- read.table("~/Downloads/ps206data1b.txt", header=T, sep="\t")
logitdata <- as.data.frame(logitdata)
attach(logitdata)
logitmodel <- glm(itn ~ altitude, family=binomial(link="logit"), data=logitdata)
summary(logitmodel)
logitmodel2 <- glm(itn ~ 1, family=binomial(link="logit"), data=logitdata)
summary(logitmodel2)
summary(logitmodel)
log(mean(itn)) - log(1 - mean(itn))
X <- cbind(1, altitude)
y <- itn
K <- as.numeric(ncol(X))
startvalue.model <- lm(itn ~ altitude)
startv <- startvalue.model$coefficients
logit.lf <- function(beta) {
exb <- exp(X%*%beta)
prob1 <- exb/(1+exb)
logexb <- log(prob1)
y0 <- 1 - y
logexb0 <- log(1 - prob1)
yt <- t(y)
y0t <- t(y0)
logl <- -sum(yt%*%logexb + y0t%*%logexb0)
return(logl)
}
logit.gr <- function(beta) {
grad <- beta*0
exb <- exp(X%*%beta)
prob1 <- exb/(1+exb)
for (k in 1:K) {
grad[k] <- sum(X[,k] * (y - prob1))
}
return(-grad)
}
logitmodel <- optim(startv, logit.lf, gr=logit.gr, method="BFGS", control=list(trace=TRUE, REPORT=1), hessian=TRUE)
coeffs <- logitmodel$par
coeffs
summary(logitmodel)
glm(itn ~ altitude, family=binomial(link="logit"), data=logitdata)
coeffs
?optim
library(servr)
jekyll()
X = matrix(c(0,0,1,0,1,1,1,0,1,1,1,1), nrow=4, byrow=TRUE)
y = matrix(c(0,1,1,0),nrow=4)
x
X
y
syn0 = matrix(runif(n = 12, min=-1, max=1), nrow=3)
syn1 = matrix(runif(n = 4, min=-1, max=1), nrow=4)
syn0
syn0 = matrix(runif(n = 12, min=-1, max=1), nrow=3)
syn0
for (j in 1:60000) {
# Feed forward through layers 0, 1, and 2
l0 = X
l1 = nonlin(l0%*%syn0)
l2 = nonlin(l1%*%syn1)
# how much did we miss the target value?
l2_error = y - l2
if (j %% 10000 == 0)
print(paste("Error:", mean(abs(l2_error))))
# in what direction is the target value?
# were we really sure? if so, don't change too much.
l2_delta = l2_error*nonlin(l2,deriv=TRUE)
# how much did each L1 value contribute to the error (according to the weights)?
l1_error = l2_delta %*% t(syn1)
# in what direction is the target l1?
# were we really sure? if so, don't change too much.
l1_delta = l1_error * nonlin(l1, deriv=TRUE)
syn1 = syn1 + t(l1) %*% l2_delta
syn0 = syn0 + t(l0) %*% l1_delta }
nonlin = function(x,deriv=FALSE) {
if(deriv==TRUE)
return( x*(1-x) )
return( 1/(1+exp(-x)) )
}
nonlin
for (j in 1:60000) {
# Feed forward through layers 0, 1, and 2
l0 = X
l1 = nonlin(l0%*%syn0)
l2 = nonlin(l1%*%syn1)
# how much did we miss the target value?
l2_error = y - l2
if (j %% 10000 == 0)
print(paste("Error:", mean(abs(l2_error))))
# in what direction is the target value?
# were we really sure? if so, don't change too much.
l2_delta = l2_error*nonlin(l2,deriv=TRUE)
# how much did each L1 value contribute to the error (according to the weights)?
l1_error = l2_delta %*% t(syn1)
# in what direction is the target l1?
# were we really sure? if so, don't change too much.
l1_delta = l1_error * nonlin(l1, deriv=TRUE)
syn1 = syn1 + t(l1) %*% l2_delta
syn0 = syn0 + t(l0) %*% l1_delta }
data(swiss)
str(swiss)
m1 <- formula(Fertility ~ Agriculture + Examination + Education + Catholic + Infant.Mortality)
lm1 <- lm(formula=m1, data=swiss)
summary(lm1)
library(nnet)
nn1 <- nnet(formula=m1, data=swiss, size=0, skip=TRUE, linout=TRUE)
summary(nn1)
library(NeuralNetTools)
plotnet(nn1)
library(neuralnet)
nn1a <- neuralnet(formula=m1, data=swiss, hidden=0, linear.output=TRUE)
plot(nn1a, rep='best')
plot(nn1a, rep='best')
?pow
?power
@^8
2^8
?`^`
# no importing here
set.set(0)
# compute sigmoid nonlinearity
sigmoid = function(x) {
output = 1 / (1+exp(-x))
return(output) }
# convert output of sigmoid function to its derivative
signmoid_output_to_derivative = function(output) {
return( output*(1-output) ) }
# training dataset generation
int2binary = array()
binary_dim = 8
largest_number = 2^binary_dim
?readBin
as.integer(intToBits(5))
intToBits(5)
t(as.integer(intToBits(5)))
as.integer(intToBits(5))
sapply(5,function(x){ as.integer(intToBits(x))})
?sapply
sapply(5,function(x){ as.integer(intToBits(x))})
as.integer(intToBits(5))
as.vector(intToBits(5))
as.integer(intToBits(5))
?intToBits
rawToBits(5)
rev(as.integer(intToBits(5)))
intToBits
binary = function(x) {
rev(as.integer(intToBits())) }
binary(6)
binary = function(x) {
rev(as.integer(intToBits(x))) }
binary = function(x) {
binary(6)
?tail
tail(binary(256))
binary(356)
binary(256)
binary(255)
tail(binary(255), binary_dim=8)
binary = function(x) {
tail(rev(as.integer(intToBits(x))), binary_dim) }
binary(256)
range(256)
?range
for (in in 1:largest_number) {
int2binary[i] = binary[i] }
for (i in 1:largest_number) {
int2binary[i] = binary[i] }
for (i in 1:largest_number) {
int2binary[i] = binary[i] }
binary(5)
matrix(runif(n = 3, min=-1, max=1), nrow=3)
?runif
synapse_0 = matrix(runif(n = input_dim*hidden_dim, min=-1, max=1), nrow=hidden_dim)
# input variables
alpha = 0.1
input_dim = 2
hidden_dim = 16
output_dim = 1
# initialize neural network weights
synapse_0 = matrix(runif(n = input_dim*hidden_dim, min=-1, max=1), nrow=hidden_dim)
synapse_0
synapse_0 = matrix(runif(n = input_dim*hidden_dim, min=-1, max=1), nrow=input_dim)
synapse_0
synapse_0 = matrix(runif(n = input_dim*hidden_dim, min=-1, max=1), nrow=input_dim)
synapse_1 = matrix(runif(n = hidden_dim*output_dim, min=-1, max=1), nrow=output_dim)
syanpse_h = matrix(runif(n = hidden_dim*hidden_dim, min=-1, max=1), nrow=hidden_dim)
synapse_0
a <- (synapse_0 <- NULL)
a
?runif
?rint
?vector
vector(length=8)
a_int = sample(1:largest_number/2) # int version
a_int = sample(1:largest_number/2, 1) # int version
a = int2binary(a_int) # binary encoding
int2binary = function(x) {
tail(rev(as.integer(intToBits(x))), binary_dim) }
a = int2binary(a_int) # binary encoding
a
d = matrix(0, nrow = 1, ncol = binary_dim)
layer_1_values <- list(layer_1_values, vector(lengt=hidden_dim))
layer_1_values = list()
layer_1_values <- list(layer_1_values, vector(lengt=hidden_dim))
layer_1_values
layer_1_values = list()
layer_1_values <- c(layer_1_values, vector(lengt=hidden_dim))
layer_1_values
layer_1_values <- c(layer_1_values, vector(length=hidden_dim))
layer_1_values = list()
layer_1_values <- c(layer_1_values, vector(length=hidden_dim))
layer_1_values
layer_1_values <- c(layer_1_values, vector(length=hidden_dim))
layer_1_values
layer_1_values = list()
layer_1_values[i] <- vector(length=hidden_dim)
a[5]
typeof(rbind(a[5],b[5]))
b_int = sample(1:largest_number/2)
b = int2binary(b_int)
# true answer
c_int = a_int + b_int
c = int2binary(c_int)
# where we'll store our best guesss (binary encoded)
d = matrix(0, nrow = 1, ncol = binary_dim)
overallError = 0
layer_2_deltas = list()
layer_1_values = list()
layer_1_values <- c(layer_1_values, vector(length=hidden_dim)) # this doesn't work yet
typeof(rbind(a[5],b[5]))
rbind(a[5],b[5])
str(rbind(a[5],b[5]))
t(rbind(a[5],b[5]))
# hidden layer (input ~+ prev_hidden)
layer_1 = sigmoid(X%*%synapse_0) +
(layer_1_values %*% syanpse_h)
X = rbind(a[binary_dim - position],b[binary_dim - position])
position(1)
position = 1
# generate input and output
X = rbind(a[binary_dim - position],b[binary_dim - position])
y = c[binary_dim - position]
# hidden layer (input ~+ prev_hidden)
layer_1 = sigmoid(X%*%synapse_0) +
(layer_1_values %*% syanpse_h)
?`%*%
`
?`%*%`
# no importing here
set.set(0)
# compute sigmoid nonlinearity
sigmoid = function(x) {
output = 1 / (1+exp(-x))
return(output) }
# convert output of sigmoid function to its derivative
signmoid_output_to_derivative = function(output) {
return( output*(1-output) ) }
# training dataset generation
# int2binary =
binary_dim = 8
largest_number = 2^binary_dim
int2binary = function(x) {
tail(rev(as.integer(intToBits(x))), binary_dim) }
# for (i in 1:largest_number) {
# int2binary[i] = binary[i] }
# input variables
alpha = 0.1
input_dim = 2
hidden_dim = 16
output_dim = 1
# initialize neural network weights
synapse_0 = matrix(runif(n = input_dim*hidden_dim, min=-1, max=1), nrow=input_dim)
synapse_1 = matrix(runif(n = hidden_dim*output_dim, min=-1, max=1), nrow=output_dim)
synapse_h = matrix(runif(n = hidden_dim*hidden_dim, min=-1, max=1), nrow=hidden_dim)
synapse_0_update = matrix(0, nrow = input_dim, ncol = hidden_dim)
synapse_1_update = matrix(0, nrow = hidden_dim, ncol = output_dim)
synapse_h_update = matrix(0, nrow = hidden_dim, ncol = hidden_dim)
position = 1
# generate input and output
X = rbind(a[binary_dim - position],b[binary_dim - position])
y = c[binary_dim - position]
# generate a simple addition problem (a + b = c)
a_int = sample(1:largest_number/2, 1) # int version
a = int2binary(a_int) # binary encoding
b_int = sample(1:largest_number/2)
b = int2binary(b_int)
# true answer
c_int = a_int + b_int
c = int2binary(c_int)
# where we'll store our best guesss (binary encoded)
d = matrix(0, nrow = 1, ncol = binary_dim)
overallError = 0
layer_2_deltas = matrix()
layer_1_values = matrix()
layer_1_values <- c(layer_1_values, vector(length=hidden_dim)) # this doesn't work yet
layer_1_values = matrix()
layer_1_values <- c(layer_1_values, matrix(0, nrow=1, ncol=hidden_dim)) # this doesn't work yet
layer_1_values <- c(layer_1_values, matrix(0, nrow=1, ncol=hidden_dim)) # this doesn't work yet
layer_1_values <- c(layer_1_values, matrix(0, nrow=1, ncol=hidden_dim)) # this doesn't work yet
layer_1_values = matrix()
layer_1_values <- rbind(layer_1_values, matrix(0, nrow=1, ncol=hidden_dim)) # this doesn't work yet
layer_1_values <- matrix(0, 1, 8)
layer_1 = sigmoid(X%*%synapse_0) +
(layer_1_values %*% synapse_h)
# generate input and output
X = rbind(a[binary_dim - position],b[binary_dim - position])
y = c[binary_dim - position]
# hidden layer (input ~+ prev_hidden)
layer_1 = sigmoid(X%*%synapse_0) +
(layer_1_values %*% synapse_h)
dim(X)
layer_1 = sigmoid((X%*%synapse_0) +
(layer_1_values %*% synapse_h))
(X%*%synapse_0)
dim(X)
dim(synapse_0)
t(X)%*%synapse_0
X = cbind(a[binary_dim - position],b[binary_dim - position])
(X%*%synapse_0)
layer_1 = sigmoid((X%*%synapse_0) +
(layer_1_values %*% synapse_h))
dim(layer_1_values)
dim(synapse_h)
layer_1_values = matrix(0 , nrow=1, ncol = hidden_dim)
layer_1_values = rbind(layer_1_values, matrix(0, nrow=1, ncol=hidden_dim)) # this doesn't work yet
layer_1 = sigmoid((X%*%synapse_0) +
(layer_1_values[position-1,] %*% synapse_h))
position
layer_1 = sigmoid((X%*%synapse_0) +
(layer_1_values[position,] %*% synapse_h))
?Abs
?abs
?round
library(servr)
jekyll(serve = FALSE)
jekyll(serve = FALSE)
jekyll(serve = FALSE)
jekyll(serve = FALSE)
library(wiod)
library(learNN)
help('learNN')
help(package = 'learNN')
help('learnn')
help(package = 'learNN')
jekyll(serve = FALSE)
jekyll(serve = FALSE)
data(wiod95)
ls()
str(ls())
ls()
str()
jekyll(serve = FALSE)
jekyll(serve = FALSE)
jekyll(serve = FALSE)
jekyll(serve = FALSE)
jekyll(serve = FALSE)
library(servr)
jekyll()
jekyll(serve = FALSE)
jekyll(serve = FALSE)
jekyll(serve = FALSE)
library(servr)
jekyll()
library(servr)
jekyll(serve = FALSE)
jekyll(serve = FALSE)
jekyll()
library(servr)
jekyll(serve = FALSE)
jekyll(serve = FALSE)
jekyll(serve = FALSE)
library(rnn)
?rnn
?ggplotly
library(plotly)
?ggplotly
install.packages("servr")
library(servr)
jekyll()
library(servr)
jekyll()
install.packages(c("colorspace", "gdtools", "ggplot2", "knitr", "R.utils", "scales", "SparseM", "XML", "yaml"))
library(decompr)
data(wiod11)
library(wiod)
data(wiod11)
decompr_object <- load_tables_vectors(inter11,
final11,
countries,
industries,
output11)
LI2<-leontief(decompr_object, post = "none",long=FALSE)
LI2<-leontief(decompr_object, post = "none")
LI2<-leontief(decompr_object, post = "exports", long=FALSE)