Group by adjoining dates in r - r

I have the following data frame
df<-structure(list(DATE = c("30/06/15", "23/06/15", "22/06/15", "21/06/15",
"18/06/15", "12/06/15", "09/06/15", "08/06/15", "02/06/15", "08/04/15",
"06/04/15", "05/04/15", "07/03/15", "06/03/15", "04/03/15", "03/03/15",
"02/03/15", "26/01/15", "25/01/15", "20/01/15", "19/01/15", "18/01/15",
"17/01/15", "16/01/15", "15/01/15", "14/01/15", "13/01/15", "12/01/15",
"11/01/15", "10/01/15", "09/01/15", "08/01/15", "07/01/15", "06/01/15",
"05/01/15", "04/01/15", "03/01/15", "02/01/15", "01/01/15", "31/12/14",
"30/12/14", "29/12/14", "28/12/14", "27/12/14", "26/12/14", "25/12/14",
"27/08/14", "26/08/14", "25/08/14"), TICKETS = c(17L, 15L, 22L,
16L, 15L, 10L, 18L, 12L, 20L, 20L, 19L, 12L, 16L, 9L, 20L, 18L,
15L, 19L, 13L, 18L, 21L, 27L, 17L, 17L, 18L, 18L, 21L, 18L, 22L,
20L, 16L, 15L, 23L, 15L, 17L, 12L, 20L, 16L, 9L, 13L, 21L, 16L,
16L, 14L, 10L, 7L, 15L, 12L, 14L), COLUMN = c(17L, 15L, 22L,
16L, 15L, 10L, 18L, 12L, 20L, 20L, 19L, 12L, 16L, 9L, 20L, 18L,
15L, 19L, 13L, 18L, 21L, 27L, 17L, 17L, 18L, 18L, 21L, 18L, 22L,
20L, 16L, 15L, 23L, 15L, 17L, 12L, 20L, 16L, 9L, 13L, 21L, 16L,
16L, 14L, 10L, 7L, 15L, 12L, 14L), FLAG = c(1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1), MA = c(0.318405243036592, 0.163298743855817, 0.706171490988531,
0.240851993446204, 0.163298743855817, 0.153604587657018, 0.168145821955216,
0.158451665756417, 0.153604587657018, 0.163298743855817, 0.182687056253413,
0.172992900054615, 0.172992900054615, 0.158451665756417, 0.182687056253413,
0.177839978154014, 0.187534134352813, 0.153604587657018, 0.172992900054615,
0.168145821955216, 0.20207536865101, 0.226310759148007, 0.187534134352813,
0.221463681048607, 0.240851993446204, 0.303864008738394, 0.318405243036592,
0.33294647733479, 0.391111414527581, 0.39595849262698, 0.410499726925178,
0.420193883123976, 0.463817586018569, 0.478358820316767, 0.468664664117968,
0.439582195521573, 0.434735117422174, 0.381417258328782, 0.342640633533588,
0.323252321135991, 0.337793555434189, 0.328099399235391, 0.303864008738394,
0.284475696340797, 0.226310759148007, 0.163298743855817, 0.158451665756417,
0.177839978154014, 0.168145821955216)), .Names = c("DATE", "TICKETS",
"COLUMN", "FLAG", "MA"), row.names = c(412L, 316L, 302L, 288L,
246L, 162L, 120L, 106L, 22L, 102L, 74L, 60L, 87L, 73L, 45L, 31L,
17L, 351L, 337L, 267L, 253L, 239L, 225L, 211L, 197L, 183L, 169L,
155L, 141L, 127L, 113L, 99L, 85L, 71L, 57L, 43L, 29L, 15L, 1L,
426L, 418L, 405L, 392L, 378L, 364L, 350L, 374L, 360L, 346L), class = "data.frame")
Now, as can be seen, there are some dates where there is clearly a tendency to "group" together, for example december and january (date format is: dd/mm/yy).
Plainly, what I want is to find these groups of adjoining dates (i.e. 25- dec -2014 to 20 - jan - 2015 and june 21st to june 23rd 2015).
As long as there are two days adjoining each other I would count it as a "group". Once "groups" are named, I could filter the longest one, for example.
Idealy I would get something like:
structure(list(DATE = structure(c(47L, 36L, 35L, 34L, 31L, 24L,
20L, 18L, 4L, 17L, 13L, 10L, 15L, 12L, 8L, 6L, 3L, 40L, 37L,
33L, 32L, 30L, 29L, 28L, 27L, 26L, 25L, 23L, 22L, 21L, 19L, 16L,
14L, 11L, 9L, 7L, 5L, 2L, 1L, 49L, 48L, 46L, 45L, 44L, 42L, 39L,
43L, 41L, 38L), .Label = c("01/01/15", "02/01/15", "02/03/15",
"02/06/15", "03/01/15", "03/03/15", "04/01/15", "04/03/15", "05/01/15",
"05/04/15", "06/01/15", "06/03/15", "06/04/15", "07/01/15", "07/03/15",
"08/01/15", "08/04/15", "08/06/15", "09/01/15", "09/06/15", "10/01/15",
"11/01/15", "12/01/15", "12/06/15", "13/01/15", "14/01/15", "15/01/15",
"16/01/15", "17/01/15", "18/01/15", "18/06/15", "19/01/15", "20/01/15",
"21/06/15", "22/06/15", "23/06/15", "25/01/15", "25/08/14", "25/12/14",
"26/01/15", "26/08/14", "26/12/14", "27/08/14", "27/12/14", "28/12/14",
"29/12/14", "30/06/15", "30/12/14", "31/12/14"), class = "factor"),
TICKETS = c(17, 15, 22, 16, 15, 10, 18, 12, 20, 20, 19, 12,
16, 9, 20, 18, 15, 19, 13, 18, 21, 27, 17, 17, 18, 18, 21,
18, 22, 20, 16, 15, 23, 15, 17, 12, 20, 16, 9, 13, 21, 16,
16, 14, 10, 7, 15, 12, 14), COLUMN = c(17, 15, 22, 16, 15,
10, 18, 12, 20, 20, 19, 12, 16, 9, 20, 18, 15, 19, 13, 18,
21, 27, 17, 17, 18, 18, 21, 18, 22, 20, 16, 15, 23, 15, 17,
12, 20, 16, 9, 13, 21, 16, 16, 14, 10, 7, 15, 12, 14), FLAG = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1), MA = c(0.318405243036592,
0.163298743855817, 0.706171490988531, 0.240851993446204,
0.163298743855817, 0.153604587657018, 0.168145821955216,
0.158451665756417, 0.153604587657018, 0.163298743855817,
0.182687056253413, 0.172992900054615, 0.172992900054615,
0.158451665756417, 0.182687056253413, 0.177839978154014,
0.187534134352813, 0.153604587657018, 0.172992900054615,
0.168145821955216, 0.20207536865101, 0.226310759148007, 0.187534134352813,
0.221463681048607, 0.240851993446204, 0.303864008738394,
0.318405243036592, 0.33294647733479, 0.391111414527581, 0.39595849262698,
0.410499726925178, 0.420193883123976, 0.463817586018569,
0.478358820316767, 0.468664664117968, 0.439582195521573,
0.434735117422174, 0.381417258328782, 0.342640633533588,
0.323252321135991, 0.337793555434189, 0.328099399235391,
0.303864008738394, 0.284475696340797, 0.226310759148007,
0.163298743855817, 0.158451665756417, 0.177839978154014,
0.168145821955216), GROUP = c(0, 1, 1, 1, 0, 0, 2, 2, 0,
0, 3, 3, 4, 4, 5, 5, 5, 6, 6, 0, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8,
8, 8)), .Names = c("DATE", "TICKETS", "COLUMN", "FLAG", "MA",
"GROUP"), row.names = c(NA, -49L), class = "data.frame")
Any ideas?

You can use diff and cumsum to generate a group number for any chosen length of gap that would separate a group. (Since the diff will be negative as your proceed backward in time as in this case, you need to use a negative threshold. Here I used -2, which then only allows gaps less than two days to be in same group, but you could use any (lower) number.
df$dt <- as.Date(df$DATE, format="%d/%m/%y")
df$grp <- cumsum( c(0, diff(as.numeric(df$dt)) < -2) )
> df$grp
[1] 0 1 1 1 2 3 4 4 5 6 6 6 7 7 7 7 7 8 8 9
[21] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
[41] 9 9 9 9 9 9 10 10 10

Related

Having trouble finding simple means with DLPYR - `mean()` command is not behaving as envisioned

I'm trying to use dplyr to find the simple mean of a column of values I grouped together:
My initial attempts were to enter the code as follows:
cust_id_flags_3 = customer_sleep %>% group_by(flags) %>% count(flags) %>% summarise(mean_val = mean(n))
But the output I get is a table
# A tibble: 27 x 2
flags mean_val
<dbl> <dbl>
1 0 1966
2 1 2555
3 2 1263
4 3 1694
5 4 1452
6 5 989
7 6 879
8 7 709
9 8 712
10 9 530
# ... with 17 more rows
What I wanted was the mean of the values in the column mean_val. I am able to get it by computing it manually:
> mean_test = sum(cust_id_flags_3$mean_val)/nrow(cust_id_flags_3)
> mean_test
[1] 569.037
Below is the data set I'm using to perform the calculation. But I know it is me doing something wrong with applying my tidyverse verbs. For context I'm doing this in order to be able to illustrate means to use a poisson regression. Thanks for any assistance.
> dput(cust_id_flags_3)
structure(list(flags = c(0, 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),
n = c(1966L, 2555L, 1263L, 1694L, 1452L, 989L, 879L, 709L,
712L, 530L, 526L, 435L, 398L, 334L, 233L, 174L, 145L, 114L,
86L, 61L, 36L, 25L, 21L, 13L, 3L, 7L, 4L)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -27L), groups = structure(list(
flags = c(0, 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), .rows = structure(list(
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -27L), .drop = TRUE))
Your data is already grouped, I can replicate the mean of 569 by
library(dplyr)
df %>%
ungroup() %>%
summarise(mean_val = mean(n))
Right now you only have one value per group (flags) such that the mean value is always the value/1. If I adjust your data to include more values per group, the group_by() command in combination with summarise works as expected.
df <- tibble(
flags = c(
0, 1, 1, 2, 2, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26
),
n = c(
1966L, 2555L, 1263L, 1694L, 1452L, 989L, 879L, 709L,
712L, 530L, 526L, 435L, 398L, 334L, 233L, 174L, 145L, 114L,
86L, 61L, 36L, 25L, 21L, 13L, 3L, 7L, 4L
)
)
df %>%
group_by(flags) %>%
summarise(mean_val = mean(n), count = n())
count = n() adds you a integer for the number of observations per group.

r summarize data by specific date for each year

I have a daily precipitation data for 50 years:
year
month
day
daily_prec
date
1960
1
1
3
1960-01-01
1960
1
2
0
1960-02-01
For each year, I want to get a sum of precipitation for some specific days of vegetation season in a new df like for example: from 1975-05-22 to 1975-07-15, from 1976-05-22 to 1976-06-15 etc.
year
start
end
sum
mean
1975
1975-22-05
1975-15-07
5
3
1976
1976-22-05
1976-15-07
2
1
Any suggestions how to do this?
You can write a function that filters the daily precipitation dataframe for the period delimited by the start and end dates, and apply that rowwise. In this example the dataframe is separate, but you can use the same da
daily_prec_df <- data.frame(
date = seq(as.Date("1960-01-01"), as.Date("1965-01-01"), by=1)
)
library(lubridate)
daily_prec_df <- daily_prec_df %>%
mutate(
year = year(date),
month = month(date),
day = day(date),
daily_prec = sample(1:10, replace = T, size = nrow(daily_prec_df))
)
get_period_aggregates <- function(start, end) {
res_df <- daily_prec_df %>%
filter(date >= start, date <= end) %>%
summarize(sum = sum(daily_prec), mean=mean(daily_prec))
}
aggregate_df <- data.frame(
year = c(1961, 1962),
start = as.Date(c("1961-05-22", "1962-05-22")),
end = as.Date(c("1961-06-22", "1962-06-22"))
)
aggregate_df %>%
rowwise() %>%
mutate(
sum = get_period_aggregates(start, end)$sum,
mean = get_period_aggregates(start, end)$mean
)
# A tibble: 2 x 5
# Rowwise:
year start end sum mean
<dbl> <date> <date> <int> <dbl>
1 1961 1961-05-22 1961-06-22 169 5.28
2 1962 1962-05-22 1962-06-22 206 6.44
You could paste an arbitrary year (like the current as shown here) to day-month and subset the data until "YYYY-07-15" using <=, then aggregate().
u <- as.POSIXct(substring(as.character(dat$date), 6), '%m-%d', tz='UTC') <=
as.POSIXct(paste0(strftime(Sys.Date(), '%Y'), '-07-15'), tz='UTC')
aggregate(prec ~ year, dat[u, ], function(x) c(sum=sum(x), mean=mean(x)))
# year prec.sum prec.mean
# 1 75 1014.000000 5.173469
# 2 76 926.000000 4.724490
# 3 77 968.000000 4.938776
Check:
sum(dat[dat$date <= as.Date('1975-07-15'), 'prec'])
# [1] 1014
mean(dat[dat$date <= as.Date('1975-07-15'), 'prec'])
# [1] 5.173469
Note: In aggregate(), instead of prec ~ year you also could use prec ~ substr(dat$date, 1, 4), so you even would not need to break the date into its individual components beforehand.
Edit
Four a time window, you consequently could do this:
u1 <- as.POSIXct(substring(as.character(dat$date), 6), '%m-%d', tz='UTC')
u <- u1 <= as.POSIXct(paste0(strftime(Sys.Date(), '%Y'), '-07-15'), tz='UTC') &
u1 >= as.POSIXct(paste0(strftime(Sys.Date(), '%Y'), '-06-15'), tz='UTC')
aggregate(prec ~ year, dat[u, ], function(x) c(sum=sum(x), mean=mean(x)))
# year prec.sum prec.mean
# 1 75 141.000000 4.548387
# 2 76 146.000000 4.709677
# 3 77 155.000000 5.000000
Data:
dat <- structure(list(date = structure(c(1826, 1827, 1828, 1829, 1830,
1831, 1832, 1833, 1834, 1835, 1836, 1837, 1838, 1839, 1840, 1841,
1842, 1843, 1844, 1845, 1846, 1847, 1848, 1849, 1850, 1851, 1852,
1853, 1854, 1855, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863,
1864, 1865, 1866, 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874,
1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883, 1884, 1885,
1886, 1887, 1888, 1889, 1890, 1891, 1892, 1893, 1894, 1895, 1896,
1897, 1898, 1899, 1900, 1901, 1902, 1903, 1904, 1905, 1906, 1907,
1908, 1909, 1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918,
1919, 1920, 1921, 1922, 1923, 1924, 1925, 1926, 1927, 1928, 1929,
1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940,
1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951,
1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962,
1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973,
1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984,
1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028,
2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039,
2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050,
2051, 2052, 2053, 2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061,
2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069, 2070, 2071, 2072,
2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081, 2082, 2083,
2084, 2085, 2086, 2087, 2088, 2089, 2090, 2091, 2092, 2093, 2094,
2095, 2096, 2097, 2098, 2099, 2100, 2101, 2102, 2103, 2104, 2105,
2106, 2107, 2108, 2109, 2110, 2111, 2112, 2113, 2114, 2115, 2116,
2117, 2118, 2119, 2120, 2121, 2122, 2123, 2124, 2125, 2126, 2127,
2128, 2129, 2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137, 2138,
2139, 2140, 2141, 2142, 2143, 2144, 2145, 2146, 2147, 2148, 2149,
2150, 2151, 2152, 2153, 2154, 2155, 2156, 2157, 2158, 2159, 2160,
2161, 2162, 2163, 2164, 2165, 2166, 2167, 2168, 2169, 2170, 2171,
2172, 2173, 2174, 2175, 2176, 2177, 2178, 2179, 2180, 2181, 2182,
2183, 2184, 2185, 2186, 2187, 2188, 2189, 2190, 2191, 2192, 2193,
2194, 2195, 2196, 2197, 2198, 2199, 2200, 2201, 2202, 2203, 2204,
2205, 2206, 2207, 2208, 2209, 2210, 2211, 2212, 2213, 2214, 2215,
2216, 2217, 2218, 2219, 2220, 2221, 2222, 2223, 2224, 2225, 2226,
2227, 2228, 2229, 2230, 2231, 2232, 2233, 2234, 2235, 2236, 2237,
2238, 2239, 2240, 2241, 2242, 2243, 2244, 2245, 2246, 2247, 2248,
2249, 2250, 2251, 2252, 2253, 2254, 2255, 2256, 2257, 2258, 2259,
2260, 2261, 2262, 2263, 2264, 2265, 2266, 2267, 2268, 2269, 2270,
2271, 2272, 2273, 2274, 2275, 2276, 2277, 2278, 2279, 2280, 2281,
2282, 2283, 2284, 2285, 2286, 2287, 2288, 2289, 2290, 2291, 2292,
2293, 2294, 2295, 2296, 2297, 2298, 2299, 2300, 2301, 2302, 2303,
2304, 2305, 2306, 2307, 2308, 2309, 2310, 2311, 2312, 2313, 2314,
2315, 2316, 2317, 2318, 2319, 2320, 2321, 2322, 2323, 2324, 2325,
2326, 2327, 2328, 2329, 2330, 2331, 2332, 2333, 2334, 2335, 2336,
2337, 2338, 2339, 2340, 2341, 2342, 2343, 2344, 2345, 2346, 2347,
2348, 2349, 2350, 2351, 2352, 2353, 2354, 2355, 2356, 2357, 2358,
2359, 2360, 2361, 2362, 2363, 2364, 2365, 2366, 2367, 2368, 2369,
2370, 2371, 2372, 2373, 2374, 2375, 2376, 2377, 2378, 2379, 2380,
2381, 2382, 2383, 2384, 2385, 2386, 2387, 2388, 2389, 2390, 2391,
2392, 2393, 2394, 2395, 2396, 2397, 2398, 2399, 2400, 2401, 2402,
2403, 2404, 2405, 2406, 2407, 2408, 2409, 2410, 2411, 2412, 2413,
2414, 2415, 2416, 2417, 2418, 2419, 2420, 2421, 2422, 2423, 2424,
2425, 2426, 2427, 2428, 2429, 2430, 2431, 2432, 2433, 2434, 2435,
2436, 2437, 2438, 2439, 2440, 2441, 2442, 2443, 2444, 2445, 2446,
2447, 2448, 2449, 2450, 2451, 2452, 2453, 2454, 2455, 2456, 2457,
2458, 2459, 2460, 2461, 2462, 2463, 2464, 2465, 2466, 2467, 2468,
2469, 2470, 2471, 2472, 2473, 2474, 2475, 2476, 2477, 2478, 2479,
2480, 2481, 2482, 2483, 2484, 2485, 2486, 2487, 2488, 2489, 2490,
2491, 2492, 2493, 2494, 2495, 2496, 2497, 2498, 2499, 2500, 2501,
2502, 2503, 2504, 2505, 2506, 2507, 2508, 2509, 2510, 2511, 2512,
2513, 2514, 2515, 2516, 2517, 2518, 2519, 2520, 2521, 2522, 2523,
2524, 2525, 2526, 2527, 2528, 2529, 2530, 2531, 2532, 2533, 2534,
2535, 2536, 2537, 2538, 2539, 2540, 2541, 2542, 2543, 2544, 2545,
2546, 2547, 2548, 2549, 2550, 2551, 2552, 2553, 2554, 2555, 2556,
2557, 2558, 2559, 2560, 2561, 2562, 2563, 2564, 2565, 2566, 2567,
2568, 2569, 2570, 2571, 2572, 2573, 2574, 2575, 2576, 2577, 2578,
2579, 2580, 2581, 2582, 2583, 2584, 2585, 2586, 2587, 2588, 2589,
2590, 2591, 2592, 2593, 2594, 2595, 2596, 2597, 2598, 2599, 2600,
2601, 2602, 2603, 2604, 2605, 2606, 2607, 2608, 2609, 2610, 2611,
2612, 2613, 2614, 2615, 2616, 2617, 2618, 2619, 2620, 2621, 2622,
2623, 2624, 2625, 2626, 2627, 2628, 2629, 2630, 2631, 2632, 2633,
2634, 2635, 2636, 2637, 2638, 2639, 2640, 2641, 2642, 2643, 2644,
2645, 2646, 2647, 2648, 2649, 2650, 2651, 2652, 2653, 2654, 2655,
2656, 2657, 2658, 2659, 2660, 2661, 2662, 2663, 2664, 2665, 2666,
2667, 2668, 2669, 2670, 2671, 2672, 2673, 2674, 2675, 2676, 2677,
2678, 2679, 2680, 2681, 2682, 2683, 2684, 2685, 2686, 2687, 2688,
2689, 2690, 2691, 2692, 2693, 2694, 2695, 2696, 2697, 2698, 2699,
2700, 2701, 2702, 2703, 2704, 2705, 2706, 2707, 2708, 2709, 2710,
2711, 2712, 2713, 2714, 2715, 2716, 2717, 2718, 2719, 2720, 2721,
2722, 2723, 2724, 2725, 2726, 2727, 2728, 2729, 2730, 2731, 2732,
2733, 2734, 2735, 2736, 2737, 2738, 2739, 2740, 2741, 2742, 2743,
2744, 2745, 2746, 2747, 2748, 2749, 2750, 2751, 2752, 2753, 2754,
2755, 2756, 2757, 2758, 2759, 2760, 2761, 2762, 2763, 2764, 2765,
2766, 2767, 2768, 2769, 2770, 2771, 2772, 2773, 2774, 2775, 2776,
2777, 2778, 2779, 2780, 2781, 2782, 2783, 2784, 2785, 2786, 2787,
2788, 2789, 2790, 2791, 2792, 2793, 2794, 2795, 2796, 2797, 2798,
2799, 2800, 2801, 2802, 2803, 2804, 2805, 2806, 2807, 2808, 2809,
2810, 2811, 2812, 2813, 2814, 2815, 2816, 2817, 2818, 2819, 2820,
2821, 2822, 2823, 2824, 2825, 2826, 2827, 2828, 2829, 2830, 2831,
2832, 2833, 2834, 2835, 2836, 2837, 2838, 2839, 2840, 2841, 2842,
2843, 2844, 2845, 2846, 2847, 2848, 2849, 2850, 2851, 2852, 2853,
2854, 2855, 2856, 2857, 2858, 2859, 2860, 2861, 2862, 2863, 2864,
2865, 2866, 2867, 2868, 2869, 2870, 2871, 2872, 2873, 2874, 2875,
2876, 2877, 2878, 2879, 2880, 2881, 2882, 2883, 2884, 2885, 2886,
2887, 2888, 2889, 2890, 2891, 2892, 2893, 2894, 2895, 2896, 2897,
2898, 2899, 2900, 2901, 2902, 2903, 2904, 2905, 2906, 2907, 2908,
2909, 2910, 2911, 2912, 2913, 2914, 2915, 2916, 2917, 2918, 2919,
2920, 2921), class = "Date"), month = c(1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12), day = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L), year = c(75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L,
76L, 76L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L), prec = c(9, 9, 3, 8, 6, 5, 7, 1, 7, 7, 5, 7,
9, 3, 5, 9, 10, 1, 5, 6, 9, 1, 10, 9, 1, 5, 4, 9, 4, 8, 7, 8,
4, 7, 0, 8, 0, 2, 9, 6, 4, 4, 0, 10, 4, 10, 9, 6, 10, 6, 3, 3,
4, 8, 0, 7, 7, 2, 3, 5, 7, 10, 8, 6, 8, 2, 3, 8, 7, 2, 0, 1,
2, 5, 2, 7, 0, 4, 5, 0, 6, 2, 4, 6, 8, 6, 2, 1, 1, 3, 7, 0, 2,
9, 9, 7, 3, 5, 7, 6, 6, 2, 2, 4, 9, 10, 7, 7, 5, 0, 6, 8, 8,
5, 5, 5, 0, 4, 6, 8, 4, 4, 6, 6, 7, 4, 9, 10, 2, 7, 9, 6, 6,
9, 9, 6, 8, 1, 8, 6, 1, 1, 5, 8, 7, 8, 2, 9, 3, 1, 7, 3, 8, 4,
7, 8, 2, 0, 1, 7, 9, 6, 6, 2, 5, 2, 5, 3, 1, 2, 7, 4, 4, 5, 4,
1, 8, 6, 8, 8, 9, 9, 3, 3, 7, 7, 9, 8, 1, 3, 2, 8, 1, 1, 1, 1,
5, 1, 7, 7, 9, 5, 9, 4, 2, 4, 10, 5, 3, 3, 5, 6, 3, 1, 5, 8,
6, 4, 5, 9, 5, 2, 5, 10, 3, 8, 3, 2, 0, 2, 4, 2, 3, 0, 10, 8,
1, 9, 6, 4, 1, 6, 1, 2, 5, 5, 2, 1, 5, 9, 3, 2, 3, 5, 0, 8, 1,
5, 6, 6, 7, 1, 3, 9, 5, 1, 9, 2, 9, 9, 1, 8, 5, 1, 9, 3, 3, 4,
5, 4, 5, 0, 2, 10, 3, 2, 5, 0, 3, 0, 9, 7, 3, 4, 3, 1, 8, 6,
1, 0, 5, 4, 1, 3, 9, 9, 4, 2, 3, 3, 1, 10, 5, 1, 2, 9, 3, 7,
9, 10, 4, 4, 1, 3, 6, 9, 4, 8, 7, 8, 9, 0, 2, 4, 7, 5, 5, 3,
8, 3, 4, 1, 8, 4, 0, 0, 10, 4, 8, 9, 4, 6, 1, 2, 7, 5, 7, 10,
5, 5, 6, 7, 3, 10, 6, 6, 6, 9, 4, 3, 1, 3, 8, 1, 7, 10, 2, 1,
1, 8, 6, 5, 4, 3, 6, 8, 1, 10, 2, 1, 9, 5, 7, 2, 7, 5, 10, 9,
6, 1, 0, 5, 6, 4, 9, 1, 10, 3, 1, 4, 1, 6, 2, 7, 1, 2, 3, 8,
4, 1, 1, 5, 10, 8, 5, 1, 5, 3, 7, 5, 5, 5, 4, 1, 9, 0, 4, 2,
1, 5, 7, 9, 4, 9, 3, 5, 1, 8, 7, 1, 4, 4, 7, 7, 4, 3, 3, 7, 9,
8, 4, 1, 8, 1, 1, 6, 3, 2, 4, 9, 3, 0, 9, 10, 5, 5, 6, 9, 2,
8, 2, 6, 8, 1, 9, 5, 9, 2, 1, 0, 10, 5, 8, 4, 5, 2, 8, 3, 2,
0, 1, 2, 5, 8, 1, 9, 6, 1, 9, 3, 2, 8, 2, 3, 5, 3, 2, 3, 4, 6,
8, 9, 8, 1, 6, 7, 0, 9, 5, 3, 9, 2, 8, 6, 4, 1, 0, 5, 5, 2, 7,
10, 5, 2, 6, 8, 7, 2, 3, 3, 9, 2, 7, 1, 1, 1, 9, 3, 9, 8, 8,
2, 0, 0, 3, 3, 7, 3, 5, 8, 6, 2, 10, 0, 2, 6, 0, 0, 6, 6, 2,
9, 7, 6, 2, 5, 10, 10, 5, 7, 3, 8, 4, 10, 8, 5, 5, 3, 1, 5, 5,
3, 8, 3, 5, 5, 8, 2, 4, 5, 5, 5, 7, 4, 8, 9, 1, 2, 5, 3, 5, 6,
2, 7, 2, 7, 3, 4, 7, 5, 3, 2, 2, 2, 6, 7, 1, 5, 9, 7, 3, 1, 6,
8, 5, 7, 5, 4, 10, 9, 2, 10, 9, 7, 5, 5, 2, 1, 5, 3, 8, 9, 3,
8, 2, 8, 6, 4, 8, 3, 5, 8, 1, 4, 1, 1, 2, 3, 3, 4, 5, 9, 7, 10,
3, 8, 1, 1, 7, 5, 6, 1, 9, 8, 0, 7, 5, 9, 9, 5, 8, 5, 2, 7, 8,
7, 2, 3, 3, 6, 9, 9, 4, 7, 8, 7, 4, 3, 1, 7, 2, 6, 1, 4, 3, 8,
4, 4, 4, 6, 4, 10, 10, 9, 1, 5, 3, 7, 6, 9, 9, 6, 5, 9, 9, 4,
1, 8, 9, 4, 2, 1, 4, 2, 1, 3, 6, 1, 10, 9, 10, 7, 1, 0, 7, 7,
9, 4, 4, 5, 4, 1, 4, 2, 7, 0, 9, 9, 8, 10, 6, 7, 4, 1, 7, 1,
3, 6, 4, 1, 9, 4, 7, 0, 6, 3, 4, 1, 8, 1, 9, 9, 2, 5, 5, 2, 5,
2, 3, 5, 2, 9, 5, 6, 7, 6, 9, 1, 0, 5, 2, 6, 4, 0, 0, 10, 2,
0, 2, 1, 2, 4, 0, 4, 6, 6, 3, 7, 9, 0, 8, 8, 1, 6, 5, 1, 3, 4,
8, 2, 5, 8, 3, 4, 7, 0, 6, 9, 4, 9, 10, 7, 3, 2, 3, 9, 1, 2,
5, 3, 8, 9, 0, 8, 4, 7, 1, 5, 9, 6, 9, 8, 6, 8, 9, 2, 2, 1, 8,
8, 3, 4, 6, 4, 4, 9, 4, 1, 7, 7, 1, 0, 4, 0, 6, 9, 3, 2, 5, 1,
4, 3, 9, 9, 8, 8, 10, 6, 2, 1, 8, 10, 10, 1, 6, 9, 1, 4, 2, 4,
5, 10, 9, 9, 8, 1, 0, 3, 2, 8, 5, 5, 1, 3, 8, 4, 0, 7, 6, 1,
3, 4, 3, 7, 4, 6, 6, 8, 7, 7, 4, 7, 0, 8, 1, 9, 1, 2, 8, 2, 4,
4, 6, 6, 1, 2, 3, 4, 7, 1, 4, 0, 5, 8, 5, 2, 9, 1, 3, 9, 8, 1,
8, 9, 1, 1, 5, 1, 5, 0, 8, 1, 8, 5, 5, 0, 6, 7, 2, 8, 4, 6, 2,
2, 2, 8, 1, 3, 8, 3, 9, 0, 3, 3, 1, 4, 5, 0, 10, 9, 10, 2, 0,
1, 4, 5, 9, 6, 5, 1, 9, 3, 4, 6, 7, 2, 6, 8, 10, 0, 8, 2, 1,
7, 3, 8, 6, 5, 0, 3, 0, 9, 1, 1, 4, 7, 9, 2, 4, 6, 6, 0, 5, 9,
2, 7, 9, 0, 1, 1, 4, 10, 4, 7, 7, 0, 9, 5, 7, 0, 9, 0, 8, 5,
3, 5)), row.names = c(NA, -1096L), class = "data.frame")

geom_tile tiles displaced when using ggplotly

Issue:
When using ggplotly in combination with geom_tile, in the following reprex (see below), tiles are displaced.
ggplot output:
ggplotly output:
As you can see, the y-values are partially off-placed by one and the x-values are partially off-placed by a lot.
plotly raises the following warning:
Versions: I have the same issue using R version 3.5.3 and 4.0.1., using ggplot2 version 3.1.1/3.3.3 and plotly version 3.9.0/4.9.3 respectively.
Question: How can I fix this issue such that the ggplotly graph has the same, correct tile placement as the original ggplot2 graph?
Reprex:
library(ggplot2)
library(plotly)
library(data.table)
dt <- structure(list(x = c(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 7, 8, 9, 11, 12, 13, 14, 15,
16, 17, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), y = c(1L,
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, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 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, 24L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 25L, 25L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L, 27L, 27L, 27L), Color = c(0.105263157894737,
0.0736842105263158, -0.136842105263158, -0.136842105263158, -0.221052631578947,
0.105263157894737, 0.0421052631578947, -0.031578947368421, -0.136842105263158,
-0.178947368421053, -0.210526315789474, -0.242105263157895, -0.263157894736842,
NA, NA, 0.761904761904762, 0.619047619047619, 0.523809523809524,
0.476190476190476, 0.380952380952381, 1.19047619047619, 1.0952380952381,
1.04761904761905, 0.952380952380952, 0.904761904761905, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.436923076923077,
0.313846153846154, 0.704615384615385, 1.56615384615385, 1.44307692307692,
1.19692307692308, 1.13230769230769, 1.04, 0.947692307692308,
0.916923076923077, 0.898461538461538, 0.876923076923077, 1.76923076923077,
0.569105691056911, 0.447154471544715, 0.894308943089431, 0.853658536585366,
0.731707317073171, 0.634146341463415, 0.58130081300813, 0.33739837398374,
0.313008130081301, 0.272357723577236, 0.252032520325203, 0.235772357723577,
1.4390243902439, NA, 1.09195402298851, 1.07471264367816, 1.47701149425287,
1.4367816091954, 1.83908045977012, 2.35632183908046, 2.18390804597701,
1.95402298850575, 1.94252873563218, 1.92528735632184, 1.91379310344828,
1.89080459770115, 3.59770114942529, NA, 0.457227138643068, 0.752212389380531,
0.604719764011799, 0.471976401179941, 0.678466076696165, 0.575221238938053,
0.457227138643068, 0.56047197640118, 0.530973451327434, 0.497050147492625,
0.463126843657817, 0.548672566371681, NA, 0.273665320771646,
0.165993719156572, 0.122625990728279, 0.272169881860326, 0.182443547181098,
0.0373859727830118, -0.091969493046209, -0.153282488410348, -0.108419321070734,
-0.129355465829221, -0.145057574398086, -0.157918349035442, -0.0354419021982952,
NA, 0.336239103362391, 0.62266500622665, 0.460772104607721, 0.361145703611457,
0.24906600249066, 0.161892901618929, 0.386052303860523, 0.311332503113325,
0.261519302615193, 0.23412204234122, 0.207970112079701, 0.183063511830635,
NA, 0.39348710990502, 0.251017639077341, 0.33921302578019, 0.617367706919946,
0.597014925373134, 0.556309362279512, 0.461329715061058, 0.345997286295794,
0.440976933514247, 0.400271370420624, 0.37449118046133, 0.347354138398915,
0.831750339213026, 0.820189274447949, 0.725552050473186, 0.630914826498423,
0.599369085173502, 0.378548895899054, 0.157728706624606, 0.126182965299685,
0.0630914826498423, 0.517350157728707, 0.495268138801262, 0.476340694006309,
0.451104100946372, 0.892744479495268, 0.632743362831858, 0.411504424778761,
0.942477876106195, 0.853982300884956, 0.809734513274336, 0.721238938053097,
0.676991150442478, 0.455752212389381, 0.438053097345133, 0.411504424778761,
0.389380530973451, 0.36283185840708, 0.327433628318584, 0.746268656716418,
0.54726368159204, 0.462686567164179, 0.412935323383085, 0.114427860696517,
0.796019900497512, 0.746268656716418, 0.681592039800995, 0.6318407960199,
0.592039800995025, 0.552238805970149, 0.527363184079602, 0.492537313432836,
NA, 0.402097902097902, 0.340909090909091, 0.236013986013986,
0.131118881118881, 0.236013986013986, 0.0262237762237762, -0.0174825174825175,
-0.0699300699300699, 0.13986013986014, 0.106643356643357, 0.0769230769230769,
0.0629370629370629, 0.048951048951049, NA, 0.476495726495726,
0.391025641025641, 0.252136752136752, 0.273504273504273, 0.412393162393162,
0.391025641025641, 0.358974358974359, 0.273504273504273, 0.145299145299145,
0.123931623931624, 0.0737179487179487, 0.0416666666666667, 0.111111111111111,
NA, 0.777385159010601, 0.600706713780919, 0.353356890459364,
0.49469964664311, 0.459363957597173, 0.653710247349823, 0.618374558303887,
0.547703180212014, 0.512367491166078, 0.484098939929329, 0.45583038869258,
0.431095406360424, 0.696113074204947, NA, 1.06481481481481, 0.925925925925926,
0.694444444444445, 1.01851851851852, 0.694444444444445, 0.648148148148148,
0.601851851851852, 0.569444444444445, 0.541666666666667, 0.523148148148148,
0.50462962962963, 0.490740740740741, 0.851851851851852, NA, 0.574712643678161,
0.498084291187739, 0.383141762452107, 0.651340996168582, 0.53639846743295,
0.762452107279693, 0.67816091954023, 0.448275862068965, 0.409961685823755,
0.333333333333333, 0.283524904214559, 0.245210727969349, 0.532567049808429,
1.04166666666667, 0.925925925925926, 0.740740740740741, 0.717592592592593,
0.625, 0.671296296296296, 0.625, 0.578703703703704, 0.532407407407407,
0.643518518518518, 0.578703703703704, 0.518518518518518, 0.851851851851852,
NA, 0.775193798449612, 0.682170542635659, 0.578811369509044,
0.527131782945736, 1.25064599483204, 1.19896640826873, 0.96640826873385,
0.837209302325581, 0.75968992248062, 0.689922480620155, 0.674418604651163,
0.664082687338501, 0.86046511627907, 0.783132530120482, 0.642570281124498,
0.582329317269076, 0.522088353413655, 0.642570281124498, 0.522088353413655,
0.421686746987952, 0.281124497991968, 0.200803212851406, 0.180722891566265,
0.112449799196787, 0.0562248995983936, 0.305220883534137, 1.89473684210526,
1.78947368421053, 1.73684210526316, 1.47368421052632, 1.47368421052632,
1.36842105263158, 1.26315789473684, 1.21052631578947, 1.10526315789474,
1.10526315789474, NA, NA, NA, NA, NA, NA, NA, NA, 2.25, 2, 2,
2, 1.53153153153153, 1.35135135135135, 1.21621621621622, 1.08108108108108,
0.990990990990991, 0.986486486486486, 0.981981981981982, 0.711711711711712,
0.621621621621622, 0.576576576576577, 0.576576576576577, 0.576576576576577,
1.02702702702703, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.129740518962076, 0.0449101796407186, -0.0998003992015968,
-0.229540918163673, -0.384231536926148, -0.484031936127745, -0.62874251497006,
-0.738522954091816, -0.828343313373254, -0.838323353293413, -0.839321357285429,
-0.839820359281437, -0.800399201596807, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, -355L), class = c("data.table",
"data.frame"))
p <- ggplot(dt, aes(x, y, fill = Color)) + geom_tile()
p
g <- ggplotly(p)
g
The problem is that you have the same coordinates repeated more than once with different values for the Color variable. Indeed if you check how many unique combinations for x and y you have there are only 346 not 355 as the number of rows in your data.table.
nrow(unique(dt[, .(x, y)]))
# [1] 346
If you try changing this line of your example:
p <- ggplot(dt, aes(x, y, fill = Color)) + geom_tile()
to
p <- ggplot(unique(dt, by = c('x', 'y')), aes(x, y, fill = Color)) + geom_tile()
you will get the same plot in both ggplot2 and plotly.
Best!

Place a background image in a specific position in ggplot2 and take measurements

My question is quite challenging for me so far. Hope with your help we can get what I need.
I want to place an image as background of a ggplot.
Data to reproduce : Two objects are necessary: a data frame and an image.
DATA at the end of this question.
Background image:
And this is the image to place as background (molar.png):
What I got so far...
I ggplotted:
library(png)
library(grid)
library(ggplot2)
img <- readPNG("images/molar.png")
g <- rasterGrob(img, interpolate = TRUE)
ggplot(df), aes(x = X, y = Y)) +
coord_fixed(ratio = .75) +
annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
geom_point(alpha = .20, size = 1, shape = 19, color = "red") +
stat_smooth(method = 'lm', formula = y ~ poly(x, 3)) +
xlim(0, 100) + ylim(0, 100) +
theme_minimal() +
geom_hline(aes(yintercept = 0), color = "red", lty = 2) +
geom_vline(aes(xintercept = 0), color = "red", lty = 2)
I got this result:
... and what I really want
1. Adapt the regression to this line
Take this red horizontal line as reference:
The X axis (from 0 to 100) must be fitted to this line, as shown now:
This is really what I want: to adapt the background image (without modifying its proportion) to that horizontal line, from the cusp tip (gray) to the ending of the white area to the right.
This would require to select two landmarks in the image, but don't know how to make it automatic by simply selecting one or two points, so that the regression automatically fits that distance.
2. Take measurements
I would like to measure the distance between the highest point of the regression and the point [100, 0], as shown below in green line.
The point here, how can we reference if we are having a scale in the image?
DATA
df <- structure(list(tooth_num = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10),
sample = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L,
36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
47L, 48L, 49L, 50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L,
33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L,
43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L,
51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L,
38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L,
50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L,
49L, 50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L,
36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L), X = c(0, 1.96207644325269,
3.96448989844662, 6.02285180886395, 8.15165005779206, 10.3635651674945,
12.6674140636077, 15.0640931778694, 17.5406465345979, 20.0647832611694,
22.5857329433686, 25.0458992379799, 27.3978582481916, 29.6291517474813,
31.8764771189544, 34.0520372099874, 36.1615029782313, 38.2134690535817,
40.2184171634735, 42.1879211839819, 44.1341445581471, 46.0695527612659,
48.0067272490216, 49.9582712234201, 51.9366936524031, 53.9541918066236,
56.0223057345439, 58.1471219902002, 60.015986446532, 62.0418690161973,
64.172643764213, 66.3646706538791, 68.5820610043302, 70.7926665021678,
72.9630123083219, 75.0533364802678, 77.0143642675254, 78.7889946597798,
80.3221949636097, 81.6792186440018, 83.1246290439824, 84.6572566946537,
86.2607918541562, 87.9199855179157, 89.6207074088368, 91.349834829432,
93.0950207010492, 94.844403612905, 96.5863082060859, 98.3089189787115,
100, 0, 2.07242714587787, 4.28691224245265, 6.60638277527078,
8.99634804343432, 11.4282251674021, 13.8802080085279, 16.3366019674999,
18.7865642588865, 21.2228067680933, 23.6405691464549, 26.0268342309397,
28.2980527055826, 30.4950596311624, 32.6499592979795, 34.7798224410736,
36.8956207286784, 39.0058437957066, 41.1183659656274, 43.2421092543673,
45.3894713560051, 47.5991212687389, 49.7770452245596, 51.9171025815641,
54.0147450240845, 56.0668835223753, 58.0722874535502, 60.0316719092865,
61.9474399302204, 63.8232002348162, 65.6632101130593, 67.4719472029522,
69.2537602596947, 71.1020020072457, 73.0197406878018, 74.8895017058263,
76.7134344938606, 78.4953847484249, 80.2407780197449, 81.9562954259909,
83.649499477401, 85.3283767502077, 87.0008805605653, 88.6745443161437,
90.3562154355033, 92.0029343703826, 93.595173297479, 95.1912911811735,
96.7911009335273, 98.3943073713479, 100, 0, 2.49538787860394,
4.98986333706054, 7.48094091039875, 9.95158514589746, 12.3765352721187,
14.7826711324534, 17.1741487059107, 19.5379450224354, 21.8376445004796,
24.2011429944043, 26.5151298348149, 28.7199538624058, 31.0167238490571,
33.2844073975993, 35.4322146421557, 37.6300939882099, 39.8422715265431,
41.8385466322099, 43.9606111742119, 46.1772777113709, 48.0735169131695,
50.1458450809683, 52.0775711992983, 54.0389983532847, 56.0114855556445,
57.9507899054059, 59.8445463497116, 61.7655941740918, 63.7919980969677,
65.7952774486913, 67.8300149894114, 69.6945929413499, 71.6911766995294,
73.5625774964864, 75.3913842466844, 77.1888383995349, 78.9539192357634,
80.672317733178, 82.2770969086854, 83.9169027294331, 85.5602445166866,
87.1949684008868, 88.8468983089335, 90.5308901736114, 92.1034422363185,
93.6509365884513, 95.3521507218448, 96.9695007185512, 98.5200274462478,
100, 0, 1.93071662500976, 3.82909585694513, 5.83503737371176,
7.96719181032991, 10.2303413170764, 12.617316402646, 15.1102082752161,
17.6402520534245, 20.1564846032436, 22.6509074609361, 25.0393836587477,
27.3129712478007, 29.4988321446333, 31.6158908190034, 33.5547876344398,
35.4063709991687, 37.2999307397267, 39.3177890463815, 41.2304325787261,
43.1170170017018, 45.1909193214654, 47.1049295380901, 49.1859515610248,
51.13271739105, 53.1310164292379, 55.0797239964915, 57.1128373874235,
58.9454191107869, 60.9254683084337, 62.9947826516433, 65.0902258076096,
67.1539265387035, 69.1321933211508, 71.0068236131378, 72.9528127482717,
74.868864278393, 76.7358744964615, 78.5828026706554, 80.4923369340278,
82.44389985077, 84.3433495275878, 86.0966137745744, 87.9057900961145,
89.7809605893561, 91.5773662545089, 93.2971230872706, 94.8659471916182,
96.5480144068937, 98.2832055612151, 100, 0, 2.45904597118919,
4.9478077699383, 7.4437174468872, 9.93045045419645, 12.3969081092835,
14.8359017068762, 17.2430125015322, 19.6157029336795, 21.9526351315638,
24.2532312350846, 26.5173310764965, 28.744978623416, 30.9362680921638,
33.0912151854599, 35.2096628761033, 37.2911746284744, 39.334940182215,
41.3396279466586, 43.3031840060133, 45.2225306271349, 47.2687426646706,
49.3178157378126, 51.3497697054769, 53.3645511784153, 55.3621224700994,
57.3424678778091, 59.3055999637205, 61.2515438521856, 63.1803529324531,
65.0920931559475, 66.9868618795333, 68.8647627411635, 70.7259245031432,
72.5704916304973, 74.3986274315152, 76.2105077766622, 78.0063242391239,
79.7862840948059, 81.5506040412458, 83.2995196192455, 85.0332663696068,
86.7521175196599, 88.4563337345588, 90.1461348523852, 91.8219291438623,
93.4838736361913, 95.1323766000954, 96.7676892790966, 98.3900943221571,
100, 0, 2.20427029931411, 4.42577825043187, 6.67629529636218,
8.96662563154085, 11.3064974908959, 13.704119943729, 16.165246307201,
18.6916215311874, 21.2788860284595, 23.9143289820342, 26.5756256258544,
29.2320665145903, 31.8490369716412, 34.3727083595587, 36.7961343283599,
39.1127253229244, 41.3113092041144, 43.3888560030568, 45.3501991751572,
47.2064997097139, 48.9732280089765, 50.6683743389121, 52.3111665976677,
53.9212675271295, 55.5183816810814, 57.1220463657154, 58.7515758904346,
60.4258971077205, 62.1633013292058, 63.9808505967227, 65.8934996324456,
67.912799670054, 70.0333597621695, 71.9831740002692, 73.9087652121441,
75.8101919344515, 77.6875628781259, 79.5410090537812, 81.3706865591696,
83.1767905164818, 84.9595299852075, 86.7191558367347, 88.4559245173709,
90.170123135482, 91.862066674033, 93.5320896282075, 95.1805376430287,
96.8077452136806, 98.414102584544, 100, 0, 2.50973310686108,
4.98257000649406, 7.40768627831619, 9.78047063985944, 12.100599944502,
14.3704650807463, 16.5940944776443, 18.776452301705, 20.9230915203372,
23.0399794423239, 25.1335076302947, 27.2106366228331, 29.2791814137006,
31.3594526601995, 33.4580064720123, 35.556233172214, 37.6533199380137,
39.7484638590935, 41.8408600426407, 43.9297035958428, 46.0141896258872,
48.0934993624991, 50.1668041229306, 52.2332633294661, 54.2920126144546,
56.3421658028046, 58.3828149119835, 60.4130261870291, 62.4318202756028,
64.4382019654095, 66.4311244992937, 68.4095034527027, 70.3722187161805,
72.3180946704221, 74.2459160462301, 76.1544160295474, 78.0422683314784,
79.9081070132353, 81.7505086436864, 83.5679982468404, 85.3590473193511,
87.1220777955072, 88.8554739421999, 90.5575664989655, 92.2266544854268,
93.8610012363035, 95.458856208853, 97.0184430879027, 98.537991505764,
100, 0, 2.49186292873155, 4.96628290858449, 7.40387310725186,
9.79133340002794, 12.1468199057625, 14.5011896071325, 16.8431655039685,
19.1723761535772, 21.4884872575346, 23.7911793751244, 26.0801603047613,
28.355162607706, 30.6159386554963, 32.8622655825162, 35.0939428097114,
37.310792044589, 39.5126572812178, 41.6993973713744, 43.8708934533968,
46.0270489521849, 48.1677771977769, 50.2930162830572, 52.4027142060487,
54.4968362987667, 56.5753602746497, 58.6382811811283, 60.6856064470565,
62.7173459775728, 64.7335369169464, 66.7342089805929, 68.7194141704892,
70.6892069648972, 72.6436517472169, 74.582822805987, 76.5067919534617,
78.4156508121718, 80.30948109951, 82.1883843431457, 83.8953343826177,
85.318702783443, 86.7569115580089, 88.2078484355329, 89.6694432420707,
91.1396480902398, 92.6164522369269, 94.0978597967267, 95.5818971707953,
97.0665932365733, 98.5499867766401, 100, 0, 2.61369623570294,
5.25019204546923, 7.89758672867171, 10.4854413123113, 13.0066240676632,
15.4846710718853, 17.9159547041448, 20.2976684759407, 22.6276875072264,
24.9044541625748, 27.1268476764071, 29.2940720764344, 31.4055784162507,
33.4609732842652, 35.4599616217844, 37.4022781047122, 39.2876574089524,
41.115772453938, 42.8862115298645, 44.5984417012627, 46.5824735364946,
48.8579364441288, 51.0689873528466, 53.2187049370753, 55.31212578009,
57.3558486751465, 59.3576503629965, 61.3261515894915, 63.2705517814855,
65.2004117555501, 67.125505044989, 69.0556829505137, 71.0007693255153,
72.9704141903561, 74.9739450593852, 77.0201427878221, 79.1169465130639,
80.9682934274075, 82.7378404654242, 84.4621062867744, 86.1441466931183,
87.7873262684694, 89.3952520481705, 90.9717460715737, 92.5207813382925,
94.0464474990516, 95.5529302701972, 97.0444725499921, 98.5253927168307,
100, 0, 2.19489342240264, 4.47470248895734, 6.80636996136228,
9.16189235515265, 11.5204119968896, 13.8679818386464, 16.1962336516669,
18.5008000956494, 20.7800403414259, 23.0340446346007, 25.2640080457511,
27.4717901812673, 29.6596690589774, 31.8301824946609, 33.9860569994518,
36.1301804326855, 38.2656102060449, 40.395611569574, 42.5237505919975,
44.6539625286031, 46.790726843019, 48.9392449637073, 51.1057000819157,
53.0838372365286, 55.0005198693747, 56.9159691456296, 58.8292579968134,
60.7394374767241, 62.645542230868, 64.5465987006055, 66.4416169190052,
68.3295795719832, 70.2094638760246, 72.080216965747, 73.9407777716216,
75.7900606116825, 77.6269798039636, 79.4504168499154, 81.2592587204191,
83.0523732433488), Y = c(84.5741422911491, 87.0989688246201,
89.5436034157802, 91.8694746551154, 94.0314174040144, 95.9740138468311,
97.628391911438, 98.9112343041615, 99.7302288176452, 100,
99.6682935009841, 98.7381965579207, 97.2664228638507, 95.3825982722905,
93.5434433368078, 91.50049262398, 89.2915552625009, 86.9514674858642,
84.511940176201, 82.0015588642778, 79.4462792120302, 76.8706085478357,
74.2982919192514, 71.7531887159568, 69.2602636348217, 66.8466919870944,
64.5423551809367, 62.3722941413001, 59.6800326256192, 57.2857466826492,
55.128758759083, 53.1294867372666, 51.2007256913408, 49.2527551687403,
47.1957784888164, 44.9429337520794, 42.418488359019, 39.5725891439776,
36.3973083864229, 33.0253591771941, 29.7461795438129, 26.5669349259918,
23.4758099710143, 20.4586262556193, 17.4994139966193, 14.5814411300049,
11.6872895390296, 8.79946487886403, 5.90043469063777, 2.97289519972717,
0, 93.0321380957158, 95.1946109521693, 96.937054277536, 98.2619255824812,
99.1910694062447, 99.7577986711693, 100, 99.9550821661862,
99.6575185832176, 99.1377550776607, 98.4215955114931, 97.4779796804826,
95.9441517048638, 94.1389735933651, 92.2010899059795, 90.1898222195824,
88.1388247774415, 86.0724678936469, 84.0123913148843, 81.9837162612796,
80.0234419211725, 78.2597001700458, 76.3926747068224, 74.4111019409554,
72.3108177571526, 70.0927095898208, 67.7621020453996, 65.3282107888812,
62.802504203367, 60.1968602570691, 57.5234982391254, 54.7931356266002,
52.0153976695973, 49.3678395136313, 46.8461605150396, 44.2290481856401,
41.5261960397215, 38.748799403644, 35.9094871516561, 33.021434271488,
30.098088807609, 27.1525574835635, 24.19719611686, 21.2436096189696,
18.3024472027724, 15.3103597044707, 12.2378569960892, 9.17074715833242,
6.10882539864554, 3.05175039610287, 0, 100, 99.9740960784806,
99.8730270144876, 99.6594688780895, 99.1648671200451, 98.300448086114,
97.3281159413371, 96.2806584450564, 95.1069839916948, 93.6992988433163,
92.5227992310794, 91.1519207300211, 89.4389990501897, 88.0315526476314,
86.5030621141021, 84.637382900149, 82.9008651750625, 81.2041782110219,
79.064856495856, 77.1370635776142, 75.4555767641426, 73.1766285349799,
71.1390523302985, 68.8176471360649, 66.5503178701644, 64.3037992816136,
61.9957638533255, 59.6077485751849, 57.270625947771, 55.1882053198138,
53.005988938906, 50.8933669646611, 48.4803231663425, 46.2904669191802,
43.8645308465569, 41.369278904061, 38.8252432322521, 36.2327421588907,
33.5735116287118, 30.7665372266409, 28.0047653665761, 25.244744309195,
22.4736613621324, 19.7247419854022, 17.0183591556834, 14.1736629139801,
11.3015506938947, 8.6188037004008, 5.82611425658707, 2.95264914351926,
0, 86.7221127164863, 89.2450865945842, 91.8256626160176,
94.2050712850442, 96.3082958568556, 98.0567601186487, 99.356578317864,
100, 99.8266960099512, 99.3672567218446, 98.6837623193952,
97.4546359200075, 95.8095875992727, 93.8391732848531, 91.6986699837336,
89.1914649315853, 86.5315472203614, 83.9427805951582, 81.5889388575256,
79.0374892354799, 76.4382738493923, 74.2129556980193, 71.6847000287053,
69.5197780116735, 67.026504640704, 64.6334322074442, 62.1466653908716,
59.8252416036741, 57.210640130131, 54.8147737058654, 52.5672567218449,
50.3760405702802, 48.1148980958759, 45.6822122284949, 43.0613338436512,
40.5655726724713, 38.0157305521002, 35.3812266768728, 32.7135011003732,
30.1526743852262, 27.6670940579847, 25.0888527413645, 22.2776767773419,
19.7712372021815, 17.1514304851209, 14.4034446464453, 11.542359582815,
8.48492967180167, 5.57098842216043, 2.73145153573813, 0,
99.1041781658029, 99.7254865136605, 100, 99.9614316109632,
99.6447768919466, 99.0834281892782, 98.3070217828948, 97.341621109093,
96.2090754809024, 94.9278903961505, 93.5136855943403, 91.9791492509625,
90.3350457026248, 88.5899864186136, 86.7511628918963, 84.8242550277467,
82.8134769494328, 80.7219434437174, 78.5514867381084, 76.302473278107,
73.9737121158334, 71.8911107192181, 69.810524772861, 67.694531029918,
65.5438623813919, 63.3593433296612, 61.1416609600414, 58.8917313862862,
56.6101958880234, 54.2979247733192, 51.9556967388641, 49.5842904813491,
47.1845305031526, 44.757241306653, 42.303109977166, 39.8229610170695,
37.317527317367, 34.7876333804369, 32.2340120972821, 29.657396358906,
27.058564861999, 24.438158886189, 21.7969571281666, 19.1356924789353,
16.4549604124345, 13.7555396253553, 11.0379797859501, 8.30305959090951,
5.55132870848586, 2.78342841830685, 0, 84.1515543172144,
86.2722836260793, 88.3566153916604, 90.3773896250419, 92.3062620354674,
94.111690717205, 95.7577913244362, 97.203231722867, 98.4009161759049,
99.2995644137824, 99.8474619227218, 100, 99.7284790643066,
99.0275302806241, 97.8277140842702, 96.2611985608363, 94.3983312397508,
92.2676143186827, 89.9054453409169, 87.3514589414394, 84.6443817904904,
81.8193876527848, 78.9076236667722, 75.9361708679116, 72.9289916301439,
69.9079560608225, 66.8938683956418, 63.9077697306659, 60.9718854638001,
58.1105727362662, 55.3511099671633, 52.7239337138365, 50.2623228580521,
47.9794231502588, 45.4070011187705, 42.7981815439979, 40.1541092510553,
37.4757316814164, 34.7641541834675, 32.0204031521378, 29.2455049823579,
26.4404465923295, 23.6062938537105, 20.7439547312464, 17.8544950965959,
14.9388623912317, 11.9979251031726, 9.0326701506196, 6.04400549831871,
3.03283911101569, 0, 100, 99.5848739044175, 98.8739209129315,
97.9105346461211, 96.7351022995956, 95.3831954679074, 93.885250878182,
92.2670226841415, 90.5504870541479, 88.7545871260625, 86.8960843842295,
84.9905164585802, 83.0528356573708, 81.0986594271243, 79.1673905588146,
77.2717198905873, 75.3754106896234, 73.4768134130185, 71.5743849399925,
69.6665023331719, 67.7516224717761, 65.8282022350241, 63.8947517131965,
61.9497011799818, 59.9915873311911, 58.0188670460438, 56.0301302314122,
54.0238603720464, 51.9985941637575, 49.9528949078877, 47.8852726947172,
45.794397247711, 43.6787786571497, 41.5370334354364, 39.3678047005055,
37.1697621758216, 34.941522373788, 32.6818614399924, 30.3895023089609,
28.063247731812, 25.7019536707251, 23.304582510003, 20.8700966339479,
18.3975648489857, 15.8861889891947, 13.3352240997155, 10.7440582533417,
8.11223915605116, 5.43939433041456, 2.72533753771655, 0,
100, 99.9134879636606, 99.4737726511386, 98.7119319033914,
97.6645725261045, 96.4711111592938, 95.2721931018034, 94.0226109361477,
92.7232680879358, 91.374959571704, 89.9785165390125, 88.5348785524954,
87.0449851747857, 85.5095952833957, 83.9297207150073, 82.3061926211812,
80.6400228385995, 78.9320063817987, 77.1831189504364, 75.3942278330975,
73.5662725924159, 71.7000843799518, 69.7965666213147, 67.8565504680649,
65.8809393458113, 63.8706005431396, 61.8262568005362, 59.7488476806353,
57.6390236498756, 55.4977604079147, 53.3256361471427, 51.123554293169,
48.8921653124316, 46.6322280824431, 44.344429206666, 42.0294914255877,
39.6880290686225, 37.3207287392334, 34.9282409038592, 32.3093546108134,
29.3214008878868, 26.3486608522194, 23.3891108304469, 20.4407632862286,
17.5015222721514, 14.5693279778263, 11.6421205928639, 8.71769575877807,
5.79402980220384, 2.86895450167932, 0, 99.0881176828924,
99.616812993443, 99.9245775334157, 100, 99.4144184933504,
98.4080380989905, 97.2482296158507, 95.9447863261935, 94.5072176490275,
92.9445788221548, 91.2653289705444, 89.4773594926569, 87.5879656741198,
85.6037331424258, 83.5306797985592, 81.3743977486237, 79.139712667937,
76.8311379822374, 74.4527045497319, 72.0079322747694, 69.5001707437473,
67.4173241176181, 65.7355478830473, 63.9261751158092, 62.0062376111431,
59.9926536189868, 57.9018588217461, 55.7494373120646, 53.5502067518011,
51.3181332130511, 49.0667285867045, 46.8089938097941, 44.5579865920031,
42.3268498019911, 40.129152103299, 37.979256976578, 35.89220917429,
33.8840753566102, 31.5408977402496, 29.095103557573, 26.6000023276786,
24.0608455207656, 21.4827994480562, 18.8707181711938, 16.2292854338693,
13.56295788917, 10.8759367132551, 8.1722811506567, 5.45590851427953,
2.73059418540176, 0, 96.4662652426023, 98.0232502530471,
99.1147667554613, 99.7607331141328, 100, 99.8803419056255,
99.4511603186742, 98.7585534337238, 97.843294734107, 96.7405371746194,
95.4801583016932, 94.0874997966255, 92.5842056245715, 90.989010880653,
89.3184813331895, 87.587358543869, 85.8094473196242, 83.9978129241588,
82.1654220154131, 80.3260794003192, 78.4945266405658, 76.6880690477,
74.9274631350027, 73.2399733961649, 70.9563625122581, 68.4738638027609,
65.9881111030584, 63.4970829949925, 60.9982157287041, 58.4893399773897,
55.9680398998363, 53.4319489577125, 50.8788978242146, 48.3064706581295,
45.7125474355354, 43.0948602238647, 40.4513876049598, 37.7800095548987,
35.0788032612873, 32.3458952146129, 29.5794612082463)), .Names = c("tooth_num",
"sample", "X", "Y"), row.names = c(NA, 500L), class = "data.frame")

Writing a function to build a list of all possible models where each variable has multiple types of regressions

I'd like to build off of this pretty clean way to build a list of all possible models:
Write a function to list all possible models
My problem is that I'd like to be able to set each variable to have multiple regression types. For example, I'd like to be able to test:
x
s(x)
plogis(x)
poly(x,2)
poly(x,3)
I've adapted the code from the answer above to be able to run as a function from a few inputs:
step_gam <- function (dependent, var_list, data) {
vars <- list()
for(n in 1:length(var_list)){
vars[[n]] = list(paste(var_list[n]),
paste("s(",var_list[n],")"),
paste("plogis(",var_list[n],")"),
paste("Poly(",var_list[n],",2)"),
paste("Poly(",var_list[n],",3)"))
}
vars <- unlist(vars)
models <- list()
for (i in 1:length(vars)){
vc <- combn(vars,i)
for (j in 1:ncol(vc)){
model <- as.formula(paste0(dependent, "~", paste0(vc[,j], collapse = "+")))
models <- c(models, model)
}
}
lapply(models, function (x) gam(x, data = data))
}
var_list <- c("EstimatedAge")
step_gam("prop_transit", var_list, weekly_stb_gam)
The only issue is that currently, the function loops back through each variable, so even if I only have 1 term, I end up with 31 models, not 5.
example of the current output for object models:
y~x
y~x + s(x)
y~x + s(x) + plogis(x)
...
What I'd like to be able to do is include each variable only once.
example of expected output for object models:
y~x
y~s(x)
y~plogis(x)
But, I have multiple variables:
example of expected output for object models
y ~ x
y ~ z
y ~ x + z
y ~ s(x)
y ~ s(z)
y ~ s(x) + z
y ~ x + s(z)
y ~ s(x) + s(z)
weekly_stb_gam
dput:
structure(list(EstimatedAge = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2), woy = c(49L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 38L, 6L, 37L, 10L, 36L, 44L, 11L, 24L,
21L, 22L, 23L, 28L, 31L, 41L, 45L, 2L, 5L, 6L, 7L, 9L, 16L, 18L,
19L, 22L, 27L, 28L, 32L, 35L, 39L, 42L, 45L, 49L, 3L, 5L, 19L,
20L, 22L, 28L, 31L, 32L, 35L, 37L, 38L, 51L, 1L, 2L, 8L, 9L,
14L, 15L, 18L, 19L, 25L, 27L, 28L, 33L, 36L, 24L, 25L, 26L, 27L,
29L, 30L, 32L, 33L, 34L, 35L, 36L, 38L, 39L, 40L, 42L, 43L, 44L,
46L, 47L, 48L, 49L, 50L, 51L, 52L, 1L, 3L, 4L, 8L, 11L, 12L,
13L, 14L, 15L), at_large = c(0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 21L, 27L, 6L, 12L, 21L, 24L, 26L, 21L, 0L, 1L, 2L, 4L,
5L, 7L, 8L, 8L, 8L, 9L, 10L, 11L, 10L, 11L, 14L, 15L, 13L, 15L,
15L, 16L, 21L, 23L, 24L, 26L, 28L, 25L, 24L, 27L, 30L, 28L, 29L,
30L, 32L, 32L, 33L, 27L, 26L, 28L, 28L, 26L, 24L, 26L, 22L, 23L,
20L, 23L, 24L, 24L, 24L, 4L, 4L, 4L, 4L, 6L, 5L, 7L, 7L, 6L,
6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
9L, 9L, 11L, 11L, 11L, 10L, 10L, 10L), Avg_Flow = c(5468.14285714286,
7159.28571428571, 3200.28571428571, 6569.71428571429, 6975, 6374.57142857143,
7077, 7172.42857142857, 7171.28571428571, 7167.57142857143, 1490.85714285714,
4373.85714285714, 2424.28571428571, 3429.85714285714, 2378.28571428571,
1003.28571428571, 644.857142857143, 3208.28571428571, 693.428571428571,
1126.14285714286, 1406.57142857143, 4852.85714285714, 4635.42857142857,
989.285714285714, 2135.85714285714, 920.285714285714, 378, 1487.85714285714,
2495.14285714286, 1937, 493.714285714286, 412.857142857143, 190.428571428571,
374.428571428571, 1184.85714285714, 1190.57142857143, 2190, 2419.57142857143,
2001.28571428571, 291.571428571429, 1288.42857142857, 5468.14285714286,
3861.42857142857, 2534.28571428571, 326.714285714286, 535.285714285714,
606.285714285714, 246.857142857143, 342.571428571429, 346, 2323.28571428571,
762.857142857143, 464.571428571429, 1567.14285714286, 2464, 2536.42857142857,
2389.28571428571, 2331.28571428571, 498, 513.571428571429, 1164.85714285714,
1307.71428571429, 4135.85714285714, 6417.85714285714, 7159.28571428571,
7077, 7167.57142857143, 1804.28571428571, 2047.71428571429, 6250.28571428571,
5450.71428571429, 4564.28571428571, 4994.14285714286, 5268.71428571429,
6639.14285714286, 6953.71428571429, 5281.85714285714, 3636.57142857143,
3423.14285714286, 1993.71428571429, 1350.42857142857, 824.142857142857,
1309, 2493.85714285714, 1851.71428571429, 1366.28571428571, 1404.57142857143,
1365.14285714286, 1906.28571428571, 1993.85714285714, 1444.57142857143,
1122.42857142857, 1492.71428571429, 663.714285714286, 721.285714285714,
2517.85714285714, 922.142857142857, 835.285714285714, 906.857142857143,
492.142857142857), Avg_Temperature = c(14.5571428571429, 24.2285714285714,
24.4571428571429, 25.3714285714286, 25.3857142857143, 24.3857142857143,
24.7, 23.7571428571429, 22.4857142857143, 22.6428571428571, 23.9428571428571,
13.6, 22.8142857142857, 15.9714285714286, 24.6714285714286, 17.5142857142857,
18.5142857142857, 23, 19.9, 22.9, 24.5285714285714, 25.2571428571429,
23.8142857142857, 18.1857142857143, 15.1428571428571, 9.58571428571429,
11.5571428571429, 11.8571428571429, 13.6285714285714, 14.5285714285714,
19.9714285714286, 20.7857142857143, 20.6428571428571, 23.5285714285714,
26.1857142857143, 26.1571428571429, 25.7428571428571, 25.1, 22.8285714285714,
20.6142857142857, 17.1571428571429, 14.5571428571429, 10.6142857142857,
11.8285714285714, 20.1571428571429, 19.4285714285714, 22.5, 24.2714285714286,
25.0142857142857, 24.4142857142857, 24.6142857142857, 23.6714285714286,
22.9571428571429, 9.77142857142857, 8.31428571428571, 9.21428571428571,
14.6142857142857, 16.0571428571429, 18.4428571428571, 19.1857142857143,
19.8285714285714, 20.0857142857143, 23.0142857142857, 25.0857142857143,
24.2285714285714, 24.7, 22.6428571428571, 22.8714285714286, 22.7857142857143,
26.3714285714286, 26.2571428571429, 24.7571428571429, 24.4142857142857,
23.4714285714286, 24.9, 24.1571428571429, 24.0714285714286, 24.0142857142857,
21.7142857142857, 20.0285714285714, 18.5571428571429, 18.4, 17.46,
15.18, 14.6, 12.6857142857143, 12.0285714285714, 8.87142857142857,
8.34285714285714, 8.58571428571429, 8.65714285714286, 8.98571428571429,
9.87142857142857, 10.7857142857143, 14.3571428571429, 16.5714285714286,
17.3571428571429, 16.3142857142857, 17.3714285714286, 19.8428571428571
), Avg_Turbidity = c(5.99857142857143, 11.0457142857143, 24.9557142857143,
11.02, 8.63285714285714, 8.62571428571429, 9.84714285714286,
9.91714285714286, 8.31571428571429, 7.93142857142857, 7.34428571428571,
6.58285714285714, 10.2442857142857, 9.04285714285714, 8.57571428571429,
3.69142857142857, 6.71285714285714, 9.22285714285714, 17.6685714285714,
14.6685714285714, 13.5242857142857, 13.0742857142857, 19.3942857142857,
9.04428571428571, 7.74285714285714, 8.15428571428571, 9.22571428571429,
9.17857142857143, 8.56285714285714, 9.18857142857143, 8.43428571428571,
7.8, 8.91857142857143, 7.99, 11.69, 11.42, 14.1371428571429,
9.30857142857143, 6.42428571428571, 3.93, 4.21857142857143, 5.99857142857143,
10.71, 6.77285714285714, 8.67, 8.87571428571428, 7.26285714285714,
9.11285714285714, 10.0828571428571, 10.6071428571429, 9.18, 6.09857142857143,
5.55571428571429, 8.71, 8.13, 9.99857142857143, 12.805, 9.59666666666667,
12.2728571428571, 12.4328571428571, 7.57, 8.03714285714286, 10.5885714285714,
11.4442857142857, 11.0457142857143, 9.84714285714286, 7.93142857142857,
13.8442857142857, 16.9785714285714, 14.8142857142857, 13.8042857142857,
16.63, 20.01, 11.8728571428571, 9.90571428571429, 12.34, 64.4071428571429,
10.9714285714286, 10.3914285714286, 10.07, 10.5142857142857,
7.41142857142857, 7.122, 7.942, 11.9171428571429, 8.47142857142857,
7.97857142857143, 8.00714285714286, 8.08142857142857, 9.42857142857143,
8.95571428571429, 8.38714285714286, 8.46428571428572, 8.22428571428572,
8.12142857142857, 9.46714285714286, 9.14285714285714, 35.4285714285714,
8.88, 8.37714285714286), n_Salmonids = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 4, 0, 43, 0, 0, 7, 0, 86, 13, 0, 0, 0, 0, 0, 0, 0,
0, 5, 12, 104, 22, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 1, 1,
0, 0, 0, 0, 0, 0, 0, 6, 17, 13, 93, 21, 7, 12, 13, 5, 0, 0, 0,
0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 1, 43, 17, 17, 85, 33)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -100L))
> dput(weekly_stb_gam[1:100,c(4:10,13)])
structure(list(EstimatedAge = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2), woy = c(49L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 38L, 6L, 37L, 10L, 36L, 44L, 11L, 24L,
21L, 22L, 23L, 28L, 31L, 41L, 45L, 2L, 5L, 6L, 7L, 9L, 16L, 18L,
19L, 22L, 27L, 28L, 32L, 35L, 39L, 42L, 45L, 49L, 3L, 5L, 19L,
20L, 22L, 28L, 31L, 32L, 35L, 37L, 38L, 51L, 1L, 2L, 8L, 9L,
14L, 15L, 18L, 19L, 25L, 27L, 28L, 33L, 36L, 24L, 25L, 26L, 27L,
29L, 30L, 32L, 33L, 34L, 35L, 36L, 38L, 39L, 40L, 42L, 43L, 44L,
46L, 47L, 48L, 49L, 50L, 51L, 52L, 1L, 3L, 4L, 8L, 11L, 12L,
13L, 14L, 15L), at_large = c(0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 21L, 27L, 6L, 12L, 21L, 24L, 26L, 21L, 0L, 1L, 2L, 4L,
5L, 7L, 8L, 8L, 8L, 9L, 10L, 11L, 10L, 11L, 14L, 15L, 13L, 15L,
15L, 16L, 21L, 23L, 24L, 26L, 28L, 25L, 24L, 27L, 30L, 28L, 29L,
30L, 32L, 32L, 33L, 27L, 26L, 28L, 28L, 26L, 24L, 26L, 22L, 23L,
20L, 23L, 24L, 24L, 24L, 4L, 4L, 4L, 4L, 6L, 5L, 7L, 7L, 6L,
6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
9L, 9L, 11L, 11L, 11L, 10L, 10L, 10L), Avg_Flow = c(5468.14285714286,
7159.28571428571, 3200.28571428571, 6569.71428571429, 6975, 6374.57142857143,
7077, 7172.42857142857, 7171.28571428571, 7167.57142857143, 1490.85714285714,
4373.85714285714, 2424.28571428571, 3429.85714285714, 2378.28571428571,
1003.28571428571, 644.857142857143, 3208.28571428571, 693.428571428571,
1126.14285714286, 1406.57142857143, 4852.85714285714, 4635.42857142857,
989.285714285714, 2135.85714285714, 920.285714285714, 378, 1487.85714285714,
2495.14285714286, 1937, 493.714285714286, 412.857142857143, 190.428571428571,
374.428571428571, 1184.85714285714, 1190.57142857143, 2190, 2419.57142857143,
2001.28571428571, 291.571428571429, 1288.42857142857, 5468.14285714286,
3861.42857142857, 2534.28571428571, 326.714285714286, 535.285714285714,
606.285714285714, 246.857142857143, 342.571428571429, 346, 2323.28571428571,
762.857142857143, 464.571428571429, 1567.14285714286, 2464, 2536.42857142857,
2389.28571428571, 2331.28571428571, 498, 513.571428571429, 1164.85714285714,
1307.71428571429, 4135.85714285714, 6417.85714285714, 7159.28571428571,
7077, 7167.57142857143, 1804.28571428571, 2047.71428571429, 6250.28571428571,
5450.71428571429, 4564.28571428571, 4994.14285714286, 5268.71428571429,
6639.14285714286, 6953.71428571429, 5281.85714285714, 3636.57142857143,
3423.14285714286, 1993.71428571429, 1350.42857142857, 824.142857142857,
1309, 2493.85714285714, 1851.71428571429, 1366.28571428571, 1404.57142857143,
1365.14285714286, 1906.28571428571, 1993.85714285714, 1444.57142857143,
1122.42857142857, 1492.71428571429, 663.714285714286, 721.285714285714,
2517.85714285714, 922.142857142857, 835.285714285714, 906.857142857143,
492.142857142857), Avg_Temperature = c(14.5571428571429, 24.2285714285714,
24.4571428571429, 25.3714285714286, 25.3857142857143, 24.3857142857143,
24.7, 23.7571428571429, 22.4857142857143, 22.6428571428571, 23.9428571428571,
13.6, 22.8142857142857, 15.9714285714286, 24.6714285714286, 17.5142857142857,
18.5142857142857, 23, 19.9, 22.9, 24.5285714285714, 25.2571428571429,
23.8142857142857, 18.1857142857143, 15.1428571428571, 9.58571428571429,
11.5571428571429, 11.8571428571429, 13.6285714285714, 14.5285714285714,
19.9714285714286, 20.7857142857143, 20.6428571428571, 23.5285714285714,
26.1857142857143, 26.1571428571429, 25.7428571428571, 25.1, 22.8285714285714,
20.6142857142857, 17.1571428571429, 14.5571428571429, 10.6142857142857,
11.8285714285714, 20.1571428571429, 19.4285714285714, 22.5, 24.2714285714286,
25.0142857142857, 24.4142857142857, 24.6142857142857, 23.6714285714286,
22.9571428571429, 9.77142857142857, 8.31428571428571, 9.21428571428571,
14.6142857142857, 16.0571428571429, 18.4428571428571, 19.1857142857143,
19.8285714285714, 20.0857142857143, 23.0142857142857, 25.0857142857143,
24.2285714285714, 24.7, 22.6428571428571, 22.8714285714286, 22.7857142857143,
26.3714285714286, 26.2571428571429, 24.7571428571429, 24.4142857142857,
23.4714285714286, 24.9, 24.1571428571429, 24.0714285714286, 24.0142857142857,
21.7142857142857, 20.0285714285714, 18.5571428571429, 18.4, 17.46,
15.18, 14.6, 12.6857142857143, 12.0285714285714, 8.87142857142857,
8.34285714285714, 8.58571428571429, 8.65714285714286, 8.98571428571429,
9.87142857142857, 10.7857142857143, 14.3571428571429, 16.5714285714286,
17.3571428571429, 16.3142857142857, 17.3714285714286, 19.8428571428571
), Avg_Turbidity = c(5.99857142857143, 11.0457142857143, 24.9557142857143,
11.02, 8.63285714285714, 8.62571428571429, 9.84714285714286,
9.91714285714286, 8.31571428571429, 7.93142857142857, 7.34428571428571,
6.58285714285714, 10.2442857142857, 9.04285714285714, 8.57571428571429,
3.69142857142857, 6.71285714285714, 9.22285714285714, 17.6685714285714,
14.6685714285714, 13.5242857142857, 13.0742857142857, 19.3942857142857,
9.04428571428571, 7.74285714285714, 8.15428571428571, 9.22571428571429,
9.17857142857143, 8.56285714285714, 9.18857142857143, 8.43428571428571,
7.8, 8.91857142857143, 7.99, 11.69, 11.42, 14.1371428571429,
9.30857142857143, 6.42428571428571, 3.93, 4.21857142857143, 5.99857142857143,
10.71, 6.77285714285714, 8.67, 8.87571428571428, 7.26285714285714,
9.11285714285714, 10.0828571428571, 10.6071428571429, 9.18, 6.09857142857143,
5.55571428571429, 8.71, 8.13, 9.99857142857143, 12.805, 9.59666666666667,
12.2728571428571, 12.4328571428571, 7.57, 8.03714285714286, 10.5885714285714,
11.4442857142857, 11.0457142857143, 9.84714285714286, 7.93142857142857,
13.8442857142857, 16.9785714285714, 14.8142857142857, 13.8042857142857,
16.63, 20.01, 11.8728571428571, 9.90571428571429, 12.34, 64.4071428571429,
10.9714285714286, 10.3914285714286, 10.07, 10.5142857142857,
7.41142857142857, 7.122, 7.942, 11.9171428571429, 8.47142857142857,
7.97857142857143, 8.00714285714286, 8.08142857142857, 9.42857142857143,
8.95571428571429, 8.38714285714286, 8.46428571428572, 8.22428571428572,
8.12142857142857, 9.46714285714286, 9.14285714285714, 35.4285714285714,
8.88, 8.37714285714286), n_Salmonids = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 4, 0, 43, 0, 0, 7, 0, 86, 13, 0, 0, 0, 0, 0, 0, 0,
0, 5, 12, 104, 22, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 1, 1,
0, 0, 0, 0, 0, 0, 0, 6, 17, 13, 93, 21, 7, 12, 13, 5, 0, 0, 0,
0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 1, 43, 17, 17, 85, 33), prop_transit = c(NaN,
NaN, 0, 0, 0, 0, 0, 0, 0, 0, 0.0476190476190476, 0.037037037037037,
0.333333333333333, 0.0833333333333333, 0.0952380952380952, 0.0416666666666667,
0.0384615384615385, 0.0476190476190476, NaN, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0.166666666666667, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0909090909090909, 0,
0, 0, 0)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-100L))
You have to get all possible terms in your formula per variable, and then arrange every combination of these terms. That includes an "empty" term.
I made a function to generate formulas based on your inputs.
Be careful, the more variables you have, the number of formulas grows exponentially !
mk_formulas <- function (dependent, var_list) {
nvars <- length(var_list)
vars <- lapply(var_list, function(v){
possible_terms <- list("", # empty term
paste0(v),
paste0("s(",v,")"),
paste0("plogis(",v,")"),
paste0("Poly(",v,", 2)"),
paste0("Poly(",v,", 3)"))
})
vars <- do.call(cbind, vars)
possible_combinations <- expand.grid(rep(list(seq_len(nrow(vars))), nvars))
f_list <- apply(possible_combinations, 1, function(cmb) {
keep <- cmb != 1 # remove empty terms
coords <- cbind(cmb[keep], seq_len(nvars)[keep]) # get combination of terms
return(paste(vars[coords], collapse = "+")) # paste
})
f_list <- lapply(f_list[-1], function(f) paste0(dependent, " ~ ", f))
return(do.call(rbind, f_list))
}
To test it out :
var_list1 <- list("x")
var_list2 <- as.list(letters[24:25])
var_list3 <- as.list(letters[24:26])
mk_formulas("dep", var_list = var_list1)
# [,1]
# [1,] "dep ~ x"
# [2,] "dep ~ s(x)"
# [3,] "dep ~ plogis(x)"
# [4,] "dep ~ Poly(x, 2)"
# [5,] "dep ~ Poly(x, 3)"
mk_formulas("dep", var_list = var_list2)
# [,1]
# [1,] "dep ~ x"
# [2,] "dep ~ s(x)"
# [3,] "dep ~ plogis(x)"
# [4,] "dep ~ Poly(x, 2)"
# [5,] "dep ~ Poly(x, 3)"
# [6,] "dep ~ y"
# [7,] "dep ~ x+y"
# [8,] "dep ~ s(x)+y"
# [9,] "dep ~ plogis(x)+y"
# [10,] "dep ~ Poly(x, 2)+y"
# [11,] "dep ~ Poly(x, 3)+y"
# [12,] "dep ~ s(y)"
# [13,] "dep ~ x+s(y)"
# [14,] "dep ~ s(x)+s(y)"
# [15,] "dep ~ plogis(x)+s(y)"
# [16,] "dep ~ Poly(x, 2)+s(y)"
# [17,] "dep ~ Poly(x, 3)+s(y)"
# [18,] "dep ~ plogis(y)"
# [19,] "dep ~ x+plogis(y)"
# [20,] "dep ~ s(x)+plogis(y)"
# [21,] "dep ~ plogis(x)+plogis(y)"
# [22,] "dep ~ Poly(x, 2)+plogis(y)"
# [23,] "dep ~ Poly(x, 3)+plogis(y)"
# [24,] "dep ~ Poly(y, 2)"
# [25,] "dep ~ x+Poly(y, 2)"
# [26,] "dep ~ s(x)+Poly(y, 2)"
# [27,] "dep ~ plogis(x)+Poly(y, 2)"
# [28,] "dep ~ Poly(x, 2)+Poly(y, 2)"
# [29,] "dep ~ Poly(x, 3)+Poly(y, 2)"
# [30,] "dep ~ Poly(y, 3)"
# [31,] "dep ~ x+Poly(y, 3)"
# [32,] "dep ~ s(x)+Poly(y, 3)"
# [33,] "dep ~ plogis(x)+Poly(y, 3)"
# [34,] "dep ~ Poly(x, 2)+Poly(y, 3)"
# [35,] "dep ~ Poly(x, 3)+Poly(y, 3)"
# mk_formulas("dep", var_list = var_list3) # 215 formulas...

Resources