2011-12-07 19 views
10

पारित होने पर पारित होने पर मैंने question से डेटा का विश्लेषण करने के बारे में पूछा लेकिन महीने तक डेटा को कताई करके नकली स्पाइक्स और कटोरे उत्पन्न नहीं करना चाहते थे। उदाहरण के लिए यदि कोई प्रत्येक महीने के आखिरी दिन बिल चुकाता है लेकिन एक मौके पर कुछ दिन देर से भुगतान करता है तो एक महीने शून्य व्यय को प्रतिबिंबित करेगा और अगले महीने सामान्य खर्च को दोगुना कर देगा। सभी नकली जंक।संचयी योग प्लॉट पर नियमित समय बिंदुओं पर एक इंटरपोलेशन की ढलानों को मैं कैसे प्राप्त करूं?

मेरे प्रश्न में answers में से एक ने बाइनिंग में हिकोओं को दूर करने के लिए संचयी योग पर रैखिक स्पलीन चिकनाई का उपयोग करके इंटरपोलेशन की अवधारणा को समझाया। मैं इसके बारे में चिंतित हूं और इसे आर में लागू करना चाहता हूं लेकिन ऑनलाइन कोई उदाहरण नहीं मिल रहा है। मैं सिर्फ भूखंड मुद्रित नहीं करना चाहता हूं। मैं प्रत्येक बार बिंदु (शायद हर दिन) पर तात्कालिक ढलान प्राप्त करना चाहता हूं लेकिन उस ढलान को एक स्पिन से लिया जाना चाहिए जो कुछ दिनों से पहले कुछ बिंदुओं (या शायद कुछ हफ्तों या कुछ महीने) से इनपुट इनपुट करता है समय बिंदु के बाद। दूसरे शब्दों में, के अंत में मैं डेटा फ्रेम जैसे कुछ प्राप्त करना चाहता हूं जिसमें एक कॉलम प्रति दिन या प्रति सप्ताह मरीज़ है लेकिन यह अनियमितताओं के अधीन नहीं है जैसे कि मैंने कुछ दिन देर से भुगतान किया है या क्या महीने में 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 

When one is late with a payment by a couple of days it appears as if the expense was zero in one month and double in the next. That is spurious

#so lets use cummulated expense over time 
register$cumamount <- cumsum(register$amounts) 
cum <- ggplot(data=register,aes(dates,cumamount))+geom_point() 
cum+stat_smooth() 

cumulative amount over time smooths out variability that changes an item's bin

#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. 

two scenarios but showing the amount of money paid in each month

#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 ($)') 

Here we see the cumulative sum data for the two scenarios

तो सरल साजिश के लिए परिवर्तनीय interpolate.daily वर्ष के हर दिन के लिए लगभग $ 50/30.4 = $ 1.64 प्रति दिन होगा। दूसरी साजिश के लिए जहां हर महीने भुगतान की जाने वाली राशि दूसरे महीने में हर महीने बढ़ने लगती है, पहले वर्ष में प्रत्येक दिन प्रति दिन $ 1.64 की दैनिक दर दिखाएगी और दूसरे वर्ष की तारीखों में दैनिक दरों को देखेंगे धीरे-धीरे $ 1.64 प्रति दिन से $ 3.12 प्रति दिन बढ़ रहा है।

अंत तक इस तरह से पढ़ने के लिए आपको बहुत बहुत धन्यवाद। जैसा कि मैं था, तुम इतनी चिंतित होनी चाहिए!

+1

मुझे लगता है कि आप बुरी सलाह मिला - यह करने के लिए किया जाएगा एक अधिक सामान्य सांख्यिकीय रास्ता कर्नेल घनत्व अनुमान का उपयोग करने के लिए। – hadley

+0

@ हडली मेरे प्रश्न के उत्तरदाताओं में से एक [घनत्व अनुमान और कर्नल के बारे में बात की थी] (http://stats.stackexchange.com/a/2737/104)। हां, मुझे यह बहुत कुछ समझ में नहीं आया और उन्होंने मैटलैब में एक कार्यान्वयन प्रदान किया जिसे मैंने कभी काम नहीं किया है। – Farrel

+1

वैसे यह ggplot2 में छोटा है - बस 'geom = "घनत्व का उपयोग करें" ' – hadley

उत्तर

1

यहां ऐसा करने का एक मूल तरीका है। निस्संदेह अधिक जटिल विकल्प हैं, और ट्विक करने के लिए पैरामीटर हैं, लेकिन यह एक अच्छा प्रारंभिक बिंदु होना चाहिए।

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) 

आप इसे साजिश हैं, तो आप splines की दिलचस्प व्यवहार देख सकते हैं:

plot(newdates, money.per.day, type='l') 

enter image description here

+0

आपको बहुत धन्यवाद। जिस व्यक्ति ने मुझे इस तकनीक के बारे में बताया, मुझे सलाह दी गई कि संचयी राशि मौलिक रूप से बढ़ रही है मूल मूल्यों की गैर-नकारात्मकता। मैंने कहा कि मुझे लगता है कि स्पिनलाइन रैखिक होनी चाहिए या monotonic पर सेट होना चाहिए। मैं splinefun funciton का पता लगाऊंगा। लेकिन मुझे खुशी है कि आपने मुझे दिखाया कि व्युत्पन्न सूत्र के खिलाफ एक्स के समूह को कैसे फेंकना है। – Farrel

+0

@ फेरेल ग्रेट मुझे खुशी है कि यह मदद करता है। आपके जैसे विकल्पों का पता लगाने के लिए निश्चित रूप से स्मार्ट। Cumsum वास्तव में monotonically बढ़ रहा है ('प्लॉट (डीएफ) कोशिश करें; लाइनें (newdates, df.spline (newdates)) 'देखने के लिए), लेकिन आप चाहते थे कि पहली व्युत्पन्न (अर्थात। पैसा/दिन) नहीं है। जब आप एक लंबे महीने से एक छोटे से, आदि जाते हैं तो यह ऊपर और नीचे जाएगा .. –

+0

मैं इस कल के साथ खेलने जा रहा हूं। – Farrel