R levelplot and interpolation [closed] - r

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
some days ago I managed to make levelplots, with interpolation, with the following command within a script:
levelplot(jan~lon*lat,APM,main="Jan",panel=panel.levelplot.raster,interpolate=T)
I accidentaly did not keep that session, only the script, so it is not possible for me to go through the history and recover all the commands I had used.
Now, after loading latticeExtra, the same command produces an empty plot. On the other hand, leaving out the last two elements of the instruction, i.e.:
levelplot(jan~lon*lat,APM,main="Jan")
The graph is drawn.
I'd like to have interpolation at the surface, but something is going wrong with
panel=panel.levelplot.raster
the same behaviour happens when rasterVis is loaded.
I guess I am missing something... any help?
The data has the following structure:
> head(APM)
lat lon jan feb mar apr may jun jul aug sep oct nov dec
1 -18.5 10.5 29.7 28.8 25.6 25.6 26.8 29.9 35.5 46.8 35.5 27.5 27.5 27.9
2 -17.5 10.5 28.8 29.8 26.3 26.2 27.8 31.6 39.7 63.1 40.4 27.6 27.6 28.3
3 -16.5 10.5 28.7 30.0 26.9 26.8 28.6 32.1 41.1 109.4 42.8 29.7 28.9 29.4
4 -15.5 10.5 28.4 29.5 27.5 26.9 29.1 34.2 46.4 109.5 40.8 29.7 29.7 28.5
5 -14.5 10.5 28.2 29.3 27.4 27.8 27.8 42.8 60.7 104.3 49.1 29.4 28.8 28.6
6 -13.5 10.5 27.8 28.4 27.7 28.3 29.8 41.2 102.8 105.7 47.8 29.5 28.5 28.0
totalling 224 cells listed by latitude and longitude:
> dput(APM)
structure(list(lat = c(-18.5, -17.5, -16.5, -15.5, -14.5, -13.5,
-12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5,
-16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
-7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5,
-11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5,
-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5,
-5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5,
-10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5,
-14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5,
-18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5,
-9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5,
-13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5,
-17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5,
-8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5,
-12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5,
-16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
-7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5,
-11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5,
-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5,
-5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5,
-10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5,
-14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5,
-18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5,
-9.5, -8.5, -7.5, -6.5, -5.5), lon = c(10.5, 10.5, 10.5, 10.5,
10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 11.5,
11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5,
11.5, 11.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5,
12.5, 12.5, 12.5, 12.5, 12.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5,
13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 14.5, 14.5, 14.5,
14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5,
15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5,
15.5, 15.5, 15.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5,
16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 17.5, 17.5, 17.5, 17.5, 17.5,
17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 18.5, 18.5,
18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5,
18.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5,
19.5, 19.5, 19.5, 19.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5,
20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 21.5, 21.5, 21.5, 21.5,
21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 22.5,
22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5,
22.5, 22.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5,
23.5, 23.5, 23.5, 23.5, 23.5, 24.5, 24.5, 24.5, 24.5, 24.5, 24.5,
24.5, 24.5, 24.5, 24.5, 24.5, 24.5, 24.5, 24.5, 25.5, 25.5, 25.5,
25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5
), jan = c(29.7, 28.8, 28.7, 28.4, 28.2, 27.8, 28, 29.4, 30.3,
32.5, 33.2, 33.5, 33.1, 34.3, 28.4, 29.4, 29.6, 29, 28.9, 28.8,
28.5, 28.9, 29.9, 31, 32.2, 32.9, 35.8, 37.4, 30, 27.4, 29.6,
30, 28.4, 30.6, 30.4, 29.9, 30.2, 31.3, 33.9, 35.5, 35.8, 34.5,
30.3, 23.8, 25.2, 23.2, 22.9, 24, 27.2, 29.3, 31.6, 32.3, 31,
31.4, 37.3, 37.4, 24.9, 25.1, 23.4, 21.2, 23.5, 22.8, 23.5, 24.5,
26, 27.7, 28.6, 33.3, 37.3, 40.7, 37.9, 38.5, 27.1, 22.5, 24.7,
23.3, 24.7, 24.8, 26.6, 27.4, 30.7, 33.9, 35.9, 37.6, 30.7, 29.5,
26.6, 24.1, 24.1, 25.4, 25.4, 25.7, 28, 28.2, 32.9, 36.3, 35.2,
40.4, 22.7, 25.5, 26.5, 24.6, 24.3, 24, 25.8, 26.7, 29.4, 31.8,
35.4, 38, 37.4, 41.3, 22.2, 23.7, 26.8, 25.8, 25.3, 24, 25.1,
26.9, 29.6, 31.8, 34.4, 35.6, 39.3, 40.1, 26.2, 26.2, 26, 25.8,
25.3, 23.2, 24.5, 25.9, 26.9, 30.8, 33.3, 38.3, 40.2, 41.2, 26.9,
26.4, 27.1, 23.1, 22.9, 24, 28.5, 26.9, 27.1, 31, 32.7, 36.6,
38, 41.6, 26.6, 27.2, 27.3, 26, 23.6, 25.8, 33.2, 33.8, 25.8,
28.1, 31.6, 34.7, 35.3, 38.5, 27.3, 28.1, 28.6, 27, 31.5, 31.8,
29.9, 27.6, 25.4, 28.4, 29.8, 32.2, 36.4, 36.8, 28.2, 26.2, 27.3,
27, 27.1, 23.7, 23.5, 25.3, 26.1, 29, 29.3, 29.3, 36.4, 35.1,
24.2, 25.1, 23.5, 23.1, 24, 24.2, 24.3, 26.7, 26.7, 26.8, 29.3,
30.4, 33.3, 33.4, 24.8, 24.3, 24.5, 24.8, 26.1, 24.6, 25, 25.3,
27.2, 26.8, 28.9, 30.5, 31.2, 33.1), feb = c(28.8, 29.8, 30,
29.5, 29.3, 28.4, 28.6, 28.7, 28.7, 28.5, 29.4, 32.2, 34.4, 37.4,
29.4, 30.5, 30.8, 30.7, 29.6, 28.3, 28, 28.4, 28.4, 29, 29.5,
31.4, 33.3, 37.1, 31.7, 26.5, 29.8, 30.4, 28.4, 28.9, 27.7, 27.8,
29.3, 30, 30.4, 31.7, 32.9, 34.7, 29.1, 23.5, 24.2, 23.3, 22.7,
23.6, 25.4, 26.4, 27.7, 28.1, 26.9, 27.5, 30.4, 31.6, 22.3, 24.7,
23.7, 21.2, 23.4, 22.5, 22.3, 21.7, 23.2, 23.3, 23.2, 27.5, 31.6,
34, 33, 33.3, 26.3, 22.5, 23.9, 24, 24.5, 23.7, 24.2, 24.5, 25.4,
27.8, 31, 33.4, 29.3, 27.5, 24.7, 23.2, 23.5, 24.7, 25.7, 25.4,
25.8, 25.6, 27.2, 30.7, 30.4, 35, 21.4, 23.3, 24.1, 24.7, 25.2,
25.2, 25.6, 25.9, 27.7, 26.9, 30.1, 31.5, 32, 34.5, 20.2, 22.4,
24.9, 25.1, 25.3, 25.5, 24.1, 25.8, 26.2, 28.4, 29.3, 30.9, 33.5,
35.6, 24.3, 24.2, 24, 24.5, 24.9, 24.7, 23.8, 25, 26.2, 27.1,
29.9, 31.3, 34.6, 34.7, 25.3, 23.6, 24.9, 22.6, 23.1, 23.1, 25.3,
26.8, 24.9, 25.1, 28.1, 30.7, 32.1, 34.2, 24, 23.4, 24.6, 25.6,
24.9, 26.4, 30.6, 31.2, 22.2, 23.9, 27.6, 30.8, 29.1, 29.7, 24,
23.6, 25.4, 25.8, 29.9, 29.5, 27.2, 26.3, 23.5, 24.5, 25.6, 27,
28.9, 28.3, 24.4, 22, 23.4, 24.9, 25.4, 22.7, 23, 23, 22.4, 23.7,
24.6, 24.9, 29.8, 28.5, 21.3, 22.8, 21.3, 21.1, 21.7, 22.8, 24.4,
23.2, 23.3, 22.4, 25.4, 26.3, 27.3, 27.1, 22.8, 22.1, 21.4, 21.9,
24.1, 24.1, 26, 23.5, 23.8, 22.5, 23, 24.9, 26.5, 28)), .Names = c("lat",
"lon", "jan", "feb"), row.names = c(NA,
-224L))

The following produces a plot in a fresh interactive session of R
APM <- results of dput(APM) from the OP
library(latticeExtra)
levelplot(jan~lon*lat,APM,main="Jan",panel=panel.levelplot.raster,interpolate=T)
or alternatively
levelplot(jan~lon*lat,APM,main="Jan",interpolate=TRUE, useRaster = TRUE)
If this is not working for you, check to see if you have the latest versions of the packages in question; for example,
R version 2.15.2 (2012-10-26)
latticeExtra_0.6-24
lattice_0.20-10
RColorBrewer_1.0-5
If the problem still is not resolved, there's something else that hasn't been mentioned in this post.

Related

Finding the 10 closest values to 100 and the 30 closest ones to 30

I have the variables price (dollars) and size (sq.feet). From my dataset, I want to find the 10 houses that best fit the description "price close to 100 and size close to 30".
I can see that I have to use the abs() function, and find the smallest absolute difference. However, I want both conditions to be true at the same time, I don't know how to do that with abs.
I thought about doing
which(abs(...))
but I don't know how.
Any ideas?
The data:
data_struct <-
structure(
list(
new_baltimore.SQFT = c(
11.25,
28.92,
30.62,
26.12,
22.04,
39.42,
21.88,
25.6,
44.12,
19.88,
12.08,
10.99,
12.8,
29.79,
14.3,
13.72,
11.84,
18.06,
10.72,
8.96,
14.38,
36.75,
20,
22.82,
24.86,
19.2,
11.58,
26,
14.4,
11.62,
23.08,
23.76,
15.6,
10,
22.8,
16.76,
22.1,
14.28,
15.36,
16,
23.04,
24.94,
11.82,
12.88,
11.2,
18.12,
38.25,
17.68,
19.02,
32.8,
15.16,
21.975,
12.6,
23.52,
17.52,
47.61,
20.55,
35.52,
8.4,
13.68,
14.48,
12.8,
12.8,
18,
15.4,
10.08,
8.96,
8.96,
20,
12.88,
12,
18.16,
14.28,
26,
12.02,
20.8,
11.78,
8.68,
17.6,
11.4,
44.55,
46.32,
10.24,
9.6,
31.2,
26.4,
13.6,
27.48,
17.86,
18.04,
14.84,
10.46,
14.56,
6.96,
9.5,
11.86,
12.88,
12.32,
6.72,
10.08,
15.6,
6.72,
11.52,
11.76,
10.24,
11.52,
9.28,
6.72,
15.6,
15.5,
9.84,
15.6,
13.76,
10.24,
5.76,
10.08,
11.52,
12.15,
9.77,
15,
14.4,
14.5,
22.54,
10.24,
7.8,
8.4,
10.92,
42.9,
9,
10.5,
10.08,
12.6,
8.96,
8.58,
7.56,
10.8,
13.44,
10.24,
14.44,
12.24,
13.2,
9.6,
15.22,
24.16,
10.24,
10.24,
9.88,
23.2,
17.68,
24.3,
35.94,
21.6,
11.02,
21,
23.92,
14.4,
28,
11.44,
21.94,
10.24,
16.86,
9.92,
13.44,
12,
14.76,
8.96,
11.52,
8.64,
8.12,
11.12,
11.28,
10.36,
11.52,
17.1,
17.52,
10.73,
11.2,
12.8,
12,
41.07,
12.8,
22.36,
10.56,
13.44,
11.02,
17.98,
18.88,
11.76,
9.36,
11.52,
27.3,
23.04,
17.68,
13.36,
11.6,
11.52,
9.98,
12.96,
11.13,
19.6,
11.52,
12.16,
0,
10.64
),
new_baltimore.PRICE = c(
47,
113,
165,
104.3,
62.5,
70,
127.5,
64.5,
145,
63.5,
58.9,
65,
48,
3.5,
12.8,
17.5,
36,
41.9,
53.5,
24.5,
24.5,
55.5,
60,
51,
46,
46,
44,
54.9,
42.5,
44,
44.9,
37.9,
33,
43.9,
49.6,
52,
37.5,
50,
35.9,
42.9,
107,
112,
44.9,
55,
102,
35.5,
62.9,
39,
110,
8,
62,
85.9,
57,
110,
67.7,
89.5,
70,
74,
13,
48,
24,
53.5,
34.5,
53,
87.5,
33.5,
24,
9.6,
30,
41,
30,
38.9,
20.7,
49.9,
18.6,
39,
34,
16,
18.9,
15.2,
41.5,
53,
22,
24.9,
6.7,
32.5,
30,
59,
29.5,
26,
16.5,
39,
48.9,
33.5,
46,
54,
57.9,
37.9,
32,
31,
34,
29,
32.5,
51.9,
31,
41.8,
48,
28,
35,
46.5,
51.9,
35.4,
16,
35,
35,
36.5,
35.9,
45,
40,
35,
38,
37,
23,
25.5,
39.5,
21.5,
9,
67.5,
13.4,
12.5,
28.5,
23,
33.5,
9,
11,
30.9,
31.65,
33,
33.4,
47,
40,
46,
45.5,
57,
29.9,
30,
34,
51,
64.5,
57.5,
85.5,
61,
38,
56.5,
60.4,
51.5,
54,
69,
56,
27.9,
37.5,
32.9,
22,
29.9,
39.9,
32.6,
38.5,
21.5,
25.9,
27.5,
22.9,
31.5,
8.5,
5.5,
33,
57,
47,
43.5,
43.9,
68.5,
44.25,
61,
40,
44.5,
57,
35,
35.1,
64.5,
40,
42.6,
50,
58,
58,
55,
43,
54,
39,
45,
42,
38.9,
43.215,
26.5,
30,
29.5
)
),
class = "data.frame",
row.names = c(NA,-204L)
)
I think you're right with absolute differences. Try to subtract price and size to get a zero point, similar to de-meaning. Using replicate we subtract a respective matrix. From the absolute differences first rank the columns then again the rowSums. The first k values of the order should give you the desired appartements!
f <- \(price, size, k, data) {
aux <- abs(data - t(replicate(nrow(data), c(size, price))))
data[order(rank(rowSums(as.data.frame(lapply(aux, rank)))))[1:k], ]
}
f(price=100, size=30, k=10, data=data_struct[1:2])
# new_baltimore.SQFT new_baltimore.PRICE
# 2 28.920 113.0
# 4 26.120 104.3
# 42 24.940 112.0
# 54 23.520 110.0
# 58 35.520 74.0
# 41 23.040 107.0
# 151 35.940 85.5
# 8 25.600 64.5
# 88 27.480 59.0
# 52 21.975 85.9
You could do:
data_struct[head(order(pdist::pdist(data_struct, c(30,100))#dist),10), ]
new_baltimore.SQFT new_baltimore.PRICE
4 26.120 104.3
41 23.040 107.0
54 23.520 110.0
42 24.940 112.0
2 28.920 113.0
49 19.020 110.0
151 35.940 85.5
52 21.975 85.9
45 11.200 102.0
65 15.400 87.5
using tidyverse you could do:
data_struct %>%
mutate(price_diff = (new_baltimore.PRICE -100)^2,
size_diff = (new_baltimore.SQFT-30)^2) %>%
slice_min(price_diff + size_diff, n=10) %>%
select(-ends_with('diff'))
new_baltimore.SQFT new_baltimore.PRICE
1 26.120 104.3
2 23.040 107.0
3 23.520 110.0
4 24.940 112.0
5 28.920 113.0
6 19.020 110.0
7 35.940 85.5
8 21.975 85.9
9 11.200 102.0
10 15.400 87.5
in base R:
data_struct[head(order(colSums((t(data_struct) - c(30, 100))^2)), 10),]
new_baltimore.SQFT new_baltimore.PRICE
4 26.120 104.3
41 23.040 107.0
54 23.520 110.0
42 24.940 112.0
2 28.920 113.0
49 19.020 110.0
151 35.940 85.5
52 21.975 85.9
45 11.200 102.0
65 15.400 87.5

R cut function: how to cut data which can include the right lowest and highest boundaries

I am a beginner in R. I used cut function in R to bin my data. My data starts from 0 but after cutting the lower boundary has a negative result and I have no idea why this happened.
My code is:
cancer_rtcl$cancer_rate_cut=cut(cancer_rtcl$rate,6)
The statistical summary of my data is:
> summary(cancer_rtcl$rate)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0 13.3 16.5 16.4 18.8 63.5
> dput(cancer_rtcl$rate)
c(63.5, 41.5, 36, 33.9, 29.7, 27.2, 27.2, 26, 25.9, 25.9, 25.3,
25.1, 24.6, 24.3, 23.6, 23.3, 22.8, 22.7, 22.5, 22.4, 22.3, 22.3,
21.9, 21.9, 21.7, 21.6, 21.5, 21.4, 21.3, 21.2, 21.2, 20.9, 20.8,
20.7, 20.5, 20.5, 20.3, 20.2, 20, 19.7, 19.7, 19.6, 19.6, 19.5,
19.4, 19.1, 19, 19, 19, 18.9, 18.9, 18.8, 18.8, 18.8, 18.8, 18.8,
18.7, 18.5, 18.5, 18.5, 18.4, 18.3, 18.3, 18.2, 18.2, 18.2, 18.1,
18.1, 18, 17.9, 17.9, 17.9, 17.8, 17.8, 17.8, 17.7, 17.7, 17.6,
17.6, 17.6, 17.5, 17.4, 17.4, 17.3, 17.3, 17.3, 17.3, 17.3, 17.2,
17.2, 17.1, 17.1, 17.1, 17, 17, 16.9, 16.9, 16.9, 16.8, 16.8,
16.7, 16.6, 16.6, 16.6, 16.5, 16.5, 16.5, 16.5, 16.5, 16.4, 16.4,
16.4, 16.4, 16.2, 16.1, 16, 16, 16, 16, 15.9, 15.9, 15.8, 15.8,
15.7, 15.7, 15.7, 15.7, 15.6, 15.6, 15.6, 15.6, 15.6, 15.5, 15.4,
15.4, 15.4, 15.3, 15.3, 15.3, 15.3, 15.2, 15.1, 15.1, 15, 15,
14.8, 14.6, 14.6, 14.4, 14.2, 14.2, 14.1, 14.1, 14.1, 14.1, 14,
13.9, 13.8, 13.7, 13.6, 13.6, 13.6, 13.3, 13.2, 13.2, 13.1, 13.1,
13, 12.9, 12.9, 12.7, 12.6, 12.5, 12.4, 12.3, 12.3, 12.2, 12,
11.9, 11.8, 11.6, 11.6, 11.4, 11.4, 11.3, 11, 10.8, 10.8, 10.7,
10.6, 10.5, 10.2, 9.9, 9.8, 9.7, 9.7, 9.6, 9.6, 9.5, 9.3, 9.2,
9.2, 9, 9, 8, 7.9, 7.3, 7.1, 7, 6.9, 6.3, 4.6, 0, 0, 0, 0, 0)
But the cutting result is:
6 Levels: (-0.0635,10.6] (10.6,21.2] (21.2,31.8] (31.8,42.3] ... (52.9,63.6]
As you can see, the lowest boundary is a negative result, which is not ideal because I need to make a map based on the binned data.
I also tried another type of coding:
cancer_rtcl$rate_cut=cut(cancer_rtcl$rate,c(5,10,15,20,25))
But in this way, I lost the data larger than 25.
Can anyone help to figure out how to bin the data and get the exact lowest and highest boundaries?
Thanks!
Does this work to capture data larger than 25? cancer_rtcl$rate_cut1=cut(cancer_rtcl$rate,c(5,10,15,20,25,Inf))

Seasonal adjustment package (Census Bureau) in R only adjusting half the data

I've managed to get the library(seasonal) package to run perfectly on half my data (columns 2 and 8) but it refuses to seasonally adjust the other half. I'm really confused as the code runs fine and no error shows up, so I'm wondering if this is a feature of my data (I'm new to time-series) or are there hidden errors in my code?
head(x)
Date European Maori Pacific Peoples Asian MELAA Other Ethnicity Total
1 2004-09-30 7.9 17.9 14.8 15.4 13.4 14.1 9.7
2 2004-12-31 7.9 18.6 13.3 20.9 14.3 14.9 9.9
3 2005-03-31 7.8 17.8 14.6 21.1 12.6 16.1 9.8
4 2005-06-30 7.6 18.1 11.9 20.4 12.6 13.7 9.4
5 2005-09-30 7.1 17.4 10.5 16.1 9.4 19.2 9.0
6 2005-12-31 7.6 15.9 9.8 16.6 11.1 12.0 9.3
Code as follows:
*Note the seasonal adjustment runs smoothly in sub1 and sub2. No seasonal adjustment occurs in sub3 and sub4
library(tidyverse)
library(seasonal)
x <- read.csv("HLF524501_20191112_083700_50.csv", header = TRUE, check.names = FALSE, stringsAsFactors = FALSE)
x <- x %>%
mutate(Date = ifelse(substring(Date, 5, nchar(x)) == "Q3", paste(substring(Date, 0, 4), "09-30", sep = "-"),
ifelse(substring(Date, 5, nchar(x)) == "Q4", paste(substring(Date, 0, 4), "12-31", sep = "-"),
ifelse(substring(Date, 5, nchar(x)) == "Q1", paste(substring(Date, 0, 4), "03-31", sep = "-"),
paste(substring(Date, 0, 4), "06-30", sep = "-")))))
x$Date <- as.Date(x$Date)
sub1 <- x[,c(1,8)]
season0<-ts(sub1[,-1],frequency=4,start=c(2004,3))
sea1 <- seas(season0)
plot(sea1)
sub2 <- x[,c(1,2)]
season0<-ts(sub2[,-1],frequency=4,start=c(2004,3))
sea2 <- seas(season0)
plot(sea2)
sub3 <- x[,c(1,3)]
season0<-ts(sub3[,-1],frequency=4,start=c(2004,3))
sea3 <- seas(season0)
plot(sea3)
sub4 <- x[,c(1,4)]
season0<-ts(sub4[,-1],frequency=4,start=c(2004,3))
sea4 <- seas(season0)
plot(sea4)
Data as follows:
dput(x)
structure(list(Date = c("2004-09-30", "2004-12-31", "2005-03-31",
"2005-06-30", "2005-09-30", "2005-12-31", "2006-03-31", "2006-06-30",
"2006-09-30", "2006-12-31", "2007-03-31", "2007-06-30", "2007-09-30",
"2007-12-31", "2008-03-31", "2008-06-30", "2008-09-30", "2008-12-31",
"2009-03-31", "2009-06-30", "2009-09-30", "2009-12-31", "2010-03-31",
"2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30",
"2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30",
"2012-12-31", "2013-03-31", "2013-06-30", "2013-09-30", "2013-12-31",
"2014-03-31", "2014-06-30", "2014-09-30", "2014-12-31", "2015-03-31",
"2015-06-30", "2015-09-30", "2015-12-31", "2016-03-31", "2016-06-30",
"2016-09-30", "2016-12-31", "2017-03-31", "2017-06-30", "2017-09-30",
"2017-12-31", "2018-03-31", "2018-06-30", "2018-09-30", "2018-12-31",
"2019-03-31", "2019-06-30", "2019-09-30"), European = c(7.9,
7.9, 7.8, 7.6, 7.1, 7.6, 7.7, 6.7, 7.2, 8.5, 8.1, 7.8, 7.2, 7.7,
9, 8.1, 8.9, 9.6, 10.8, 11.5, 12.1, 12.5, 11.4, 11.3, 10.9, 11.4,
11.8, 11.7, 10.9, 11, 11.6, 11.8, 12.6, 12.4, 11.1, 10.7, 11.2,
12.2, 11.2, 10.8, 10.3, 11.7, 11.3, 10.9, 11, 10.7, 11.4, 10.9,
10.4, 11.1, 10.6, 10.1, 10, 11.1, 11, 10.4, 9.5, 11.4, 10.2,
9.5, 8.7), Maori = c(17.9, 18.6, 17.8, 18.1, 17.4, 15.9, 17.3,
16.1, 15.6, 18.1, 19.3, 17, 16.8, 15.9, 18.9, 16.2, 17.5, 18.9,
21.7, 22.4, 24.9, 26.3, 25, 25.5, 25.6, 25.1, 25.8, 25.5, 25.1,
24.5, 25, 25, 28.1, 28.4, 24, 24.7, 24.8, 25.7, 24.5, 22.9, 23.1,
25.1, 24.4, 24, 25.1, 23.5, 25.2, 22.8, 22.9, 23.1, 22.3, 21.4,
20.6, 21.7, 20.9, 20.3, 19, 20.9, 19.3, 17.6, 17.6), `Pacific Peoples` = c(14.8,
13.3, 14.6, 11.9, 10.5, 9.8, 15, 12.3, 12.3, 12.6, 11.6, 15,
9.5, 12.8, 17.6, 13.2, 15.6, 16.1, 21.3, 23.4, 25.1, 25.8, 24.4,
25.8, 26.1, 24.8, 24.2, 25.1, 25.2, 24.2, 27.6, 27.8, 29.6, 29.2,
27.7, 27.7, 27.2, 26.2, 24.8, 21.6, 22.4, 22.9, 23.7, 20.8, 23.9,
21.7, 21.8, 18.8, 18.7, 19.8, 19.9, 18.9, 18.4, 18.5, 16.9, 17.4,
14.8, 20.4, 18, 15.5, 14.1), Asian = c(15.4, 20.9, 21.1, 20.4,
16.1, 16.6, 11.1, 12.5, 8.9, 19.6, 17.3, 14.6, 15.5, 13.3, 13.9,
14.6, 13.4, 15.2, 16, 17, 20.6, 19.4, 18.2, 18.7, 17.9, 17.8,
18.2, 15.2, 15.3, 19, 18.4, 18.3, 18.5, 17.8, 14, 14.7, 14.4,
14.5, 14.9, 14.9, 13, 13.7, 15.6, 14.9, 13.7, 14.5, 17.5, 14.6,
13.4, 12.5, 13.1, 11.3, 11.1, 13.1, 11.9, 11.1, 11.1, 12.5, 11.1,
10.5, 9.5), MELAA = c(13.4, 14.3, 12.6, 12.6, 9.4, 11.1, 18.6,
7, 13.7, 15.4, 19.7, 20.5, 12.1, 15.9, 25.8, 21.7, 25.6, 26.4,
26.5, 26.9, 19.1, 28.5, 23, 21.4, 24.2, 14.5, 19.8, 25, 27.1,
14.2, 23.7, 22.4, 23.8, 21.8, 18.3, 14.5, 21.7, 20.7, 25.5, 21.4,
20.9, 26.2, 21.9, 25.9, 19.9, 18.7, 21.7, 19, 15.8, 23.9, 17.4,
18.3, 17, 20.3, 17.5, 17.9, 10.9, 16.1, 14.9, 22.6, 13.5), `Other Ethnicity` = c(14.1,
14.9, 16.1, 13.7, 19.2, 12, 9.8, 13.4, 10.1, 16.6, 10, 16.1,
18.5, 9.2, 9.2, 13, 8.2, 9.8, 6.7, 8.6, 10.2, 9.1, 12.6, 10.6,
8.8, 10.6, 11.1, 12.5, 12, 13.7, 15, 16.8, 12.2, 12.7, 7.9, 10.8,
12.5, 13.2, 12.1, 12.4, 8.5, 9.5, 13.1, 12.6, 10.2, 7.8, 9.4,
11.8, 11.9, 14.2, 13, 13.4, 12.3, 12.6, 13.4, 11.6, 13.7, 11.4,
11.2, 13.6, 9.3), Total = c(9.7, 9.9, 9.8, 9.4, 9, 9.3, 9.4,
8.5, 8.6, 10.3, 10.3, 9.7, 8.8, 9.2, 10.8, 9.8, 10.3, 11.2, 12.6,
13.4, 14.6, 14.9, 13.9, 13.8, 13.7, 13.9, 14.3, 13.8, 13.4, 13.6,
14.3, 14.4, 15.3, 14.9, 13.2, 13, 13.5, 14.4, 13.4, 12.9, 12.3,
13.7, 13.8, 13.2, 13.2, 12.8, 13.8, 12.7, 12.2, 12.8, 12.5, 11.6,
11.7, 12.6, 12.1, 11.7, 10.9, 12.8, 11.5, 10.8, 9.9)), class = "data.frame", row.names = c(NA,
-61L))
Would really appreciate any help :)
Thanks!
The seas function doesn't include seasonality if it doesn't detect seasonality in the data. This is based on QS statistics, which is discussed in detail here: https://stats.stackexchange.com/questions/148573/the-results-and-specifics-from-the-qs-function-in-r
You can use summary(sea1) and qs(sea1) vs. summary(sea3) and qs(sea3) to see ARIMA modeling details and the tests for seasonality on your data. More broadly, would recommend using summary on all 4 of your models to see the modeling details for each part of your data.

How can I order two vectors in R on the sorting of one?

My question is about the two following vectors:
list_1 <- c(17.5, 19.3, 17.0, 16.5, 19.4, 19.0, 19.6, 20.7, 18.5, 19.0, 20.2,
21.0, 19.7, 19.5, 17.0, 20.2, 19.3, 19.6, 19.9, 18.4, 18.6, 19.5)
list_2 <- c(24.5, 24.5, 24.0, 23.9, 25.8, 27.5, 29.1, 26.5, 28.1, 26.5,
29.2, 30.5, 27.0, 28.5, 25.2, 28.5, 29.1, 29.1, 26.8, 27.8, 26.5, 26.8)
The values in both vectors are correlated, that means that for example 17.5 (list_1) and 24.5 (list_2) belong together.
I want to order list_1 from small to large, and order list_2 relative to the new placement of the value it correlates with in list_1, so keeping 17.5 and 24.5 together on the same placement in both lists.
list_1 <- c(17.5, 19.3, 17.0, 16.5, 19.4, 19.0, 19.6, 20.7, 18.5, 19.0, 20.2,
21.0, 19.7, 19.5, 17.0, 20.2, 19.3, 19.6, 19.9, 18.4, 18.6, 19.5)
list_2 <- c(24.5, 24.5, 24.0, 23.9, 25.8, 27.5, 29.1, 26.5, 28.1, 26.5,
29.2, 30.5, 27.0, 28.5, 25.2, 28.5, 29.1, 29.1, 26.8, 27.8, 26.5, 26.8)
df <- data.frame(list_1, list_2)
df <- df[order(df$list_1),]
list2 <- df$list_2
Demo

What is the expansion of this model formula in R

I have a set of data points that can be fit with the following model:
temp <- structure(list(x = 1:119, y = c(50, 50, 43.1, 39.2, 34.3, 34.9,
34.3, 32.5, 31.8, 30.1, 30.5, 29.7, 28.6, 28.5, 28.5, 28.6, 27.8,
27.7, 26, 27, 26.9, 25.6, 25.8, 25.5, 25.1, 24.3, 24.1, 24.3,
24.1, 24.8, 24.7, 24.5, 23.9, 23.8, 23.7, 23.7, 23, 23.3, 23.7,
23.2, 22.8, 23.5, 22.7, 22.3, 22.7, 22.1, 22.1, 22.4, 22.5, 22.3,
22, 22.2, 22.1, 22.3, 21.9, 23.5, 21.5, 21.4, 22.3, 21.9, 21.7,
21.2, 21.6, 21.5, 21.5, 21.5, 21.7, 21.3, 20.9, 20.9, 21.2, 20.9,
20.8, 21, 21.3, 21.3, 20.7, 21, 20.8, 20.8, 20.8, 20.9, 20.7,
20.7, 20.1, 20.8, 20.8, 22.3, 20.8, 20.6, 20.4, 20.5, 20.2, 20.6,
20.1, 20.5, 20.5, 22, 20.5, 20.3, 20.3, 20.3, 20.1, 20.1, 20.1,
20.2, 20, 20.2, 20.2, 20.1, 20.1, 20, 20.1, 20.1, 20, 19.9, 20,
19.8, 19.9)), .Names = c("x", "y"), row.names = c(NA, -119L), class = "data.frame")
mod <- glm(y ~ log(x)*atan(x), data=temp)
plot(temp)
lines(predict(mod), col="red")
The question is, which is the resulting expansion of the model formula?

Resources