- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在交叉验证中,我问了 question关于按日期分析数据,但不想通过按月对数据进行分箱来生成虚假的峰值和波谷。例如,如果一个人在每个月的最后一天支付账单,但有一次支付晚了几天,那么一个月的费用将为零,而下个月的费用将是平时的两倍。都是假垃圾。
answers 之一我的问题解释了使用线性样条平滑累积和来克服分箱中的打嗝的插值概念。我对它很感兴趣,想在 R 中实现它,但在网上找不到任何例子。我不只是想打印绘图。我想获得每个时间点(也许每天)的瞬时斜率,但该斜率应该从输入几天(或者可能几周或几个月)之前到几天的点的样条线得出时间点之后。换句话说,在一天结束时我想要得到一些东西,例如一个数据框,其中一列是每天的钱或每周的患者,但这不受变幻莫测的影响,例如我是否延迟支付几天或该月是否碰巧有 5 个手术日(而不是通常的 4 天)。
这是一些简化的模拟和绘图,以显示我所面临的情况。
library(lubridate)
library(ggplot2)
library(reshape2)
dates <- seq(as.Date("2010-02-01"), length=24, by="1 month") - 1
dates[5] <- dates[5]+3 #we are making one payment date that is 3 days late
dates#look how the payment date is the last day of every month except for
#2010-05 where it takes place on 2010-06-03 - naughty boy!
amounts <- rep(50,each=24)# pay $50 every month
register <- data.frame(dates,amounts)#this is the starting register or ledger
ggplot(data=register,aes(dates,amounts))+geom_point()#look carefully and you will see that 2010-05 has no dots in it and 2010-06 has two dots
register.by.month <- ddply(register,.(y=year(dates),month=month(dates)),summarise,month.tot=sum(amounts))#create a summary of totals by month but it lands up omiting a month in which nothing happened. Further badness is that it creates a new dataframe where one is not needed. Instead I created a new variable that allocates each date into a particular "zone" such as month or
register$cutmonth <- as.Date(cut(register$dates, breaks = "month"))#until recently I did not know that the cut function can handle dates
table(register$cutmonth)#see how there are two payments in the month of 2010-06
#now lets look at what we paid each month. What is the total for each month
ggplot(register, aes(cutmonth, amounts))+ stat_summary(fun.y = sum, geom = "bar")#that is the truth but it is a useless truth
#so lets use cummulated expense over time
register$cumamount <- cumsum(register$amounts)
cum <- ggplot(data=register,aes(dates,cumamount))+geom_point()
cum+stat_smooth()
#That was for everything the same every month, now lets introduce a situation where there is a trend that in the second year the amounts start to go up,
increase <- c(rep(1,each=12),seq(from=1.01,to=1.9,length.out=12))
amounts.up <- round(amounts*increase,digits=2)#this is the monthly amount with a growth of amount in each month of the second year
register <- cbind(register,amounts.up)#add the variable to the data frarme
register$cumamount.up <- cumsum(register$amounts.up) #work out th cumulative sum for the new scenario
ggplot(data=register,aes(x=dates))+
geom_point(aes(y=amounts, colour="amounts",shape="amounts"))+
geom_point(aes(y=amounts.up, colour="amounts.up",shape="amounts.up"))# the plot of amount by date
#I am now going to plot the cumulative amount over time but now that I have two scenarios it is easier to deal with the data frame in long format (melted) rather than wide format (casted)
#before I can melt, the reshape2 package unforutnately can't handle date class so will have to turn them int o characters and then back again.
register[,c("dates","cutmonth")] <- lapply(register[,c("dates","cutmonth")],as.character)
register.long <- melt.data.frame(register,measure.vars=c("amounts","amounts.up"))
register.long[,c("dates","cutmonth")] <- lapply(register.long[,c("dates","cutmonth")],as.Date)
ggplot(register.long, aes(cutmonth,value))+ stat_summary(fun.y = sum, geom = "bar")+facet_grid(. ~ variable) #that is the truth but it is a useless truth,
cum <- ggplot(data=register,aes(dates,cumamount))+geom_point()
#that is the truth but it is a useless truth. Furthermore it appears as if 2010-06 is similar to what is going on in 2011-12
#that is patently absurd. All that happened was that the 2010-05 payment was delayed by 3 days.
#so lets use cummulated expense over time
ggplot(data=register.long,aes(dates,c(cumamount,cumamount.up)))+geom_point() + scale_y_continuous(name='cumulative sum of amounts ($)')
因此,对于简单绘图,变量 interpolate.daily 约为一年中每天每天 $50/30.4 = $1.64。对于第二个图,每月支付的金额在第二年每个月开始增加,将显示第一年每天的每日费率 1.64 美元,而对于第二年的日期,人们将看到每日费率逐渐从每天 1.64 美元增加到每天约 3.12 美元。
非常感谢您从头到尾阅读本文。您一定和我一样感兴趣!
最佳答案
这是一种基本方法。当然,还有更复杂的选项和需要调整的参数,但这应该是一个很好的起点。
dates <- seq(as.Date("2010-02-01"), length=24, by="1 month") - 1
dates[5] <- dates[5]+3
amounts <- rep(50,each=24)
increase <- c(rep(1,each=12),seq(from=1.01,to=1.9,length.out=12))
amounts.up <- round(amounts*increase,digits=2)
df = data.frame(dates=dates, cumamount.up=cumsum(amounts.up))
df.spline = splinefun(df$dates, df$cumamount.up)
newdates = seq(min(df$dates), max(df$dates), by=1)
money.per.day = df.spline(newdates, deriv=1)
如果您绘制它,您可以看到样条线的有趣行为:
plot(newdates, money.per.day, type='l')
关于r - 如何获得累积和图上常规时间点插值的斜率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8408760/
当给定两个 bool 参数时,^ 运算符执行异或,例如 true ^ true == false true ^ false == true false ^ true == true false ^ f
我需要下载一个文件(例如: https://www.betaseries.com/srt/391160 )所以我在网上找到了不同的方法: def download(String remoteUrl,
可以说,我们正在计算考试成绩的平均值: 起始考试成绩:75、80、92、64、83、99、79 平均值= 572/7 = 81.714 ... 现在给出81.714,如果您不知道初始测试分数,是否可以
我和一个 friend 正在争论线程池中的线程数应该是处理器计数+ 1还是仅仅是处理器计数。 我之所以选择处理器数量,是因为每个处理器可以分配偶数个线程,而他选择处理器数量+ 1是因为他认为这将帮助他
我已经养成了尽可能使用闭包来代替常规方法的习惯,即使我不需要访问自由变量。所以,我将使用这个: def addNumbers = { 左、右 -> 左 + 右 } ..而不是这个: def addNu
我对 Groovy 非常陌生,我正在尝试《Groovy in Action》书中的这个示例。我有这个 fibonacci.groovy 程序,当尝试使用 java 命令运行该程序时,我收到 NoCla
我有 3 个 TextView 。我需要将它们的权重设置为 Light、Regular 和 Condensed。有人可以帮助我了解如何在 Android 中实现这一点吗? 最佳答案 在 TextVie
如果用户启动我的应用程序并最初选择不允许位置服务,我想通过 UIAlertMessage 提示用户重新考虑(“更新”和“不,谢谢。”)。 “不,谢谢。”这将是一个简单的取消,我希望“更新”将它们直接链
如何在 groovy 中显示一个值是真还是假?我使用 Eclipse 作为我的 IDE。 assert 4 * ( 2 + 3 ) - 6 == 14 //integers only 而且我也
我的问题与“多处理器编程的艺术”一书有关。第4章介绍安全/常规/原子寄存器及其实现。 以下是安全多读取器单写 boolean 寄存器的以下实现,该寄存器基于安全单读取器单写 boolean 寄存器,被
使用下面的代码来保存 float 的值 domainInstance.standardScore = params["standardScore"] as float 在这种情况下,我的输入是 17.
使用下面的代码来保存 float 的值 domainInstance.standardScore = params["standardScore"] as float 在这种情况下,我的输入是 17.
在iOS的about部分中,它具有有关设备的大量信息。 我和我可以访问此信息吗? 我想快速获取settings -> General -> About的数据。在iOS中获得相同的价格是否可行? 最佳答
我正在开发Windows服务,它将承载两件事: WCF服务 用于定期作业执行的“常规” Windows服务(使用Quartz.net) 因此,基本上,一个应用程序(可执行)承载这两种服务类型。 这两种
在mysql中,我有一个名为users的表,其中包含系统中的用户列表... id | name | surname | active ____________________________ 1
所以我在 Debian 服务器上设置了一个 MySQL 数据库,并且它在 phpMyAdmin 客户端上运行良好。我目前正在开发一个项目,编写一个 Java 服务器,该服务器能够通过 JDBC 连接使
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
前两天考试了,其中一道题是把@前面的字母换成新的名字 所以在试卷中我们有 array = "toto@yahoo.com","mimi@yahoo.com".soso@yahoo.com"所以我们应该
大家好 如果字符串语法如下,我如何从字符串中获取数字(正数): t_def_type_id_2 t_def_type_id_22 t_def_type_id_334 所以,在第一个字符串中我想得到 1
我正在寻找不会在内核中阻塞的文件描述符类型。我知道我可以使用 fstat(2) 但 fstat 还会给我各种元数据信息(访问时间等),这些信息可能会阻塞任意时间(特别是在网络文件系统上)。 编辑:我正
我是一名优秀的程序员,十分优秀!