-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path.Rhistory
More file actions
237 lines (237 loc) · 8.76 KB
/
.Rhistory
File metadata and controls
237 lines (237 loc) · 8.76 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
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
knitr::opts_chunk$set(echo = FALSE)
knitr::include_graphics("progit2.png")
knitr::include_graphics("progit2.png")
knitr::include_graphics("progit2.png")
knitr::include_graphics("progit2.png")
ls()
ls
ls()
x <- 3
ls()
knitr::include_graphics("signup.png")
knitr::include_graphics("yourprofile.png")
knitr::include_graphics("your-profile.png")
knitr::include_graphics("my-profile.png")
knitr::include_graphics("new-repo.png")
knitr::include_graphics("new-repo.png")
knitr::include_graphics("new-repo.png")
knitr::include_graphics("fever.png")
knitr::include_graphics("fever.png")
knitr::include_graphics("fever.PNG")
knitr::include_graphics("fever.jpg")
knitr::include_graphics("fever.jpg")
knitr::include_graphics("fever.jpg")
knitr::include_graphics("Octocat.png")
?.subset
install.packages(c("investr", "scam"))
library(investr)
library(mgcv)
library(scam)
### GAM/SCAM/GP/Kriging
#' Inverse Prediction Function
#'
#' Reverse predicts x for a given y.
#'
#' @param object: A fitted model object.
#' objects).
#'
#' @param y: The response value of interest.
#'
#' @param x.name: Character string giving the name of the (single) independent
#' variable.
#'
#' @param ... Additional arguments to be passed onto \code{uniroot}.
#'
#' @export
inv.predict <- function(object, y, x.name, ...) {
UseMethod("inv.predict")
}
#' @rdname inv.predict
#' @export
inv.predict.default <- function(object, y, x.name, interval = FALSE,
lower, upper, level = 0.95,...) {
x0.est <- uniroot(lower = lower, upper = upper, extendInt = "yes", ..., f = function(x) {
predFit(object, newdata = setNames(data.frame(x), x.name)) - y
})$root
res <- if (interval) {
lwr <- uniroot(lower = lower, upper = x0.est, extendInt = "yes", ..., f = function(x) {
predFit(object, newdata = setNames(data.frame(x), x.name),
interval = "confidence")[, "upr"] - y
})$root
upr <- uniroot(lower = x0.est, upper = upper, extendInt = "yes", ..., f = function(x) {
predFit(object, newdata = setNames(data.frame(x), x.name),
interval = "confidence")[, "lwr"] - y
})$root
lwr <- min(c(lwr, upr))
upr <- max(c(lwr, upr))
c("estimate" = x0.est, "lower" = lwr, "upper" = upr)
} else {
x0.est
}
res
}
#' GAM prediction
#' @rdname predFit
#' @export
predFit.gam <- function(object, newdata, type = c("link", "response"),
interval = c("none", "confidence", "prediction"),
level = 0.95, ...) {
type <- match.arg(type)
interval <- match.arg(interval)
res <- if (interval == "none") {
predict.gam(object, newdata = newdata, type = type, ...)
} else if (interval == "confidence") {
pred <- predict.gam(object, newdata = newdata, se.fit = TRUE, type = "link",
...)
out <- cbind("fit" = pred$fit,
"lwr" = pred$fit - pred$se.fit * stats::qnorm((level + 1) / 2),
"upr" = pred$fit + pred$se.fit * stats::qnorm((level + 1) / 2))
if (type == "response") {
out <- apply(out, MARGIN = 2, FUN = function(x) {
stats::family(object)$linkinv(x)
})
}
out
} else {
stop("Prediction intervals are currently not supported for GAMs.")
}
res
}
#' SCAM prediction
#' @rdname predFit
#' @export
predFit.scam <- function(object, newdata, type = c("link", "response"),
interval = c("none", "confidence", "prediction"),
level = 0.95, ...) {
type <- match.arg(type)
interval <- match.arg(interval)
res <- if (interval == "none") {
predict.scam(object, newdata = newdata, type = type, ...)
} else if (interval == "confidence") {
pred <- predict.scam(object, newdata = newdata, se.fit = TRUE, type = "link",
...)
out <- cbind("fit" = pred$fit,
"lwr" = pred$fit - pred$se.fit * stats::qnorm((level + 1) / 2),
"upr" = pred$fit + pred$se.fit * stats::qnorm((level + 1) / 2))
if (type == "response") {
out <- apply(out, MARGIN = 2, FUN = function(x) {
stats::family(object)$linkinv(x)
})
}
out
} else {
stop("Prediction intervals are currently not supported for SCAMs.")
}
res
}
# ptm <- proc.time()
N <- 50 ### How many individuals are in each permutation
Hstar <- 2 ### Assume we know how many total haplotypes are possible
perm <- 10000 ### How many permutations are we considering
specs <- 1:N
### Set up a container to hold the identity of each individual from each permutation
pop <- array(dim = c(perm, N))
### Create an ID for each haplotype
haps <- as.character(1:Hstar)
### Assign probabilities of occurrence to each haplotype, ensure they sum to 1
### This is where we assume we "know" the distribution of haplotypes
### Here, I have assumed they all occur with equal frequency, but you can change this to assume some dominant ones and some
### rare ones, whatever you want
# probs <- rep(1/Hstar, Hstar)
probs <- c(0.5, 0.5)
# probs <- runif(H) # random values from U(0, 1) distribution
# probs <- sort(probs/sum(probs), decreasing = TRUE)
### Generate permutations, we assume each permutation has N individuals, and we sample those individuals' haplotypes
### from our probabilities
for(i in 1:perm){
pop[i,] <- sample(haps, size = N, replace = TRUE, prob = probs)
}
### Make a matrix to hold the 1:N individuals from each permutation
HAC.mat <- array(dim = c(perm, N))
for(j in specs){
for(i in 1:perm){
ind.index <- sample(specs, size = j, replace = FALSE) ## which individuals will we sample
hap.plot <- pop[i, ind.index] ## pull those individuals from a permutation
HAC.mat[i,j] <- length(unique(hap.plot)) ## how many haplotypes did we get for a given sampling intensity (j) from each ### permutation (i)
}
}
### Calculate the mean and CI for number of haplotypes at each sampling intensity (j)
means <- apply(HAC.mat, MARGIN = 2, mean)
lower <- apply(HAC.mat, MARGIN = 2, function(x) quantile(x, 0.025))
upper <- apply(HAC.mat, MARGIN = 2, function(x) quantile(x, 0.975))
### Plot the curve and frequency barplot
par(mfrow = c(1, 2))
plot(specs, means, type = "n", xlab= "Specimens sampled", ylab = "Unique haplotypes", ylim = c(1, H))
polygon(x = c(specs, rev(specs)), y = c(lower, rev(upper)), col = "gray")
lines(specs, means, lwd = 2)
# HAC.bar <- barplot(N*probs, xlab = "Unique haplotypes", ylab = "Specimens sampled", names.arg = 1:H)
d <- data.frame(specs, means)
plot(specs, means, type = "n", xlab= "Specimens sampled", ylab = "Unique haplotypes")
polygon(x = c(specs, rev(specs)), y = c(lower, rev(upper)), col = "gray")
lines(specs, means, lwd = 2)
d <- data.frame(specs, means)
HAC.mod <- lm(log(means) ~ log(specs), data = d)
HAC.nls <- nls(means ~ a*specs^b, start=list(a = exp(coef(HAC.mod)[1]), b = coef(HAC.mod)[2]), data = d)
AIC(HAC.nls)
### Plot curve
x <- specs
curve(coef(HAC.nls)[1]*x^coef(HAC.nls)[2], add = TRUE, from = 1, to = N, col = "red", lwd = 2)
### Calculate N*
HAC.invest <- invest(HAC.nls, y0 = Hstar, interval = "inversion", upper = 1000000)
HAC.invest
# GAMs
HAC.cr <- gam(means ~ s(specs, bs = "cr", k = 10), optimizer = c("outer", "bfgs"), data = d)
HAC.cr$aic
HAC.cr <- inv.predict(HAC.cr, y = Hstar, x.name = "specs", interval = TRUE, lower = 1, upper = 1000000)
HAC.cr
plot(HAC.cr)
Hstar
library(pdp)
partial(HAC.cr, pred.var = "specs", plot = TRUE)
plot(d)
lines(xx, yy, type = "l")
xx <- seq(from = min(d$specs), to = max(d$specs))
yy <- predict(HAC.dr, newdata = data.frame(specs = xx))
yy <- predict(HAC.cr, newdata = data.frame(specs = xx))
plot(d)
lines(xx, yy, type = "l")
plot(d, ylim = c(1, 2))
lines(xx, yy, type = "l")
plot(d, ylim = c(1, 2.5))
lines(xx, yy, type = "l")
plot(d, ylim = c(0.8, 2.2))
lines(xx, yy, type = "l")
inv.predict(HAC.cr, y = 1.6, x.name = "specs", interval = TRUE, lower = 1, upper = 1000000)
inv.predict(HAC.cr, y = 1.6, x.name = "specs", interval = TRUE)
inv.predict(HAC.cr, y = 1.6, x.name = "specs", interval = TRUE, lower = 0, upper = 10)
xx <- seq(from = min(d$specs), to = max(d$specs) + 100000, length = 500)
yy <- predict(HAC.cr, newdata = data.frame(specs = xx))
plot(d, ylim = c(0.8, 2.2))
lines(xx, yy, type = "l")
partial(HAC.nls, pred.var = "specs", plot = T)
xx <- seq(from = min(d$specs), to = max(d$specs), length = 500)
yy <- predict(HAC.cr, newdata = data.frame(specs = xx))
plot(d, ylim = c(0.8, 2.2))
lines(xx, yy, type = "l")
Hstar
abline(h = Hstar, lty = 2, col = "red")
HAC.gps <- gam(means ~ s(specs, bs = "gp", k = 10, m = 1), optimizer = c("outer", "bfgs"), data = d) # spherical
plot(HAC.gps)
HAC.mpi <- scam(means ~ s(specs, bs = "mpi", k = 10), data = d) # mpi
plot(HAC.mpi)
HAC.mpi <- inv.predict(HAC.mpi, y = Hstar, x.name = "specs", interval = TRUE, lower = 1, upper = 1000000)
xx <- seq(from = min(d$specs), to = max(d$specs), length = 500)
yy <- predict(HAC.mpi, newdata = data.frame(specs = xx))
plot(d, ylim = c(0.8, 2.2))
lines(xx, yy, type = "l")
abline(h = Hstar, lty = 2, col = "red") # looks like multiple solutions
HAC.mpi <- inv.predict(HAC.mpi, y = Hstar, x.name = "specs", interval = TRUE, lower = 1, upper = 10)
devtools::install_github("hadley/emo")
add
source('C:/Users/greenweb/Desktop/testrepo/add.R', echo=TRUE)
add(1, 3.5)
library(ggplot2)
ggplot(cars, aes_string(x = "dist", y = "speed") + geom_point()
)
ggplot(cars, aes_string(x = "dist", y = "speed")) + geom_point()
file.exists("~/.ssh/id_rsa.pub")