offset lexis grid by one - r

I have a dataset that has age cohorts from 0 - 80, and years 1900-2021. I am making a lexis plot with this data using the lexis_grid function from the LexisPlotR package. I am using the following code to get started:
library(LexisPlotR)
lexis_grid(year_start = 1900,
year_end = 2021,
age_start = 0,
age_end = 80,
delta = 10
)
However, what this does is sets the upper righthand end of the diaganal lines at 2021, and so they don't like up with vertical decade lines and it just looks wrong:
Any advice on how to index the diagonal lines to start at 1900, rather than end at 2021?

Change year_end to 2020 or 2030, so it increments in 10s to match with age increment - delta:
lexis_grid(year_start = 1900,
year_end = 2030,
age_start = 0,
age_end = 80,
delta = 10)

Related

Holt-Winters fitted values does not start from first date in R

I am trying to overlay Holt-Winter fitted values (red) with original values (black) in R, but the red line does not begin from the start.
Perhaps this is related to the size of moving window, if yes, how do I alter in the HoltWinter() object in R?
Tried:
dfts <- dfts$Monthly_Sales %>% ts(start = c(2019, 1), end = c(2021, 12), frequency = 12)
hw1 <- HoltWinters(dfts)
hw1.pred <- predict(hw1, n.ahead = 24, prediction.interval = TRUE, level = 0.95)
plot(hw1, predicted.values = hw1.pred, ylab = "Sales Amt")
Expecting: Red line to begin from the start or near to the start.
Got:
Thanks.

How to implement small graphs to show distribution of spending in multiple categories

10 people submitted monthly spending patterns for four categories of spending, "gas, groceries, restaurants, and entertainment (ent)".
I would like to show a small grid of graphs summarizing the spending so you can see in each category the distribution of the spending.
Here are the issues with my code below.
The four separate graphs with the shared x-axis are good. However, I want the y-axis to reflect the amount of people at that level of spending, not four categories as it does now.
Looking at "grocery" for example, it appears there are some people with negative spending. I guess this is an artifact of using, geom_density_ridges. What would be a better choice?
library("tidyverse")
library("ggridges")
spend_df <- tibble(
gas = c(100, 100, 100, 100, 100, 100, 100, 100, 100, 100),
grocery = c(200, 200, 200, 500, 500, 500, 1200, 1200, 1200, 2200),
ent = c(0, 0, 50, 50, 50, 50, 1000, 1100, 1200, 3000),
restaurant = c(500, 0, 50, 2000, 20, 120, 220, 5, 250, 1000)
)
# Make long
spend_long <- spend_df |>
gather(category, category_monthly_spend, gas:restaurant)
# Reorder ridgelines
spend_long$category <- factor(spend_long$category,
levels = c("gas", "grocery", "restaurant", "ent"))
ggplot(spend_long, aes(x = category_monthly_spend, y = category)) +
geom_density_ridges() + facet_wrap(~category)
Assuming that your real data has more than 10 observations, and that you do want a density-like visualization of distribution, perhaps just geom_density with facets could work?
You may also want to experiment with scales = "free_y" in the facets.
spend_long %>%
ggplot(aes(category_monthly_spend)) +
geom_density() +
facet_wrap(~category)
Result:

Heatmap with calendR

Newbie here, I am having an issue creating a heatmap with the following data (365 variables)
The error is:
"Error in calendR(year = 2021, special.days = Events11, gradient = TRUE, :
No element of the 'special.days' vector can be greater than the number of days of the corresponding month or year"
The only time it seems to work is when "special.days = 1:365" for some reason, can anyone help?
Events11 <- c(879,1997,2122,2767,3334,3425,3373,3353,4352,2882,2964,3686,3922,3152,3536,2819,2110,3101,2687,2290,3678,2658,3004,2586,2586,1089,1922,2254,2769,1844,368,1516,1973,2292,1430,1064,663,521,844,1176,1112,1163,1544,582,459,312,221,494,645,782,907,737,1275,2485,2821,2829,3515,6612,3022,3010,3647,5073,3579,4840,6149,5813,8150,9511,6704,6938,6499,11675,4833,1952,3797,3342,3275,5974,11779,15132,10208,4661,7376,5427,5538,11605,6663,8053,8828,5658,4812,8217,17934,15919,10248,15543,14057,4645,11047,4764,9813,9884,8937,7373,7613,9728,12547,11891,5883,5561,5671,9492,11812,13328,7717,12754,16214,5780,8443,10576,21690,22206,8531,7884,10692,8006,11233,12929,6071,7876,9380,11428,14119,17265,13049,20364,12795,10549,13406,17304,20889,30104,18677,16910,15846,17105,9676,5682,18111,22761,18455,16354,17081,19631,21521,30320,24527,14793,18815,20676,21281,24869,28841,29919,19366,20332,21539,21771,23542,31261,18387,17988,20929,19720,12373,13415,11694,24158,13381,14631,15659,20661,22326,28036,28086,23317,19473,19109,19031,24713,20944,10558,16633,19040,21134,17094,19541,31877,27352,19866,21586,21290,21986,21712,27321,26976,20321,21349,22253,22841,24917,30966,25601,19443,20559,21065,21927,22585,30466,23038,14026,17085,17844,20310,24250,32693,26920,19540,20916,21128,21063,23518,26588,25821,19457,14416,16899,21696,22544,25713,19766,18540,18968,20954,20616,19491,23345,28157,24786,15904,20167,20770,22116,28073,22788,17766,19359,20847,22395,21727,27347,23476,15968,14458,13638,16652,19825,24884,21715,17256,18275,19135,19228,21720,19814,15708,14377,16425,16943,13247,16907,27027,25335,10716,15976,14720,16662,16007,20841,19649,16428,18308,17843,13039,14957,17318,3700,6407,13638,14054,7826,4673,14996,12711,10619,10775,10815,10800,11542,13998,13985,12741,12248,12649,8324,6884,7688,4925,7070,10035,8261,7648,8192,9358,7975,6196,6926,6387,2785,3155,4468,4491,6375,8552,8766,10617,9641,8539,3107,5466,5286,6626,7755,5675,5902,6524,7365,8356,9047,6872,6544,4375,4227,5613,5041,3697,4320,3224,2266,2733,3521,2042,3931,4604,4689)
calendR(year = 2021,
special.days = Events11,
gradient = TRUE,
special.col = rgb(1, 0, 0, alpha = 0.6),
low.col = "white")
"Error in calendR(year = 2021, special.days = Events11, gradient = TRUE, :
No element of the 'special.days' vector can be greater than the number of days of the corresponding month or year"
This is due to the way calendR do with scale - they restricted the values of element to no greater than 365. What you can do is normalize the input to a scale that in acceptable range no greater than 365 for a year or 3x days for a month.
Here is an example
library(calendR)
# a scale function that always have the value between 0 - 1
scale_fn <- function(x) { x / sqrt(sum(x^2)) }
# normalized your data
Events11_normalized <- scale_fn(Events11)
calendR(year = 2021,
special.days = Events11_normalized,
gradient = TRUE,
special.col = rgb(1, 0, 0, alpha = 0.6),
low.col = "white")
And here the calendR work just fine.
Created on 2022-01-26 by the reprex package (v2.0.1)

Developing a custom function of loops for forecasting any loan

Dear all,
I have written a simple function that takes inputs of a typical loan and forecasts the loan until the end of the Term, and saves all data as Time Series in a dataframe.
Inputs: R (interest rate), start (start of the loan), CMP (monthly payment), Term (how long is the loan contract), Bal0 (as default loan amount), frequency (how often is interest calculated, if 365 then daily), and months (how many months in a year).
Typical behaviour of a loan is as follows: Take out a loan, Bal0 = £1000 on start= 2022.01.20 with interest of R = 10%, CMP= £100, Term= 1 year. Daily Interest (R/365) will start counting on the following day (2022.01.21) until end of Term, calculated on previous day's closing balance.
After a precise calendar month on the 2022.02.20, the Loan Balance of £1000 will get added the cumulative daily interest over the calendar month and get the monthly payment deducted. Such that on that day the Closing Balance = £1000 + (the sum of all interests over the past month= sum(Closing_Balance*R/365)=) £0.2739 - the monthly payment (CMP) £100=
1000+0.2739-100= £900.27.
Then the daily interest starts counting on now the lower balance of £900.27. Until, again, in a precise calendar month, 2022.03.20. the same will happen: Closing Balance = £900.27 + (sum of all interests)-£100.
#Loading all packages.#
library(zoo)
library(lubridate)
library(timetk)
#Importing Data.#
data<- structure(list(Key = c(281575989162.667, 281576178390.667, 281576180153.333,
281576278448, 281576311669.333, 281576312741.333, 281576399669.333, 281576403989.333,
281576418453.333, 281576513364, 281576518421.333, 281576520128, 294909322496, 294909511724,
294909513486.667, 294909611781.333, 294909645002.667, 294909646074.667, 294909733002.667, 294909737322.667,
294909751786.667, 294909846697.333, 294909851754.667, 294909853461.333), start = structure(c(1637539200, 1637539200,
1637539200, 1637539200, 1637539200, 1637539200, 1637539200, 1637539200, 1637539200, 1637539200, 1637539200, 1637539200,
1637539200, 1637539200, 1637539200, 1637539200, 1637539200, 1637539200, 1637539200, 1637539200, 1637539200, 1637539200, 1637539200,
1637539200), class = c("POSIXct", "POSIXt"), tzone = "UTC"), CMP = c(94.32, 117.7, 246.5, 94.32, 117.7, 246.5, 94.32, 117.7, 246.5, 94.32,
117.7, 246.5, 94.32, 117.7, 246.5, 94.32, 117.7, 246.5, 94.32, 117.7, 246.5, 94.32, 117.7, 246.5), R = c(0.23497, 0.21294, 0.1485, 0.23497, 0.21294, 0.1485,
0.23497, 0.21294, 0.1485, 0.23497, 0.21294, 0.1485, 0.23497, 0.21294, 0.1485, 0.23497, 0.21294, 0.1485, 0.23497, 0.21294, 0.1485, 0.23497, 0.21294, 0.1485),
Term = c(12, 18, 36, 12, 18, 36, 12, 18, 36, 12, 18, 36, 12, 18, 36, 12, 18, 36, 12, 18, 36, 12, 18, 36), Bal0 = c(1000, 1800, 7126, 1000, 1800, 7126, 1000,
1800, 7126, 1000, 1800, 7126, 1000, 1800, 7126, 1000, 1800, 7126, 1000, 1800, 7126, 1000, 1800, 7126)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -24L))
#Writing loan amortization function.#
loan_amort <- function (R, start, CMP, Term, Bal0, frequency = 365,months = 12) {
answer = df() #Save all answers in a dataframe#
periods = Term * frequency #How many interest counting periods we have#
daily_interest = R / frequency #Daily interest for each loan#
nmonths = Term * months #For how many months loan x is for#
#For each day, function calculates the closing_balance. For the first day, closing_balance = original loan amount. Monthly_CMP is applied only after one calendar month after the start of the loan for all remaining months, and the monthly_interest is the sum of all interests each single day in that calendar month.#
for (p in 1:periods) {
closing_balance = (opening_balance – monthly_CMP + monthly_interest)
if (Date == start) {
opening_balance = Bal0
} else {
opening_balance = lag(closing_balance)
}
}
#The monthly_CMP is CMP, but gets applied only once per calendar month after start.#
for(m in 1:nmonths){
if (Date == (ymd(start) %m+% months(m)) {
monthly_CMP = CMP
} else {
monthly_CMP = 0
}
#The monthly_interest is the sum of all daily interests (lag(closing_balance) * R/365, but gets applied only once per calendar month after start.#
if (Date == (ymd(start) %m+% months(m)) {
monthly_interest = summarise_by_time(interest, .by = "month", .type = "round")
} else {
monthly_interest = 0
}
}
#If the Date is a calendar month away from start, apply the sum of all daily interests that are calculated on each days' closing_balance. If it's not a calendar month away, keep calculating the interest, but the monthly_interests remains 0 until that monthly settlement date.#
interest = daily_interest * lag(closing_balance)
#Then save closing_balance as a zooreg timeseries, where we are saving the closing_balance starting on the start date of the loan and it's saved in daily frequency.#
closing_balance = zooreg(closing_balance, start, frequency = frequency)
}
#Save all columns to a dataframe with all of these aspects#
answer$closing_balance = closing_balance
answer$opening_balance = opening_balance
answer$monthly_CMP = monthly_CMP
answer$monthly_interest = monthly_interest
answer$interest = interest
return(answer)
}
Could anyone help me out with this function, please? The output says it cannot find any of my variables in the function, including: Date, unexpected errors "m in 1:nmonths", object CMP not found, error in recognising loops on "for (p in 1:periods)", daily_interest not found, closing_balance not found, opening_balance not found, object "answer" not found, interest not found.
Hence, would anyone have better ideas on how to specify this function or write loops and saving output as a timeseries (in my case zooreg), or is it better to completely rewrite the whole script?
Any advice would be very appreciated,
thank you.

How to make QtChart do not draw line chart at certaing ranges?

The code below draws line from year 2020 to 2023 that passes through the year 2022. The point is, how to make QChartView to ignore the year 2022, i.e. draw line from 2021 immediately to 2023, or saying in another words, remove 2022 from X axis?
auto series = new QLineSeries();
series->append(QDateTime(QDate(2020, 1, 1)), 100);
series->append(QDateTime(QDate(2021, 1, 1)), 200);
series->append(QDateTime(QDate(2023, 1, 1)), 300);
series->append(QDateTime(QDate(2023, 1, 1)), 200);
QDateTimeAxis *axisX = new QDateTimeAxis;
axisX->setTickCount(20);
axisX->setLabelsAngle(-90);
axisX->setFormat("dd.MM.yyyy hh:mm:ss");
chart->addAxis(axisX, Qt::AlignBottom);
series->attachAxis(axisX);
QValueAxis *axisY = new QValueAxis;
axisY->setTickCount(30);
chart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisY);
chart->setTitle(m_stock->name());
m_chart_view->setChart(chart);
Finally solution is found. Should use QBarCategoryAxis instead of QDateTimeAxis.

Resources