I have been collaborating on this code that creates an NMDS plot and I want to add shaded polygons of the points. However, the ordihull code keeps returning the following error. Why would the argument be of length zero?
Error in if (n < 4) return(colMeans(x[-n, , drop = FALSE])) : argument is of length zero
> m1 <- metaMDS(d1)
> m2 <- metaMDS(d2)
> m3 <- metaMDS(d3)
> mdat <- data.frame(m3$points)
> mdat$site <- substr(rownames(mdat), 1, 1) mdat$col <- ifelse(mdat$site == "D", "red",
ifelse(mdat$site == "H", "blue", "green"))
> plot(mdat[,1], mdat[,2], pch=16, col=mdat$col, display = "sites",
xlab="NMDS1", ylab="NMDS2", xlim=c(-0.2, 0.2),
ylim=c(-0.2, 0.2), main= "Phylum")
> ordihull(mdat[,1], mdat[,2], display="sites", label=T,
lwd=2, draw="polygon",col= c("blue", "red", "green"))
Here is the Dput:
> structure(list(p__Proteobacteria = c(44.807, 40.907, 36.558,36.811,
39.401, 40.114, 45.911, 43.133, 30.137, 27.734, 26.722,
31.261), p__Actinobacteria = c(26.819, 34.651, 40.904, 38.847,
39.446, 37.523, 29.881, 29.251, 31.783, 23.641, 34.918, 31.308
), p__Acidobacteria = c(8.48, 6.6, 5.934, 6.609, 5.89, 7.567,
5.795, 6.666, 10.616, 10.709, 8.988, 11.794), p__Bacteroidetes =
c(7.56, 8.189, 5.363, 6.223, 4.716, 3.613, 4.65, 5.2, 4.281, 2.785,
2.808, 3.271), p__Gemmatimonadetes = c(3.529, 2.108, 1.213, 1.193,
1.541, 1.439, 1.006, 1.171, 5.794, 4.107, 4.001, 2.747),
p__Chloroflexi = c(2.686, 2.987, 2.979, 3.049, 4.128, 4.564, 5.304,
4.624, 3.669, 2.775, 4.534, 4.94), p__Bacteria_unclassified =
c(2.38, 1.869, 1.579, 1.247, 2.3, 2.108, 1.36, 1.193, 3.126, 1.885,
2.987, 2.37), p__Firmicutes = c(0.998, 0.807, 2.76, 2.962, 0.866,
1.32, 1.651, 2.073, 1.099, 1.046, 1.3, 1.302), p__Verrucomicrobia =
c(0.676, 0.404, 0.32, 0.35, 0.293, 0.239, 0.188, 0.261, 0.521,
0.726, 0.52, 0.397), p__Nitrospirae = c(0.464, 0.244, 0.198, 0.208,
0.016, 0.032, 0.024, 0.042, 0.296, 0.103, 0.229, 0.211),
p__Candidatus_Saccharibacteria = c(0.421, 0.511, 0.456, 0.552,
0.523, 0.6, 0.842, 1.016, 0.672, 0.636, 0.465, 0.736),
p__Planctomycetes = c(0.392, 0.267, 0.354, 0.285, 0.275, 0.356,
0.285, 0.276, 0.33, 0.438, 0.552, 0.365), p__Fibrobacteres = c(0.14,
0.074, 0.007, 0.009, 0.072, 0.044, 0.136, 0.079, 0.117, 0.018,
0.167, 0.065), p__Candidatus_Latescibacteria = c(0.113, 0.059,
0.017, 0.005, 0.004, 0.017, 0.015, 0.009, 0, 0.011, 0.007, 0.018
), p__Latescibacteria = c(0.085, 0.04, 0.01, 0.004, 0.012, 0.015,
0.033, 0.015, 0.012, 0.016, 0.011, 0.018), p__Cyanobacteria =
c(0.079, 0.048, 1.071, 1.372, 0.32, 0.19, 2.629, 4.689, 7.133,
22.963, 11.417, 8.767), p__Thermodesulfobacteria = c(0.068, 0.057,
0.115, 0.103, 0.008, 0.01, 0.015, 0.007, 0.01, 0.003, 0.002, 0.013),
p__Elusimicrobia = c(0.059, 0.021, 0.012, 0.001, 0.004, 0.002,
0.015, 0.017, 0, 0.002, 0.005, 0.006), p__Chlorobi = c(0.052,
0.025, 0.002, 0.012, 0.029, 0.046, 0.033, 0.04, 0.05, 0.02,
0.046, 0.025), p__Armatimonadetes = c(0.046, 0.053, 0.051,
0.072, 0.076, 0.095, 0.048, 0.053, 0.197, 0.159, 0.128, 0.125
), p__Spirochaetes = c(0.035, 0.021, 0.002, 0.001, 0, 0.002,
0.024, 0.039, 0, 0, 0, 0), p__Parcubacteria = c(0.03, 0.013,
0, 0, 0.01, 0.015, 0.042, 0.037, 0.032, 0.059, 0.053, 0.011
), p__Chlamydiae = c(0.028, 0.017, 0.046, 0.05, 0.014, 0.007,
0.021, 0.022, 0.07, 0.074, 0.08, 0.152)), class = "data.frame",
row.names = c("D15B", "D610B", "D15F", "D610F", "HR15B", "HR610B",
"HR15F", "HR610F", "C15B", "C610B", "C15F", "C610F"))
Here are the codes:
> phylum.dat <- dput
> x <- data.frame(tax=names(phylum.dat), nsites=apply(phylum.dat, 2, function(x){length(which(x>0))}))
> d1 <- vegdist(phylum.dat, method = "jaccard", binary = TRUE)
> d2 <- vegdist(log1p(phylum.dat, method = "jaccard"))
> logit_phylum <- as.matrix(phylum.dat+1)/100
> d3 <- qlogis(logit_phylum)
> d3 <- d3+abs(min(d3))
> d3 <- vegdist(d3, method = "jaccard")
> m1 <- metaMDS(d1)
> m2 <- metaMDS(d2)
> m3 <- metaMDS(d3)
> e1 <- envfit(m3, phylum.dat)
> exy <- data.frame(tax=names(phylum.dat),
> x=e1$vectors$arrows[,1],
> y=e1$vectors$arrows[,2],
> pval=e1$vectors$pvals,
> r=e1$vectors$r)
> rownames(exy) <- NULL
> exy <- exy[order(-exy$r),]
> mdat <- data.frame(m3$points)
> mdat$site <- substr(rownames(mdat), 1, 1)
> mdat$col <- ifelse(mdat$site == "D", "red",
> ifelse(mdat$site == "H", "blue", "green"))
> mdat$rad <- sqrt((mdat$MDS1^2) + (mdat$MDS2^2))
> max(mdat$rad)
> exy$x2 <- 0.17 * exy$r * exy$x
> exy$y2 <- 0.17 * exy$r * exy$y
> exy$adj <- ifelse(exy$x < 0, 1, 0)
> plot(mdat[,1], mdat[,2], pch=16, col=mdat$col,
> xlab="NMDS1", ylab="NMDS2", xlim=c(-0.2, 0.2),
> ylim=c(-0.2, 0.2), main= "Phylum")
> ordihull(mdat[,1], mdat[,2], display="sites", label=T,
> lwd=2, draw="polygon",col= c("blue", "red", "green"))
This is my data frame, with two columns Y (response) and X (covariate):
## Editor edit: use `dat` not `data`
dat <- structure(list(Y = c(NA, -1.793, -0.642, 1.189, -0.823, -1.715,
1.623, 0.964, 0.395, -3.736, -0.47, 2.366, 0.634, -0.701, -1.692,
0.155, 2.502, -2.292, 1.967, -2.326, -1.476, 1.464, 1.45, -0.797,
1.27, 2.515, -0.765, 0.261, 0.423, 1.698, -2.734, 0.743, -2.39,
0.365, 2.981, -1.185, -0.57, 2.638, -1.046, 1.931, 4.583, -1.276,
1.075, 2.893, -1.602, 1.801, 2.405, -5.236, 2.214, 1.295, 1.438,
-0.638, 0.716, 1.004, -1.328, -1.759, -1.315, 1.053, 1.958, -2.034,
2.936, -0.078, -0.676, -2.312, -0.404, -4.091, -2.456, 0.984,
-1.648, 0.517, 0.545, -3.406, -2.077, 4.263, -0.352, -1.107,
-2.478, -0.718, 2.622, 1.611, -4.913, -2.117, -1.34, -4.006,
-1.668, -1.934, 0.972, 3.572, -3.332, 1.094, -0.273, 1.078, -0.587,
-1.25, -4.231, -0.439, 1.776, -2.077, 1.892, -1.069, 4.682, 1.665,
1.793, -2.133, 1.651, -0.065, 2.277, 0.792, -3.469, 1.48, 0.958,
-4.68, -2.909, 1.169, -0.941, -1.863, 1.814, -2.082, -3.087,
0.505, -0.013, -0.12, -0.082, -1.944, 1.094, -1.418, -1.273,
0.741, -1.001, -1.945, 1.026, 3.24, 0.131, -0.061, 0.086, 0.35,
0.22, -0.704, 0.466, 8.255, 2.302, 9.819, 5.162, 6.51, -0.275,
1.141, -0.56, -3.324, -8.456, -2.105, -0.666, 1.707, 1.886, -3.018,
0.441, 1.612, 0.774, 5.122, 0.362, -0.903, 5.21, -2.927, -4.572,
1.882, -2.5, -1.449, 2.627, -0.532, -2.279, -1.534, 1.459, -3.975,
1.328, 2.491, -2.221, 0.811, 4.423, -3.55, 2.592, 1.196, -1.529,
-1.222, -0.019, -1.62, 5.356, -1.885, 0.105, -1.366, -1.652,
0.233, 0.523, -1.416, 2.495, 4.35, -0.033, -2.468, 2.623, -0.039,
0.043, -2.015, -4.58, 0.793, -1.938, -1.105, 0.776, -1.953, 0.521,
-1.276, 0.666, -1.919, 1.268, 1.646, 2.413, 1.323, 2.135, 0.435,
3.747, -2.855, 4.021, -3.459, 0.705, -3.018, 0.779, 1.452, 1.523,
-1.938, 2.564, 2.108, 3.832, 1.77, -3.087, -1.902, 0.644, 8.507
), X = c(0.056, 0.053, 0.033, 0.053, 0.062, 0.09, 0.11, 0.124,
0.129, 0.129, 0.133, 0.155, 0.143, 0.155, 0.166, 0.151, 0.144,
0.168, 0.171, 0.162, 0.168, 0.169, 0.117, 0.105, 0.075, 0.057,
0.031, 0.038, 0.034, -0.016, -0.001, -0.031, -0.001, -0.004,
-0.056, -0.016, 0.007, 0.015, -0.016, -0.016, -0.053, -0.059,
-0.054, -0.048, -0.051, -0.052, -0.072, -0.063, 0.02, 0.034,
0.043, 0.084, 0.092, 0.111, 0.131, 0.102, 0.167, 0.162, 0.167,
0.187, 0.165, 0.179, 0.177, 0.192, 0.191, 0.183, 0.179, 0.176,
0.19, 0.188, 0.215, 0.221, 0.203, 0.2, 0.191, 0.188, 0.19, 0.228,
0.195, 0.204, 0.221, 0.218, 0.224, 0.233, 0.23, 0.258, 0.268,
0.291, 0.275, 0.27, 0.276, 0.276, 0.248, 0.228, 0.223, 0.218,
0.169, 0.188, 0.159, 0.156, 0.15, 0.117, 0.088, 0.068, 0.057,
0.035, 0.021, 0.014, -0.005, -0.014, -0.029, -0.043, -0.046,
-0.068, -0.073, -0.042, -0.04, -0.027, -0.018, -0.021, 0.002,
0.002, 0.006, 0.015, 0.022, 0.039, 0.044, 0.055, 0.064, 0.096,
0.093, 0.089, 0.173, 0.203, 0.216, 0.208, 0.225, 0.245, 0.23,
0.218, -0.267, 0.193, -0.013, 0.087, 0.04, 0.012, -0.008, 0.004,
0.01, 0.002, 0.008, 0.006, 0.013, 0.018, 0.019, 0.018, 0.021,
0.024, 0.017, 0.015, -0.005, 0.002, 0.014, 0.021, 0.022, 0.022,
0.02, 0.025, 0.021, 0.027, 0.034, 0.041, 0.04, 0.038, 0.033,
0.034, 0.031, 0.029, 0.029, 0.029, 0.022, 0.021, 0.019, 0.021,
0.016, 0.007, 0.002, 0.011, 0.01, 0.01, 0.003, 0.009, 0.015,
0.018, 0.017, 0.021, 0.021, 0.021, 0.022, 0.023, 0.025, 0.022,
0.022, 0.019, 0.02, 0.023, 0.022, 0.024, 0.022, 0.025, 0.025,
0.022, 0.027, 0.024, 0.016, 0.024, 0.018, 0.024, 0.021, 0.021,
0.021, 0.021, 0.022, 0.016, 0.015, 0.017, -0.017, -0.009, -0.003,
-0.012, -0.009, -0.008, -0.024, -0.023)), .Names = c("Y", "X"
), row.names = c(NA, -234L), class = "data.frame")
With this I run a OLS regression: lm(dat[,1] ~ dat[,2]).
At a set of values: X = quantile(dat[,2], c(0.1, 0.5, 0.7)), I would like to plot a graph similar to the following, with conditional density P(Y|X) displaying along the regression line.
How can I do this in R? Is it even possible?
I call your dataset dat. Don't use data as it masks R function data.
dat <- na.omit(dat) ## retain only complete cases
## use proper formula rather than `$` or `[,]`;
## otherwise you get trouble in prediction with `predict.lm`
fit <- lm(Y ~ X, dat)
## prediction point, as given in your question
xp <- quantile(dat$X, probs = c(0.1, 0.5, 0.7), names = FALSE)
## make prediction and only keep `$fit` and `$se.fit`
pred <- predict.lm(fit, newdata = data.frame(X = xp), se.fit = TRUE)[1:2]
#$fit
# 1 2 3
#0.20456154 0.14319857 0.00678734
#
#$se.fit
# 1 2 3
#0.2205000 0.1789353 0.1819308
To understand the theory behind the following, read Plotting conditional density of prediction after linear regression. Now I am to use mapply function to apply the same computation to multiple points:
## a function to make 101 sample points from conditional density
f <- function (mu, sig) {
x <- seq(mu - 3.2 * sig, mu + 3.2 * sig, length = 101)
dx <- dnorm(x, mu, sig)
cbind(x, dx)
}
## apply `f` to all `xp`
lst <- mapply(f, pred[[1]], pred[[2]], SIMPLIFY = FALSE)
## To plot rotated density curve, we basically want to plot `(dx, x)`
## but scaling `(alpha * dx, x)` is needed for good scaling with regression line
## Also to plot rotated density along the regression line,
## a shift is needed: `(alpha * dx + xp, x)`
## The following function adds rotated, scaled density to a regression line
## a "for-loop" is used for readability, with no loss of efficiency.
## (make sure there is an existing plot; otherwise you get `plot.new` error!!)
addrsd <- function (xp, lst, alpha = 1) {
for (i in 1:length(xp)) {
x0 <- xp[i]; mat <- lst[[i]]
dx. <- alpha * mat[, 2] + x0 ## rescale and shift
x. <- mat[, 1]
lines(dx., x., col = "gray") ## rotate and plot
segments(x0, x.[1], x0, x.[101], col = "gray") ## a local axis
}
}
Now let's see the picture:
## This is one simple way to draw the regression line
## A better way is to generate and grid and predict on the grid
## In later example I will show this
plot(dat$X, fit$fitted, type = "l", ylim = c(-0.6, 1))
## we try `alpha = 0.01`;
## you can also try `alpha = 1` in raw scale to see what it looks like
addrsd(xp, lst, 0.01)
Note, we have only scaled the height of the density, not its span. The span sort of implies confidence band, and should not be scaled. Consider further overlaying confidence band on the plot. If the use of matplot is not clear, read How do I change colours of confidence interval lines when using matlines for prediction plot?.
## A grid is necessary for nice regression plot
X.grid <- seq(min(dat$X), max(dat$X), length = 101)
## 95%-CI based on t-statistic
CI <- predict.lm(fit, newdata = data.frame(X = X.grid), interval = "confidence")
## use `matplot`
matplot(X.grid, CI, type = "l", col = c(1, 2, 2), lty = c(1, 2, 2))
## add rotated, scaled conditional density
addrsd(xp, lst, 0.01)
You see that the span of the density curve agrees with the confidence ribbon.
I am doing Dynamic Time Warping, I have two big data sets: "AIB_nf1" has 128960 observations and "AIB_nf3" has 67956 observations, I have the problem of "memory allocation is not enough", so I uesed "memory.limit(size=200000)" to increase my memory limit, and also I uesed the "bigmemory" package to store the "big vector". Here is my data:
> dput(AIB_nf1[1:90,])
structure(list(V1 = 7:96, V2 = c(0.238, 0.271, 0.305, 0.339,
0.372, 0.406, 0.44, 0.474, 0.508, 0.542, 0.576, 0.609, 0.643,
0.677, 0.711, 0.745, 0.779, 0.812, 0.846, 0.88, 0.914, 0.948,
0.982, 1.016, 1.05, 1.083, 1.118, 1.151, 1.185, 1.219, 1.253,
1.286, 1.32, 1.354, 1.388, 1.422, 1.456, 1.49, 1.523, 1.557,
1.591, 1.626, 1.659, 1.693, 1.726, 1.761, 1.794, 1.828, 1.862,
1.896, 1.929, 1.963, 1.998, 2.031, 2.065, 2.099, 2.134, 2.166,
2.2, 2.234, 2.269, 2.302, 2.336, 2.369, 2.403, 2.437, 2.472,
2.506, 2.539, 2.573, 2.606, 2.64, 2.674, 2.709, 2.742, 2.777,
2.81, 2.844, 2.878, 2.911, 2.946, 2.98, 3.014, 3.047, 3.081,
3.115, 3.149, 3.183, 3.216, 3.25), V3 = c(0.035, 0.033, 0.034,
0.034, 0.033, 0.034, 0.034, 0.034, 0.034, 0.034, 0.034, 0.033,
0.034, 0.034, 0.034, 0.034, 0.034, 0.033, 0.034, 0.034, 0.034,
0.034, 0.034, 0.034, 0.034, 0.033, 0.035, 0.033, 0.034, 0.034,
0.034, 0.033, 0.034, 0.034, 0.034, 0.034, 0.034, 0.034, 0.033,
0.034, 0.034, 0.035, 0.033, 0.034, 0.033, 0.035, 0.033, 0.034,
0.034, 0.034, 0.033, 0.034, 0.035, 0.033, 0.034, 0.034, 0.035,
0.032, 0.034, 0.034, 0.035, 0.033, 0.034, 0.033, 0.034, 0.034,
0.035, 0.034, 0.033, 0.034, 0.033, 0.034, 0.034, 0.035, 0.033,
0.035, 0.033, 0.034, 0.034, 0.033, 0.035, 0.034, 0.034, 0.033,
0.034, 0.034, 0.034, 0.034, 0.033, 0.034), V4 = c(441.100031482115,
441.669796396059, 441.38457234409, 441.09267697965, 440.803668983209,
440.529231728857, 440.278958201691, 440.052100582152, 439.844882580225,
439.647597662433, 439.451667982318, 439.246376810531, 439.031294273645,
438.809393089071, 438.578439895116, 438.336878706118, 438.079094738171,
437.801138836314, 437.501648899307, 437.191385319573, 436.884993592609,
436.598187216878, 436.33876025592, 436.109775709052, 435.909677911583,
435.736569723929, 435.592108168405, 435.476955248205, 435.395588126862,
435.361455383054, 435.384658645204, 435.481047521428, 435.657553442936,
435.924235449177, 436.276622652934, 436.704563920842, 437.201390282161,
437.748420682289, 438.330760278104, 438.934488636071, 439.545855569278,
440.152710252807, 440.732387132145, 441.276668730836, 441.793958476923,
442.321449526307, 442.885082175977, 443.500326940982, 444.163784042471,
444.876747755494, 445.629101153019, 446.413006556444, 447.219702893944,
448.04320465062, 448.878239986395, 449.696324096127, 450.47071643864,
451.192192559764, 451.884392158869, 452.567345063059, 453.255173693378,
453.950945621684, 454.660220896133, 455.383641735162, 456.128123482219,
456.907670434515, 457.71936637997, 458.561564110987, 459.417574075509,
460.277883392994, 461.121178625562, 461.938395364597, 462.720841927243,
463.46713160929, 464.182555341931, 464.861361795048, 465.501874214837,
466.111915734947, 466.705850179264, 467.293066636918, 467.862178274689,
468.390474699237, 468.869842312237, 469.315478639902, 469.74901025705,
470.191756396207, 470.653952147748, 471.154758994025, 471.698672894308,
472.293910737885), V5 = c(314.384059526516, 314.95558512382,
314.921496201275, 314.882092487732, 314.837676590888, 314.790334336513,
314.74286022387, 314.697502713392, 314.65549623984, 314.617675945866,
314.58517227401, 314.558482333973, 314.537472692021, 314.52070437541,
314.505631383973, 314.490211116595, 314.473431803066, 314.455541373727,
314.437668589236, 314.421263791453, 314.407923664018, 314.398966275808,
314.396001516912, 314.3993734291, 314.409000460683, 314.424662660756,
314.446073458842, 314.472208396383, 314.501502439277, 314.531920195551,
314.561611780958, 314.58951544553, 314.614354844383, 314.636411818191,
314.656901885219, 314.678620920816, 314.705179137416, 314.738245314209,
314.777591741161, 314.821970842093, 314.869779210569, 314.919300932594,
314.967408697864, 315.010952427006, 315.047962495732, 315.078951995879,
315.105757641079, 315.129840902302, 315.151785892227, 315.170672617989,
315.185256397008, 315.194170347423, 315.196055633466, 315.190404600552,
315.178723777359, 315.163890579158, 315.149545352313, 315.138261653969,
315.131130401149, 315.127417149137, 315.125308302653, 315.124096433129,
315.12473065196, 315.130605122803, 315.147656465417, 315.18343815854,
315.240429559451, 315.317363629517, 315.408475194359, 315.510337393659,
315.619120511544, 315.734967923778, 315.858240792688, 315.989492765941,
316.128835818992, 316.272243945941, 316.416185332662, 316.559856868433,
316.704771932581, 316.851433299391, 316.99468691065, 317.1274100422,
317.246963764075, 317.357335720734, 317.46468123328, 317.574923437671,
317.690844191521, 317.815705045656, 317.948840448128, 318.089511567484
), V6 = c(77.6544500702095, 79.5967211571173, 81.7960624289552,
84.1373713488114, 86.4154187179779, 88.389872999251, 89.8544806232982,
90.7121101760369, 90.9790614642136, 90.733492208004, 90.036020912385,
88.9146045911228, 87.4062171867472, 85.4553040059864, 83.5311252156856,
82.8657870114023, 82.6654967517532, 79.1149257310998, 71.9920633371955,
63.1528016383671, 53.4027390322975, 43.0500408037574, 32.3287058869458,
21.7032552864132, 13.0436877159348, 10.1579416553081, 15.0294328340723,
24.4751027389957, 35.0114787218733, 45.6609430756035, 56.598944264802,
68.1870637662322, 80.6420267215977, 94.013030263447, 108.179107765857,
122.858492938974, 137.712039733548, 152.531514284118, 167.20468668551,
181.491491962867, 195.111297996157, 207.897226442466, 219.758095918405,
230.666456791146, 240.668240768018, 249.810421699481, 258.129345830109,
265.600556082584, 272.17475257595, 277.857702464358, 282.793981908209,
287.195889937384, 291.288235707612, 295.158722069813, 298.833288762195,
302.308300529554, 305.47121244586, 308.291811952999, 310.952930602306,
313.467005202536, 315.472416516834, 316.598902434977, 316.745389656015,
315.996708714403, 314.344014546762, 311.707893131224, 308.249827382123,
304.066850109038, 299.233863787681, 294.15508247401, 289.41422605264,
285.436033582435, 282.436410040228, 280.552187225324, 279.597475015451,
279.272853057788, 279.365667891074, 279.787799549876, 280.350865174633,
280.751149880548, 280.726173447533, 280.03174151085, 278.348053191143,
275.547010354231, 271.909606813082, 267.872049568937, 263.86533016946,
260.083715283842, 256.347964552832, 252.276751469399), V7 = c(51.4480314095009,
57.5816201042002, 63.6411704294435, 66.2722854148427, 63.6385194727121,
55.1932472167247, 41.698301321069, 25.4354541092531, 8.93261215242534,
-6.274540964104, -19.8698945441127, -32.4185613678941, -44.1128346610455,
-50.5391460721897, -44.1463352989928, -31.916798012143, -47.0585257378509,
-109.194854761563, -187.700602527724, -245.968763437001, -279.862969097574,
-299.595147314433, -307.857736808814, -288.753809825673, -210.9726062505,
-65.1550326835475, 105.831570515081, 234.629898183141, 295.067487408662,
314.462653774103, 325.597268488998, 344.472110431487, 367.167170753237,
390.812993511555, 414.046817816858, 429.820959629612, 432.736947772346,
430.884279682886, 428.818443594366, 416.788105803434, 395.882456531845,
374.435797794311, 353.364425308882, 324.551273875127, 293.514193529575,
267.285618779515, 245.070533423311, 221.357869081285, 195.751625779721,
169.834443929127, 147.065177149355, 130.704379409803, 121.277783326993,
115.216812598445, 109.084392253419, 102.04338350126, 93.1514222125054,
84.3035263789177, 77.4440856638193, 69.4079922090317, 54.2726015042625,
31.7950327396719, 5.37919874081433, -21.8359150720515, -48.6531462458845,
-74.6084628942048, -99.3052157147383, -121.515563578365, -137.731410747763,
-142.218576651789, -133.335682485888, -113.593358480026, -87.0542409157218,
-58.3873364396833, -32.4716986389187, -12.7497949111558, 0.935384462721025,
9.74065532781512, 12.7288079897491, 8.22959665962557, -4.16598901650454,
-24.4148098216698, -51.008299986274, -78.6372761202899, -100.583633052036,
-112.486275167353, -114.944934255289, -113.660470935251, -115.313348108527,
-123.225226010314), V8 = c(176.975697942618, 177.482606302604,
177.929469720604, 178.310222605972, 178.61453336687, 178.835331762098,
178.970316780718, 179.021424834984, 178.995360296074, 178.901209232659,
178.747565020935, 178.542936376789, 178.297708598723, 178.023929456041,
158.586376116383, 72.4771473428539, -69.305876495773, -155.440235518891,
-174.95837309362, -177.007211905103, -158.263287265243, -72.3007854896034,
68.917864425121, 146.405834345958, 135.295461736752, 84.5902300761542,
39.6889156531479, 18.7499723135408, 12.1788596231181, 9.84169632124827,
8.51948006309428, 7.55574681360299, 6.78473380781338, 6.13902515098798,
5.5806484694855, 5.08376972887534, 4.6285263419629, 4.19962291193802,
3.78969606002656, 3.40085313487997, 3.04143775021902, 2.71963085315169,
2.43873130948763, 2.19700449292612, 1.99017489489138, 1.81394321882811,
1.66654182984893, 1.55014560466285, 1.47050003211031, 1.43479761105802,
1.44708400716788, 1.5039935209109, 1.59581348985406, 1.71118754373894,
1.84187151361842, 1.98475655162367, 2.14232863240605, 2.32077818414242,
2.52636607604457, 2.76146309217018, 3.02297458588718, 3.30591380879753,
3.60801537992081, 3.93017147123611, 4.27274460261113, 4.63238312712042,
5.00511718541946, 5.39181904587125, 5.79816267170558, 6.22810768706081,
6.67784925736226, 7.13703862687252, 7.59550627402344, 8.04768077958597,
8.49145777102297, 8.92484345584686, 9.3437760256399, 9.74121348244031,
10.1057619067488, 10.4226016657657, 10.6784707539567, 10.8687985553899,
11.0006871073511, 11.0869580928486, 11.1366309391458, 11.149939652299,
11.1195446827404, 11.0369084026927, 10.899617235996, 10.7156463624773
), V9 = c(1.98619210040585, 2.7560868631792, 3.37520500958573,
3.81497765960184, 4.08976538730253, 4.21602480698687, 4.2118181214032,
4.09752289801817, 3.89504281373373, 3.62559168192297, 3.30711395313635,
2.95501518872097, 2.56879140331794, 2.21294820902311, 2.06712342369418,
2.08649643223339, 1.99190978614415, 1.98463260705346, 1.75511433181296,
1.36824104408505, 1.3899734186647, 1.41621882783257, 1.48435850042031,
1.64655153339521, 1.74570861379073, 1.5196896009812, 1.32976504384076,
1.11690102008759, 1.21332498472059, 1.61473699777163, 1.36396079728927,
1.05025689764031, 1.48782770573704, 2.1745791329998, 2.42336914635558,
2.12403249679967, 1.61058758688998, 1.13732090952412, 0.756350815626496,
0.465691118403319, 0.262158442042785, 0.142886822696197, 0.116665417465433,
0.261120945116082, 0.686186437963084, 1.17631132471452, 1.1723446293262,
0.814788235861579, 1.06237917494463, 1.83194974749923, 2.13536892594869,
1.82583365361083, 1.35807114281152, 1.00266499930436, 0.965724683374411,
0.904884412143251, 1.09330199793613, 1.78024498369561, 1.96548162582243,
1.43214834988696, 1.31383517511711, 1.89856809008893, 2.34559662603644,
2.05496693192545, 1.34182994462245, 0.77295928513312, 0.725191064964074,
1.21631058850099, 1.90904475717069, 2.38372862446284, 2.32977424329766,
1.8141740457693, 1.15454557896414, 0.644768322337396, 0.528137214142275,
0.838785310438783, 1.33792102785776, 1.83052733209556, 2.25705503261945,
2.59640059123711, 2.83671152683692, 2.97758764356467, 3.02955934825042,
3.00914871790722, 2.93094956183559, 2.79939287795865, 2.60933603367312,
2.35150544013667, 2.02008602795494, 1.61944134124658)), .Names = c("V1",
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9"), row.names = c(NA,
90L), class = "data.frame")
My second data set:
> dput(AIB_nf3[1:90,])
structure(list(V1 = 7:96, V2 = c(0.236, 0.27, 0.304, 0.338, 0.372,
0.406, 0.44, 0.473, 0.507, 0.541, 0.575, 0.609, 0.643, 0.677,
0.71, 0.744, 0.778, 0.812, 0.846, 0.88, 0.913, 0.947, 0.981,
1.016, 1.049, 1.083, 1.117, 1.15, 1.184, 1.218, 1.252, 1.286,
1.32, 1.353, 1.387, 1.423, 1.455, 1.489, 1.523, 1.557, 1.59,
1.624, 1.658, 1.692, 1.726, 1.771, 1.794, 1.827, 1.861, 1.895,
1.929, 1.963, 1.997, 2.03, 2.064, 2.098, 2.132, 2.166, 2.2, 2.234,
2.267, 2.301, 2.335, 2.369, 2.403, 2.437, 2.471, 2.504, 2.538,
2.572, 2.606, 2.64, 2.674, 2.707, 2.741, 2.775, 2.809, 2.843,
2.877, 2.911, 2.944, 2.978, 3.012, 3.046, 3.08, 3.114, 3.147,
3.181, 3.215, 3.249), V3 = c(0.033, 0.034, 0.034, 0.034, 0.034,
0.034, 0.034, 0.033, 0.034, 0.034, 0.034, 0.034, 0.034, 0.034,
0.033, 0.034, 0.034, 0.034, 0.034, 0.034, 0.033, 0.034, 0.034,
0.035, 0.033, 0.034, 0.034, 0.033, 0.034, 0.034, 0.034, 0.034,
0.034, 0.033, 0.034, 0.036, 0.032, 0.034, 0.034, 0.034, 0.033,
0.034, 0.034, 0.034, 0.034, 0.045, 0.023, 0.033, 0.034, 0.034,
0.034, 0.034, 0.034, 0.033, 0.034, 0.034, 0.034, 0.034, 0.034,
0.034, 0.033, 0.034, 0.034, 0.034, 0.034, 0.034, 0.034, 0.033,
0.034, 0.034, 0.034, 0.034, 0.034, 0.033, 0.034, 0.034, 0.034,
0.034, 0.034, 0.034, 0.033, 0.034, 0.034, 0.034, 0.034, 0.034,
0.033, 0.034, 0.034, 0.034), V4 = c(244.397061324053, 244.400702120946,
243.925216555288, 243.443071251612, 242.954047234877, 242.46082119539,
241.959057483284, 241.429501231968, 240.863045745703, 240.259352731145,
239.63032746573, 238.99013001204, 238.352537803174, 237.735552453937,
237.150392846983, 236.585236045168, 236.026136509019, 235.456068404747,
234.874569393746, 234.290733373249, 233.727140918233, 233.202025357043,
232.717232961421, 232.270428417261, 231.84982306172, 231.442512212432,
231.038416380226, 230.641080216776, 230.263082097756, 229.918324740988,
229.607916123677, 229.329003437537, 229.069415382373, 228.806483078609,
228.524451292729, 228.207114513139, 227.846524592105, 227.434699094097,
226.985084592902, 226.515081576342, 226.036035597374, 225.552349769204,
225.064485653317, 224.576289037253, 224.097530276654, 223.635615610785,
223.199094889133, 222.782966742833, 222.366446949163, 221.926318957725,
221.454961351109, 220.95682356359, 220.445023295899, 219.924404781116,
219.403389529339, 218.88530175, 218.378256524002, 217.882933067039,
217.397386136205, 216.921817310201, 216.454611391707, 215.984877646023,
215.50876296071, 215.018672519419, 214.51462061468, 213.991720391506,
213.454336911796, 212.907456802085, 212.34957912176, 211.785428890574,
211.217274920029, 210.65034832934, 210.089772589538, 209.550411984895,
209.04869263392, 208.596846002682, 208.192579721103, 207.827692666835,
207.481745734835, 207.129630542572, 206.756514631536, 206.359570313963,
205.951831445474, 205.540582237763, 205.137406238673, 204.739430500441,
204.32752828419, 203.884327082834, 203.40106047782, 202.882839792132
), V5 = c(247.253076275209, 247.666689190985, 247.606310543649,
247.547739991371, 247.492902785217, 247.444192265313, 247.404598605725,
247.376411620011, 247.360554808759, 247.354950990327, 247.355325830061,
247.356755410001, 247.355374526673, 247.349593038983, 247.338336873903,
247.319018763008, 247.290260730096, 247.250866985579, 247.201788518557,
247.145173303549, 247.084612099233, 247.024232691108, 246.96678125726,
246.914326873819, 246.8659987676, 246.818690657025, 246.768022195506,
246.712434325847, 246.6529744292, 246.591936710812, 246.530521080133,
246.469431282949, 246.408215458628, 246.343444800624, 246.27347246623,
246.194302445092, 246.102249186779, 245.993279600228, 245.869830099101,
245.738116061633, 245.604788905026, 245.474912959814, 245.350217384966,
245.231111105075, 245.118352005408, 245.012596405117, 244.915173917605,
244.824620520579, 244.735372154443, 244.642676433661, 244.544802916204,
244.444945691068, 244.348915383423, 244.261960521966, 244.187504259114,
244.125516882205, 244.074409280046, 244.030967641594, 243.991962853134,
243.955729060899, 243.922546964241, 243.894413631016, 243.872980230431,
243.858764444866, 243.851178441494, 243.849077014124, 243.851190085077,
243.855978440196, 243.861740581271, 243.867022735832, 243.871230183108,
243.875240650383, 243.88014773269, 243.886630674707, 243.893315554404,
243.897163678352, 243.89491018853, 243.8852797719, 243.868871498642,
243.847308900227, 243.822535848646, 243.796244966181, 243.770400942538,
243.745875265347, 243.722933276947, 243.700012537055, 243.674107217076,
243.643324695952, 243.607245559772, 243.567282359555), V6 = c(218.506110693017,
219.686373857638, 220.80986670182, 221.979765967016, 223.049625573943,
223.70213147698, 223.600965331256, 222.698922624586, 221.231068678535,
219.515486066369, 217.703777272023, 215.900098465694, 214.096105982454,
212.101345919303, 210.190088668134, 209.351424263804, 209.440061093739,
208.293849202205, 205.794185979436, 203.040358839666, 200.585523299823,
198.627721433822, 196.954240993354, 195.354873413615, 193.720492063926,
191.993088165467, 190.190641299941, 188.440428796291, 186.748992806273,
184.947149867057, 183.244754862898, 182.011642909152, 180.591401665671,
178.76764486427, 177.055973600571, 175.903619723177, 175.561340296572,
176.035641460277, 177.113543362917, 178.461819863897, 179.883966220502,
181.256301356353, 182.579821004423, 184.046734851384, 185.836245524309,
188.015264665033, 190.53240694469, 193.272931235783, 196.118951687063,
198.977703529312, 201.620712970462, 203.783200709525, 205.503499807243,
206.862409476056, 207.942678495566, 208.871691661372, 209.751660010553,
210.55570470269, 211.270010216347, 211.919014206651, 212.081378643191,
211.14828946494, 209.841022365372, 208.653709571572, 207.440015997988,
206.138594054999, 204.719911633923, 203.171843357273, 201.517136845011,
199.855041236237, 198.346557034031, 197.194668936763, 196.545914143282,
196.477310871163, 196.936478376011, 197.735581902005, 198.633396102401,
199.361219205372, 199.686506860322, 199.455352442737, 198.693427589303,
197.583624940463, 196.324745327191, 195.13784171566, 194.171677258684,
193.491615219708, 193.137449958667, 193.025767131504, 193.125820654388,
193.539987389851), V7 = c(54.9644160370263, 40.1189099453233,
34.5740305579775, 32.5016991249785, 27.4483859631039, 15.3581825584586,
-3.23600998306826, -23.0581917053714, -38.6906044118474, -48.1042396554854,
-52.0211361419906, -53.4596609051209, -54.9986722739059, -54.6823098127378,
-45.6489995377139, -28.0493818934812, -20.4761588552912, -37.110965152763,
-61.6414579598598, -72.2722900183545, -68.5213701129446, -60.0822690411778,
-52.4725301468913, -48.7525059733258, -49.0297068513153, -50.8929738161912,
-51.7749526065722, -51.3158910990465, -50.2762034076858, -48.255618974491,
-46.138844689408, -46.9470782604168, -48.1302309707482, -48.9001136517256,
-44.3811929913082, -30.4806629718139, -10.0367025462385, 11.1240027635677,
27.573874708254, 36.7426321791337, 39.9868022114109, 40.2688397753311,
41.0228349262637, 45.3088805326236, 53.7069201811144, 63.8631251779578,
72.5883352337391, 78.6839250020183, 81.7518339571473, 80.743483955464,
74.4584265138868, 63.8832738738687, 52.1572057361882, 41.5118982840146,
33.4609578532465, 28.5247147136518, 25.7271924915013, 23.5875276104449,
20.7582964718266, 13.9218222661232, -0.919329898905578, -19.8607822002949,
-32.0229404811507, -35.4719836309114, -36.6937306645807, -38.9918098252683,
-41.9271678709482, -44.9760359215027, -47.0974850090785, -46.5545111183888,
-41.8458212818634, -32.2506818860908, -18.4345430673698, -2.96728733869023,
10.8302333095278, 20.0013767816711, 22.659933876692, 18.2813737670982,
7.82377573915431, -6.08583752023972, -19.6052893797683, -29.4648039043913,
-33.9117373755672, -32.917811540577, -27.6975776990766, -19.8924127048628,
-11.5004523135903, -3.76863197570263, 3.74207935315723, 11.560497774965
), V8 = c(159.293927504914, 72.3883850945661, -71.0084350953145,
-157.856556857663, -177.00554710696, -178.317037243782, -178.055136408475,
-177.824016066419, -177.6250281113, -177.4514922881, -177.290496619937,
-177.128825377031, -176.959406963087, -176.782453075151, -176.517253859305,
-175.96348820455, -175.190624821963, -174.700809199787, -174.511562908076,
-174.369139733249, -174.177669696731, -173.906156944182, -173.535226055074,
-173.062221173025, -172.506097075525, -171.90103960845, -171.284516587405,
-170.687423185897, -170.130944734713, -169.62716475187, -169.179600433639,
-168.784185781334, -168.432144279701, -168.115430843157, -167.832080030439,
-167.58701995647, -167.388715158312, -167.24512879833, -167.161632904219,
-167.139385723799, -167.175659886351, -167.265507864388, -167.403173794388,
-167.582756548443, -167.799531513718, -168.052339662852, -168.343381722393,
-168.673965944675, -169.042750088636, -169.447235767376, -169.887222743903,
-170.364553504055, -170.879288189246, -171.424542039734, -171.984961344876,
-172.542449289639, -173.083328471416, -173.600386896672, -174.088678181392,
-174.54068213537, -174.94645168287, -175.299463935392, -175.604199733357,
-175.877167460519, -176.138189823576, -176.399552340593, -176.66059159355,
-176.910371463733, -177.134426801918, -177.320531797548, -177.461327627444,
-177.555041112992, -177.606102037448, -177.623466028151, -177.616534625189,
-177.591456067558, -177.550466519513, -177.493975143847, -177.422083688544,
-177.334647887677, -177.231215359635, -177.112776922012, -176.984421932529,
-176.856525882669, -176.744183059778, -176.663265976465, -176.624271509496,
-176.628507345565, -176.670788271004, -176.746236125494), V9 = c(4.92179362638642,
4.45799145175165, 3.3142039265343, 2.2180986760283, 2.19253654220885,
2.75644354895474, 2.87579814794824, 2.56388872662233, 2.20030616765693,
1.88658051271806, 1.61369838922698, 1.36456236302468, 1.1246550001753,
0.859412609329144, 0.574024202847935, 0.536947947334694, 1.1229880448779,
2.03167746641588, 2.45197673151306, 2.22444301866936, 1.97085250007109,
2.02379954624926, 1.87832656594216, 1.65846674309255, 1.71474098773618,
2.01203252409553, 2.39959618941928, 2.69444035331993, 2.70471366054553,
2.42710132196309, 2.2065443976429, 2.37365419463478, 2.60338280076387,
2.57070942196687, 2.40087491789891, 2.10507860447239, 1.93303465881098,
1.96537799291892, 2.07806860623314, 2.23058789058307, 2.42662680623326,
2.6485584531679, 2.79132634856552, 2.68371169097736, 2.24101321753584,
1.5363108532042, 0.856512740292693, 0.598891393913475, 0.84853115782464,
1.36615626094641, 1.94735012797043, 2.34883820489323, 2.22762316220787,
1.58999650565364, 0.997099472422149, 1.05829046320003, 1.71186384166178,
2.23640703246486, 2.06099664143051, 1.39252499562373, 0.770866518925266,
0.589098018487265, 0.897974700574298, 1.42359498039829, 1.95719213635683,
2.42584468730935, 2.72180218117444, 2.7413158008594, 2.56998781213098,
2.38656828529225, 2.28322037883289, 2.27453720291951, 2.35232022279321,
2.50417848456058, 2.69547318585436, 2.82071143328568, 2.74275740403399,
2.45104807156726, 2.03599988832058, 1.55558507367217, 1.04203002189925,
0.617449227876231, 0.527625421413082, 0.883703135286949, 1.47366025502406,
2.07335983162881, 2.53716795522192, 2.69765677114675, 2.51693976755006,
2.19809603906687)), .Names = c("V1", "V2", "V3", "V4", "V5",
"V6", "V7", "V8", "V9"), row.names = c(NA, 90L), class = "data.frame")
First, for dynamic time warping, I need time series data frame, I used "zoo data", v4 and v5 are two features I want to use, V2 is the "Time":
zooData1 <- zoo(AIB_nf1$V4, AIB_nf1$V2)
zooData2<-zoo(AIB_nf1$V5, AIB_nf1$V2)
test<-cbind(zooData1,zooData2)
Then, I used the "big memory" package to handle this "big vector". But first, I need to convert the "zoo data frame" to a matrix that "big memory" package can read:
test<-as.matrix(test)
big1<-as.big.matrix(test)
Then, I repeated the same procedure for my second data frame:
zooData4<-zoo(AIB_nf3$V4, AIB_nf3$V2)
zooData5<-zoo(AIB_nf3$V5, AIB_nf3$V2)
ref<-cbind(zooData4,zooData5)
ref<-as.matrix(ref)
big2<-as.big.matrix(ref)
Then, I used DTW package to calculate the DTW distance between these two data sets:
alignment<-dtw(big1,big2,step.pattern =rabinerJuangStepPattern(1,slope.weighting="c",smoothed=TRUE))
I got the error message:
"Error in as.vector(data) :
no method for coercing this S4 class to a vector"