"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

June 15, 2016

Day #26 - R - Moving Weighted Average

Example code based on two day workshop on Azure ML module. Simple example storing and accessing data from Azure workspace


computeAverage<-function(Inputdate)
{
print(Inputdate)
setwd("E:/RNotes/RData/")
TrafficData <- read.csv(file = "Test3.txt")
head(TrafficData)
TrafficData$BusinessDate1 = as.Date(TrafficData$BusinessDate, format = "%Y/%m/%d")
x = as.Date(Inputdate)-7
a = TrafficData[TrafficData$BusinessDate1==x,]$TrafficCount
y = as.Date(x)-14
b = TrafficData[TrafficData$BusinessDate1==y,]$TrafficCount
z = as.Date(x)-21
c = TrafficData[TrafficData$BusinessDate1==z,]$TrafficCount
wt <- c(.25,.50,.25)
x <- c(c,b,a)
print('mean')
print(mean(x))
xm <- weighted.mean(x, wt)
xm
print('weighted mean')
print(xm)
return(xm)
}
m=computeAverage('2016-01-29')
print(m)
#Save Dataset from below configuration
#SiteName,BusinessDate,DayofWeek,HourofWeek,TrafficCount
#Site20,2016/01/01,Monday,14,59
#Site20,2016/01/08,Monday,14,19
#Site20,2016/01/15,Monday,14,84
#Site20,2016/01/22,Monday,14,31
library("AzureML")
computeAverage<-function(Inputdate,TrafficData)
{
head(TrafficData)
TrafficData$BusinessDate1 = as.Date(TrafficData$BusinessDate, format = "%Y/%m/%d")
x = as.Date(Inputdate)-7
a = TrafficData[TrafficData$BusinessDate1==x,]$TrafficCount
y = as.Date(x)-14
b = TrafficData[TrafficData$BusinessDate1==y,]$TrafficCount
z = as.Date(x)-21
c = TrafficData[TrafficData$BusinessDate1==z,]$TrafficCount
wt <- c(.25,.50,.25)
x <- c(c,b,a)
print('mean')
print(mean(x))
xm <- weighted.mean(x, wt)
xm
print('weighted mean')
print(xm)
return(xm)
}
ws <- workspace(
id = "c54ad6b088114cd5aef68ebc8dedb1d0",
auth = "eede31e0b9f5463a8c3fcd59a6156a1f",
api_endpoint = "https://studioapi.azureml.net"
)
TrafficData <- download.datasets(
dataset = ws,
name = "TrafficDataset"
)
m=computeAverage('2016-01-29',TrafficData)
print(m)

Happy Learning!!!

No comments: