Splitting a Time Series Data - r

I'm trying to split an earthquake data "A" into a test and a training set.
As I used the code for time series slicing it gave this error.
Error seq.default(1, min(ncol(x) - 1, len), by = 1) : wrong sign in 'by' argumentused .
Thus this the data,
structure(list(Month. = c(9L, 9L, 7L, 3L, 5L, 1L, 6L, 6L, 6L,
2L, 2L, 1L, 10L, 9L, 7L, 6L, 5L, 5L, 4L, 1L), Day. = c(19L, 22L,
24L, 20L, 20L, 7L, 29L, 18L, 16L, 23L, 17L, 26L, 22L, 9L, 17L,
12L, 16L, 11L, 27L, 25L), Mag. = c(7.1, 4.6, 4.5, 5.3, 4.9, 4.1,
4.5, 4, 5.8, 4.1, 4, 4.1, 4.3, 4, 4.1, 4, 4, 4, 4, 4), Lat = c(18.55,
18.02, 18.06, 18.07, 18.16, 18.18, 18.16, 18.06, 18.16, 18.17,
17.91, 18.09, 17.84, 17.82, 18.62, 17.84, 17.93, 17.72, 18.11,
18.08), Lon = c(-98.5, -98.4, -97.2, -98.52, -99.95, -99.72,
-99.15, -100.02, -99.2, -99.37, -98.24, -99.58, -97.7, -99.93,
-99.93, -97.74, -97.75, -99.85, -99.86, -99.98), Depth.km = c(51,
50.9, 69.7, 51.8, 62.7, 45.3, 59.6, 52.7, 52, 38, 49.4, 51.7,
64.8, 65, 75.7, 65.6, 63.7, 37.7, 52.1, 51.8), Day.since.Mw8.1.earthquake = c(11.55922454,
-350.4447106, -410.5616088, -902.2633565, -1207.132025, -1339.944618,
-1531.312454, -1542.510671, -1544.97941, -1657.711794, -1663.206956,
-1685.732604, -1781.218218, -1824.403634, -1878.53603, -1914.132917,
-1940.750532, -1945.763854, -1959.212801, -2052.762095)), row.names = c(NA,
20L), class = "data.frame")
**
I used this code for the time series slicing
I don't know what to do to clear this error. Can someone please help me out?
library(caret)
library(ggplot2)
library(pls)
myTimeControl <- trainControl(method = "timeslice",
initialWindow = 36,
horizon = 12,
fixedWindow = TRUE)
plsFitTime <- train(Day.since.Mw8.1.earthquake ~ Mag.,
data = A,
method = "pls",
preProc = c("center", "scale"),
trControl = myTimeControl)

Related

Error when running GLMM in lme4: Error in pwrssUpdate

I have a dataset containing male and female data. I have the response variable metabolic rate, and several predictors (Behaviour, Temperature 1, Temperature 2, Activity, Sex, Body Size, and Body Mass).
First, I conduct the GLMM using an identity link on male and female combined data:
glmer(log(Metabolic_Rate)~ Temperature.1 + Behaviour * Temperature.2 + Sex + Activity + Body_Size + Body_Mass + (1|Week), data= AMRdata, family = Gamma(link = 'identity'))
And then run model simplification. The model works just fine and I have no error messages.
Then, I separate the data by sex and run the model on just female data:
females<-subset(AMRdata,Sex=="F")
glmer(log(Metabolic_Rate)~ Temperature.1 + Behaviour * Temperature.2 + Activity + Body_Size + Body_Mass + (1|Week), data = females, family = Gamma(link = 'identity'))
I am greeted by the error message:
Error in pwrssUpdate(pp, resp, tol = tolPwrss, GQmat = GQmat, compDev = compDev, :
(maxstephalfit) PIRLS step-halvings failed to reduce deviance in pwrssUpdate
Removal of Temperature.2 takes removes this error, but I really need to keep it in the model.
Does anyone have any suggestions as to how to remove the error?
I'm using lme4 version 1.1.21, and the female model doesn't work if I don't use the log transformation, either. I have tried using a log link, but I get the same error message.
Some data:
structure(list(Metabolic_Rate = c(8.79514591, 16.71840387, 14.1932374,
10.90741585, 10.7436911, 14.97469781, 19.88267242, 12.43274774,
15.12038794, 11.84916117, 11.05467852, 19.53495917, 12.14440531,
12.09564168, 6.78392472, 10.51570692, 8.527792046, 8.731880804,
10.71404367), Behaviour = c(23L, 17L, 14L, 7L, 99L, 78L, 90L,
1L, 9L, 29L, 76L, 66L, 43L, 36L, 13L, 4L, 82L, 14L, 59L), Temperature.1 = c(21.9,
21.7, 18.52, 19.85, 20.45, 20.54, 21.7, 22, 21.32, 21.4, 21.44,
22.1, 22.22, 22.25, 20.43, 20.9, 21.63, 21.2, 21.52), Temperature.2 = c(17.5,
15.6, 12.5, 19.8, 16.6, 20.8, 21.4, 21.9, 21, 21.3, 20.5, 22,
22.1, 22.2, 20.6, 21.2, 21.9, 21.1, 21.5), Activity = c(39.54664352,
66.75914352, 40.85949074, 44.8505787, 37.20023148, 69.75388889,
72.43981481, 70.42199074, 20.71481481, 77.27662037, 62.21712963,
93.22673611, 82.39247685, 89.42141204, 35.35729167, 31.97777778,
74.65821759, 40.80590278, 54.3755787), Sex = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("F", "M"), class = "factor"), Body_Size = c(7.6,
5.8, 7.9, 7.6, 8, 7.5, 7.9, 7.6, 7.2, 7.8, 7.8, 7.4, 7.1, 8.4,
6, 7.9, 7.2, 7, 8.2), Body_Mass = c(0.747, 0.55, 0.76, 0.673,
0.691, 0.683, 0.689, 0.789, 0.6, 0.612, 0.637, 0.511, 0.582,
0.603, 0.408, 0.527, 0.666, 0.483, 0.602), Week = c(1L, 1L, 2L,
3L, 3L, 3L, 3L, 4L, 5L, 6L, 6L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 10L
)), class = "data.frame", row.names = c(NA, -19L))

2d contour color map in ggplot2

I have a huge data, please bear with me.
df <- structure(list(W = c(5216400.4123, 5399804.7349, 5595563.3087,
5792353.9932, 5993467.7466, 6189404.9279, 6380940.454, 6566630.3544,
6747453.6816, 6917820.9796, 7086201.8275, 7248213.5225, 5402700.4252,
5592654.9057, 5795404.8549, 5999223.7818, 6207520.1695, 6410455.1037,
6608831.1825, 6801152.8706, 6988434.1695, 7164886.0132, 7339280.46,
7507078.2886, 5589000.4397, 5785505.0748, 5995246.3993, 6206093.5662,
6421572.589, 6631505.2817, 6836721.9131, 7035675.3828, 7229414.6578,
7411951.0459, 7592359.0997, 7765943.0584, 5775300.4552, 5978355.2455,
6195087.9484, 6412963.3504, 6635625.0103, 6852555.4544, 7064612.6447,
7270197.897, 7470395.1463, 7659016.0859, 7845437.7356, 8024807.8293,
5961600.4694, 6171205.4145, 6394929.4952, 6619833.1358, 6849677.4283,
7073605.633, 7292503.372, 7504720.4062, 7711375.6354, 7906081.1161,
8098516.3724, 8283672.5987, 6147900.4816, 6364055.583, 6594771.0412,
6826702.9212, 7063729.8478, 7294655.8114, 7520394.1023, 7739242.9195,
7952356.1253, 8153146.1541, 8351595.0092, 8542537.3639, 6334200.5005,
6556905.7553, 6794612.5881, 7033572.7089, 7277782.2661, 7515705.9882,
7748284.8337, 7973765.4322, 8193336.6123, 8400211.1873, 8604673.6477,
8801402.1321, 6520500.5136, 6749755.9226, 6994454.1327, 7240442.494,
7491834.6868, 7736756.1617, 7976175.5621, 8208287.9459, 8434317.1037,
8647276.2228, 8857752.2827, 9060266.9029, 6706800.528, 6942606.0899,
7194295.6809, 7447312.2777, 7705887.1079, 7957806.3409, 8204066.2961,
8442810.4584, 8675297.5886, 8894341.2573, 9110830.9206, 9319131.6718,
6893100.5418, 7135456.2602, 7394137.2288, 7654182.0653, 7919939.5232,
8178856.5136, 8431957.0234, 8677332.969, 8916278.0804, 9141406.2918,
9363909.5543, 9577996.4404, 7079400.5588, 7328306.4296, 7593978.7729,
7861051.8503, 8133991.9462, 8399906.6912, 8659847.7579, 8911855.4821,
9157258.5684, 9388471.3277, 9616988.1903, 9836861.209, 7265700.5699,
7521156.5994, 7793820.3185, 8067921.639, 8348044.3652, 8620956.868,
8887738.4844, 9146377.9962, 9398239.0552, 9635536.362, 9870066.8286,
10095725.9764, 7377480.5806, 7636866.7018, 7913725.2471, 8192043.5082,
8476475.8166, 8753586.9712, 9024472.9255, 9287091.5011, 9542827.3494,
9783775.3823, 10021914.0099, 10251044.8357, 7452000.5846, 7714006.7682,
7993661.8669, 8274791.4235, 8562096.7846, 8842007.041, 9115629.2162,
9380900.5049, 9639219.5459, 9882601.398, 10123145.4642, 10354590.7474,
7526520.5952, 7791146.8356, 8073598.4841, 8357539.3348, 8647717.7534,
8930427.113, 9206785.5068, 9474709.5143, 9735611.7438, 9981427.4094,
10224376.9194, 10458136.6527, 7638300.6025, 7906856.9365, 8193503.4143,
8481661.2074, 8776149.2063, 9063057.2172, 9343519.9463, 9615423.0209,
9880200.0383, 10129666.4312, 10376224.1032, 10613455.5131, 7824600.6172,
8099707.1074, 8393344.9601, 8688530.993, 8990201.627, 9284107.395,
9571410.6772, 9849945.5329, 10121180.5245, 10376731.4677, 10629302.7393,
10872320.2844, 8010900.6306, 8292557.2766, 8593186.5077, 8895400.7788,
9204254.0428, 9505157.5709, 9799301.4089, 10084468.0455, 10362161.0109,
10623796.504, 10882381.3729, 11131185.0534, 8197200.6444, 8485407.4453,
8793028.0567, 9102270.5614, 9418306.465, 9726207.7477, 10027192.1371,
10318990.5604, 10603141.4991, 10870861.5368, 11135460.012, 11390049.8203,
8383500.6593, 8678257.6128, 8992869.6005, 9309140.3471, 9632358.8856,
9947257.9225, 10255082.8657, 10553513.0731, 10844121.988, 11117926.5692,
11388538.6484, 11648914.5899, 8569800.6735, 8871107.7799, 9192711.1487,
9516010.1349, 9846411.3046, 10168308.0987, 10482973.5998, 10788035.5824,
11085102.4738, 11364991.6051, 11641617.2821, 11907779.3576, 8756100.6891,
9063957.9534, 9392552.6936, 9722879.9204, 10060463.7195, 10389358.2746,
10710864.3279, 11022558.0956, 11326082.9668, 11612056.6477, 11894695.9209,
12166644.1258, 8942400.705, 9256808.1204, 9592394.2396, 9929749.7037,
10274516.1413, 10610408.4494, 10938755.0613, 11257080.6099, 11567063.455,
11859121.6771, 12147774.5594, 12425508.8956, 9128700.7203, 9449658.2901,
9792235.786, 10136619.4908, 10488568.5635, 10831458.6249, 11166645.792,
11491603.1215, 11808043.9427, 12106186.7106, 12400853.1962, 12684373.664,
9315000.7353, 9642508.4595, 9992077.3349, 10343489.2758, 10702620.9802,
11052508.7998, 11394536.5185, 11726125.6341, 12049024.431, 12353251.7462,
12653931.8307, 12943238.4324, 9501300.7465, 9835358.6307, 10191918.8807,
10550359.0623, 10916673.4022, 11273558.9758, 11622427.2509, 11960648.1479,
12290004.92, 12600316.7835, 12907010.4688, 13202103.2002, 9687600.7612,
10028208.7979, 10391760.4269, 10757228.8467, 11130725.8198, 11494609.1532,
11850317.9787, 12195170.6632, 12530985.4111, 12847381.8161, 13160089.1051,
13460967.9711), t = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L), .Label = c("t_m75_tc", "t_m5_tc", "t_m25_tc", "t_p0_tc",
"t_p25_tc", "t_p5_tc", "t_p75_tc", "t_p10_tc", "t_p125_tc", "t_p15_tc",
"t_p175_tc", "t_p20_tc"), class = "factor"), p = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 25L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L), .Label = c("h_30", "h_27.5", "h_25", "h_22.5",
"h_20", "h_17.5", "h_15", "h_12.5", "h_10", "h_7.5", "h_5", "h_2.5",
"h_1", "h0", "h1", "h2.5", "h5", "h7.5", "h10", "h12.5", "h15",
"h17.5", "h20", "h22.5", "h25", "h27.5", "h30"), class = "factor"),
tt = c(-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20,
-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5,
-5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5,
-2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5,
0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0,
2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5,
5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5,
7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5,
10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10,
12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5,
15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15,
17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20,
-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5,
-5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5,
-2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5,
0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0,
2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5,
5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5,
7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5,
10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10,
12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5,
15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15,
17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20), hh = c(-30, -30, -30, -30, -30, -30, -30, -30, -30,
-30, -30, -30, -27.5, -27.5, -27.5, -27.5, -27.5, -27.5,
-27.5, -27.5, -27.5, -27.5, -27.5, -27.5, -25, -25, -25,
-25, -25, -25, -25, -25, -25, -25, -25, -25, -22.5, -22.5,
-22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5,
-22.5, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20,
-20, -20, -17.5, -17.5, -17.5, -17.5, -17.5, -17.5, -17.5,
-17.5, -17.5, -17.5, -17.5, -17.5, -15, -15, -15, -15, -15,
-15, -15, -15, -15, -15, -15, -15, -12.5, -12.5, -12.5, -12.5,
-12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -10,
-10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -7.5,
-7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5,
-7.5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -2.5,
-2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5,
-2.5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5,
2.5, 2.5, 2.5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7.5, 7.5,
7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 12.5, 12.5, 12.5,
12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 17.5, 17.5, 17.5,
17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 22.5, 22.5, 22.5,
22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 27.5, 27.5, 27.5,
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), pChange = c(-36.9603395985828,
-34.7439172960321, -32.3781951432773, -30.0000000392759,
-27.5695610939649, -25.2016804880133, -22.8869934306931,
-20.6429501564101, -18.4577189167867, -16.3988477104845,
-14.3639825485445, -12.4060879418008, -34.7089231777299,
-32.4133428932464, -29.9631306906258, -27.5000000028702,
-24.982759663634, -22.5303119363876, -20.1329575059591, -17.8087697620382,
-15.5454946011909, -13.4130922883195, -11.3055534045631,
-9.27773397066822, -32.4575067375413, -30.082768511005, -27.5480662597272,
-25.000000017221, -22.3959582743917, -19.8589433581751, -17.3789215558468,
-14.974589416006, -12.6332702807612, -10.4273368770308, -8.24712417357041,
-6.14937995482153, -30.2060902852677, -27.7521941094277,
-25.1330017720295, -22.5000000339888, -19.8091568633966,
-17.1875748440126, -14.6248855936495, -12.1404090458039,
-9.72104595791446, -7.44158137752243, -5.18869498850033,
-3.02102592568144, -27.9546738487045, -25.4216197283948,
-22.7179373121271, -20.0000000362547, -17.2223554922816,
-14.5162062585492, -11.8708496834174, -9.30622873602633,
-6.80882162781682, -4.45582599644603, -2.13026579255384,
0.107328085331291, -25.7032574363111, -23.0910453534044,
-20.3028728618926, -17.5000000385206, -14.6355541030393,
-11.8448376755028, -9.11681373693055, -6.47204837670069,
-3.89659728805126, -1.47007052110743, 0.928163403392649,
3.23568204558746, -23.4518409429489, -20.7604709324913, -17.8878084007817,
-15.0000000129913, -12.0487527282989, -9.17346911179217,
-6.36277777715033, -3.63786802462598, -0.984372983331903,
1.51568489622367, 3.98659261988347, 6.36403604209831, -21.2004245196791,
-18.4298965720027, -15.4727439674661, -12.5000000188826,
-9.46195132455473, -6.50210058796172, -3.6087418536248, -0.803687660466381,
1.92785137456101, 4.50144034135001, 7.04502179407714, 9.4923900700299,
-18.949008080699, -16.0993222115141, -13.0576794906449, -10.0000000416929,
-6.8751499159766, -3.83073199524737, -0.85470586242384, 2.03049268919135,
4.84007565390208, 7.48719577439149, 10.103451003317, 12.6207440750002,
-16.6975916489697, -13.7687478147708, -10.6426150174491,
-7.50000001737204, -4.28834857749088, -1.15936348108484,
1.8993300478083, 4.86467301588778, 7.75230001662893, 10.4729512074329,
13.1618801618003, 15.749098076345, -14.4461751785688, -11.4381734289039,
-8.2275505901759, -5.00000002447191, -1.70154714595145, 1.51200509229367,
4.65336604505171, 7.69885337279645, 10.6645243334332, 13.4587066573933,
16.2203093480789, 18.8774520776898, -12.1947587794688, -9.10759903820312,
-5.81248614477539, -2.49999998685766, 0.885254237248392,
4.18337365600429, 7.40740194561595, 10.53303374179, 13.5767486357356,
16.4444620880177, 19.2787385621527, 22.0058060645328, -10.8439088911858,
-7.70925439749847, -4.3634474625498, -1.00000001287041, 2.43733506716831,
5.78619475942614, 9.05982354879595, 12.2335419201639, 15.324083242737,
18.2359153430087, 21.1137960702944, 23.8828184428618, -9.94334233686319,
-6.77702465958718, -3.39742166553716, 0, 3.47205562528221,
6.85474217379226, 10.1614379102301, 13.3672140455251, 16.4889729851691,
19.4302175391865, 22.3371677435973, 25.1341600948813, -9.04277570278022,
-5.84479490959099, -2.43139589994524, 0.999999964530837,
4.50677619306401, 7.92328961474518, 11.2630522704558, 14.5008862385618,
17.65386274452, 20.6245196833994, 23.5605394277767, 26.3855016695576,
-7.69192585558588, -4.44645028701369, -0.982357198383821,
2.49999997960674, 6.05885704111126, 9.52611073025193, 12.9154738543,
16.20139443748, 19.4011973551469, 22.4159729565176, 25.3955969661306,
28.26251406118, -5.44050941298024, -2.11587588301946, 1.43270724943368,
4.99999997975781, 8.64565844485543, 12.1974793060474, 15.6695098080378,
19.0355747810953, 22.3134216501983, 25.4017284137289, 28.4540261536177,
31.390868095154, -3.18909298608498, 0.214698500430437, 3.84777171900399,
7.49999998232584, 11.2324597893836, 14.8688478588816, 18.4235457714435,
21.8697551319615, 25.2256459476667, 28.3874838685232, 31.5124553108925,
34.5192221013328, -0.93767655435575, 2.54527287783788, 6.26283620549316,
9.99999994622222, 13.8192612112551, 17.5402164225922, 21.177581692552,
24.703935510623, 28.1378702668879, 31.3732392810203, 34.5708845346342,
37.6475760821333, 1.31373989066688, 4.87584724074345, 8.67790062914087,
12.4999999475818, 16.4060626137908, 20.211584962133, 23.9316176184945,
27.5381158626977, 31.0500945945686, 34.3589946886835, 37.6293137257467,
40.775930095563, 3.56515632723005, 7.20642159881506, 11.0929651059621,
14.9999999743196, 18.9928639969906, 22.8829535185927, 26.685653610904,
30.3722961736837, 33.9623188847861, 37.3447501386438, 40.6877428842301,
43.9042840860314, 5.81657278071209, 9.53699603423, 13.5080295429032,
17.4999999732622, 21.5796653306424, 25.5543220714269, 29.439689530804,
33.2064765318009, 36.8745432620148, 40.3305056695729, 43.7461721043463,
47.0326380825422, 8.06798923781962, 11.8675703910931, 15.9230939931377,
19.999999945618, 24.16646674768, 28.2256906109677, 32.1937255147541,
36.0406569032114, 39.7867675812361, 43.3162610409814, 46.8046013208372,
50.1609920983889, 10.3194056876762, 14.1981447805855, 18.3381584482061,
22.4999999638964, 26.7532681695515, 30.8970591589679, 34.9477614660749,
38.8748372419928, 42.6989918944149, 46.302016461938, 49.8630305167836,
53.2893460973168, 12.5708221339073, 16.5287191664524, 20.7532229334868,
24.9999999567965, 29.3400695249561, 33.5684276997172, 37.7017973666391,
41.709017592859, 45.6112162148446, 49.2877719082728, 52.9214596849349,
56.4177000962446, 14.8222385342158, 18.859293574072, 23.1682873813043,
27.499999967824, 31.9268709444106, 36.2397962537599, 40.4558333385042,
44.5431979582271, 48.5234405437337, 52.273527375152, 55.9798888965917,
59.5460540879215, 17.0736549768214, 21.1898679333522, 25.5833518339557,
29.9999999534732, 34.5136723106916, 38.9111648247215, 43.2098692547788,
47.3773783417225, 51.4356648980012, 55.2592827852321, 59.0383180864958,
62.6744081170616)), class = "data.frame", .Names = c("W",
"t", "p", "tt", "hh", "pChange"), row.names = c(NA, -324L))
I am trying to plot a colored contour map with the columns tt, hh and W, W being the z axis.
This is the code I am using::
ggplot(df, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
This is the result I got:
What I want is the color to be continuous, as seen here I am following this example for my work.
What is going wrong over here?
The grid is not evenly spaced. One way to make an evenly spaced grid is to use interpolate using loess on an evenly spaced grid:
model <- loess(W ~ tt + hh, data = df)
create an evenly spaced grid using expand.grid:
new.data <- expand.grid(tt = seq(from = min(df$tt), to = max(df$tt), length.out = 500),
hh = seq(from = min(df$hh), to = max(df$hh), length.out = 500))
predict on new data using the model:
gg <- predict(model, newdata = new.data)
combine prediction and new data:
new.data = data.frame(W = as.vector(gg),
new.data)
and now the plot looks like:
ggplot(new.data, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
You might also want to check some goodness of fit metric for loess
caret::RMSE(model$fitted, df$W)
#output
7498.393
using a narrower span could provide a better fit, especially if the data is not smooth:
model2 <- loess(W ~ tt + hh, data = df, span = 0.1)
caret::RMSE(model2$fitted, df$W)
#output
964.7582
ggplot(new.data2, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
The difference is ever so slight
ggplot(new.data, aes(x = tt, y = hh, z = W)) +
geom_tile(aes(fill = W)) +
geom_contour(aes(x = tt, y = hh, z = W),
color = "red")+
geom_contour(data = new.data2,
aes(x = tt, y = hh, z = W),
color = "white", inherit.aes = FALSE)
EDIT: also check the great post by #Henrik which is linked by him in the comment. Especially the ?akima::interp function.
EDIT2: answer to the questions in comments:
To specify a different fill one can use
scale_fill_gradient
scale_fill_gradient2
scale_fill_gradientn
Here is an example of using scale_fill_gradientn with 5 colors based on quantiles:
v <- ggplot(new.data2, aes(x = tt, y = hh, z = floor(W))) +
geom_tile(aes(fill = W), show.legend = FALSE) +
stat_contour(bins = 10, aes(colour = ..level..)) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W")) +
scale_fill_gradientn(values = scales::rescale(quantile(new.data2$W)),
colors = rainbow(5))
I removed the polygon thing since it was below the geom_tile layer and was not visible.
To add direct labels:
library(directlabels)
direct.label(v, list("far.from.others.borders", "calc.boxes", "enlarge.box",
box.color = NA, fill = "transparent", "draw.rects"))

Aesthetic Mapping Temperature Data in ggplot2

I am very new to r and I think I am just missing something very simple. At least I hope. I am trying to plot temperature change by site using aes in ggplot2. However, when I run the code no data is pulling up in the chart. This is the code I am using.
My data is called diss, x=order, and y=Tempdiff.
tempplot<-ggplot(data = diss,mapping =
aes(order,Tempdiff))+geom_segment(data = diss , mapping =
aes(x=order,y=Tempdiff, xend=order, yend=Tempdiff, color="red"))
plot(tempplot)
There are no errors when I run the code and the graph pulls up, it is just empty. Is there something in my code that I am unaware of that is removing the data or have I just missed a critical code in actually adding the data?
If I have left something out that is necessary for answering this question I am happy to add more. Thank you in advance!
Addition:
> dput(diss)
structure(list(Paper = c(1L, 2L, 3L, 4L, 5L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 10L, 10L, 10L), Specimen = structure(c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 2L,
2L, 4L, 4L, 4L), .Label = c("", "Ochotona_collaris", "Ochotona_curzoniae",
"Ochotona_principes"), class = "factor"), Site = structure(c(15L,
16L, 6L, 10L, 3L, 7L, 14L, 18L, 8L, 17L, 17L, 17L, 17L, 11L,
2L, 13L, 9L, 9L, 4L, 12L, 5L), .Label = c("", "Arapaho_Basin",
"Bodi ", "Bouldera_Pass", "Calf_Robe", "Columbia_River_Gorge",
"Craters_of_Moon", "Grand_Teton ", "Kluane_Lake", "Lava_Butte Flow_OR",
"Loveland_Pass", "Mad_Wolf", "Niwat_Ridge", "Rocky_Mt", "South_Couliee",
"Sugar_Hill_Modoc", "Tibet_China", "Yellowstone"), class = "factor"),
elevation = c(2565L, 2203L, 316L, 1200L, 2600L, 1700L, 3462L,
2424L, 2815L, 3846L, 3846L, 3846L, 3846L, 3654L, 3878L, 3744L,
2000L, 2000L, 2133L, 2026L, 2004L), yr1 = c(2012L, 1894L,
2011L, 2011L, 1972L, 2010L, 2010L, 2010L, 2010L, 2005L, 2006L,
2007L, 2008L, 1981L, 1981L, 1981L, 1999L, 2003L, 2007L, 2008L,
2008L), yr1pop = c(9L, 4L, 9L, 9L, 48L, 55L, 39L, 71L, 54L,
182L, 219L, 223L, 231L, 6L, 4L, 10L, 5L, 3L, 2L, 3L, 7L),
yr1temp = c(25, 30, 32.1, 20, 20, 27.1, 15.4, 21.2, 18, 11,
13, 14, 12.5, 24.7, 26.5, 28.4, 12, 17, 20, 20, 20), yr2 = c(2013L,
1910L, 2012L, 2012L, 2006L, 2011L, 2011L, 2011L, 2011L, 2006L,
2007L, 2008L, 2009L, 1983L, 1983L, 1983L, 2002L, 2006L, 2009L,
2009L, 2009L), yr2temp = c(26, 31.4, 33.2, 17.1, 24.76, 30.7,
18.2, 22, 20, 13, 14, 12.5, 13, 26.1, 27.3, 28.3, 15, 16,
20, 20, 10), yr2pop = c(9L, 2L, 9L, 7L, 4L, 40L, 20L, 37L,
37L, 219L, 223L, 231L, 214L, 5L, 4L, 8L, 4L, 2L, 2L, 7L,
2L), delta_temp = c(1, 1.4, 1.1, 2.9, 4.76, 3.6, 2.8, 0.8,
2, 2, 1, 1.5, 0.5, 1.4, 0.8, 0.1, 3, 1, 0, 0, 10), avg_temp = c(25.5,
30.7, 32.65, 18.55, 44.76, 28.9, 16.8, 21.6, 19, 12, 13.5,
13.25, 12.75, 25.4, 26.9, 28.35, 13, 16.5, 20, 20, 15), delta_pop = c(0L,
2L, 0L, 2L, 44L, 15L, 19L, 34L, 17L, 37L, 4L, 8L, 17L, 1L,
0L, 2L, 1L, 1L, 0L, 4L, 5L), avg_pop = c(9, 3, 9, 8, 26,
47.5, 29.5, 54, 45.5, 200.5, 221, 227, 222.5, 5.5, 4, 9,
4.5, 3.5, 2, 4, 4.5), SE_temp = c(0.499, 0.7, 0.6, 1.45,
2.381, 1.8, 1.4, 0.4, 1, 1.414, 0.707, 1.06, 0.353, 0.989,
0.565, 0.07, 2.121, 0.707, 0, 0, 7.07), CI_low_temp = c(25.025,
30.035, 32.03, 17.172, 20.119, 27.19, 15.47, 21.22, 18.05,
11.05, 13.025, 12.537, 12.512, 24.735, 26.52, 28.302, 12.075,
16.025, 20, 20, 10.25), CI_upper_temp = c(25.975, 31.365,
33.17, 19.927, 24.641, 30.61, 18.13, 21.98, 19.95, 12.95,
13.974, 13.962, 12.987, 26.065, 27.78, 28.397, 14.925, 16.975,
20, 20, 19.75), SE_pop = c(0, 1.414, 0, 1.414, 31.112, 10.606,
13.425, 24.041, 12.02, 26.162, 2.828, 5.656, 12.02, 0.707,
0, 1.414, 0.707, 0.707, 0, 2.828, 3.535), CI_pop_low = c(9,
2.05, 9, 7.05, 5.1, 40.375, 20.475, 37.85, 37.425, 182.925,
219.1, 223.2, 214.425, 5.025, 4, 8.05, 4.025, 2.025, 2, 3.1,
2.125), CI_pop_upper = c(9, 3.95, 9, 8.95, 46.9, 54.625,
38.525, 70.15, 53.575, 218.075, 222.9, 230.8, 230.575, 5.975,
4, 9.95, 4.975, 2.975, 2, 6.9, 6.875), Tempdiff = c(1, 1.4,
1.1, -2.9, 4.76, 3.6, 2.8, 0.800000000000001, 2, 2, 1, -1.5,
0.5, 1.4, 0.800000000000001, -0.0999999999999979, 3, -1,
0, 0, -10), popdiff = c(0L, -2L, 0L, -2L, -44L, -15L, -19L,
-34L, -17L, 37L, 4L, 8L, -17L, -1L, 0L, -2L, -1L, -1L, 0L,
4L, -5L), order = 1:21), .Names = c("Paper", "Specimen",
"Site", "elevation", "yr1", "yr1pop", "yr1temp", "yr2", "yr2temp",
"yr2pop", "delta_temp", "avg_temp", "delta_pop", "avg_pop", "SE_temp",
"CI_low_temp", "CI_upper_temp", "SE_pop", "CI_pop_low", "CI_pop_upper",
"Tempdiff", "popdiff", "order"), row.names = c(NA, 21L), class = "data.frame")

R 3d plot error with intersecting planes

This has stumped me all day long. Can anyone help me to understand why my data is providing me these plots, which contain intersecting planes alongside of my 3D-scatterplot? The planes occur using all 3 of the methods below.
There may be an issue in my use of unlist of the matrix that created the attached dataset.
Link
Source Data
open3d()
bg3d(color="white")
points3d(freq_subdata$x_ft,freq_subdata$y_ft,freq_subdata$count)
plot3d(freq_subdata$x_ft,freq_subdata$y_ft,freq_subdata$count)
rgl.open()
rgl.bg(color="white")
rgl.points(freq_subdata$x_ft,freq_subdata$y_ft,freq_subdata$count,color="black")
> dput(head(freq_subdata,20))
structure(list(x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), y = 1:20, x_ft = c(2.5,
2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5,
2.5, 2.5, 2.5, 2.5, 2.5, 2.5), y_ft = c(2.5, 7.5, 12.5, 17.5,
22.5, 27.5, 32.5, 37.5, 42.5, 47.5, 52.5, 57.5, 62.5, 67.5, 72.5,
77.5, 82.5, 87.5, 92.5, 97.5), count = c(2L, 4L, 6L, 8L, 10L,
12L, 14L, 16L, 18L, 20L, 22L, 24L, 26L, 28L, 30L, 32L, 34L, 36L,
38L, 40L), proportion = c(6.18888476296571e-05, 0.000123777695259314,
0.000185666542888971, 0.000247555390518629, 0.000309444238148286,
0.000371333085777943, 0.0004332219334076, 0.000495110781037257,
0.000556999628666914, 0.000618888476296571, 0.000680777323926229,
0.000742666171555886, 0.000804555019185543, 0.0008664438668152,
0.000928332714444857, 0.000990221562074514, 0.00105211040970417,
0.00111399925733383, 0.00117588810496349, 0.00123777695259314
)), .Names = c("x", "y", "x_ft", "y_ft", "count", "proportion"
), row.names = c("Var11", "Var12", "Var13", "Var14", "Var15",
"Var16", "Var17", "Var18", "Var19", "Var110", "Var111", "Var112",
"Var113", "Var114", "Var115", "Var116", "Var117", "Var118", "Var119",
"Var120"), class = "data.frame")
Link
Source Data

Adjusting legend and colors in ggplot2

I am using following data:
testdf = structure(list(var1 = c(14.9, 15.5, 16.5, 16.6, 15.1, 13.8, 13.2,
27.6, 22.3, 29.1, 18.4, 14.8, 15.7, 14.3, 15.5, 15.8, 17.6, 14.9,
16.9, 20.8, 13.9, 20.1, 16.9, 24.7, 15.2, 15.9, 15.8, 17.1, 15.9,
17.3, 17.5, 14.7, 21, 12, 18.6, 16.1, 16.1, 15.8, 15.9, 13.9,
13.6, 13.6, 14.2, 13.9, 14.1, 13.9, 13.7, 13.6, 13.9, 13.2),
age = c(7L, 7L, 8L, 10L, 7L, 11L, 9L, 14L, 12L, 15L, 10L,
12L, 12L, 9L, 9L, 10L, 15L, 10L, 12L, 14L, 15L, 13L, 15L,
13L, 11L, 9L, 14L, 12L, 12L, 15L, 13L, 12L, 15L, 7L, 14L,
8L, 10L, 8L, 9L, 9L, 8L, 10L, 9L, 9L, 11L, 10L, 10L, 9L,
9L, 9L)), .Names = c("var1", "age"), row.names = c(NA, 50L
), class = "data.frame")
I can have a histogram with following code:
ggplot(testdf)+geom_histogram(aes(var1,group=age,color=age,fill=age))
But how can I get ages 7,8,9,10,11,12,13,14,15 in legend and different colours for all these age groups, eg rainbow(9)
I tried following codes but they work only partially:
ggplot(testdf)+geom_histogram(aes(var1,group=age,color=age,fill=age))+scale_colour_continuous(breaks=c(7:15),color=rainbow(9))
ggplot(testdf)+geom_histogram(aes(var1,group=age,color=age,fill=age, legend=F))+scale_colour_continuous(breaks=c(7:15))
As #nrussel said, you have to convert age to a factor variable. You can do that within ggplot2. Moreover, you don't really need the group and colour parameters in this case.
With:
ggplot(testdf)+
geom_histogram(aes(var1, fill=as.factor(age)))
you should get the following result:

Resources