Skip to content

Instantly share code, notes, and snippets.

@lmcstro
Created February 28, 2018 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmcstro/3de7d1da5962f45ef578233a661d7a9c to your computer and use it in GitHub Desktop.
Save lmcstro/3de7d1da5962f45ef578233a661d7a9c to your computer and use it in GitHub Desktop.
an experiment in GIST for R
# select a particlar car and day to examine
x.journey.number <- 4
x.day <- "Monday"
# Big look to process all the records
#for (x.journey.number in 1:length(l.journeys)) {
#} # end big loop
df.x <- read.csv(paste(x.results.dir,"pattern_",l.journeys[x.journey.number]
,sep = ""), stringsAsFactors = FALSE)
# find the max cluster number in the data frame
x.max.cluster <- max(c(max(df.x$from),max(df.x$to)))
# initialize a matrix to calculate frequency and probability
a.x <- array(rep(0,
(x.max.cluster)*(x.max.cluster)),
dim=c(x.max.cluster,x.max.cluster))
# calculate the journeys, and add a nominal amount to allow for some uncertainty
# when there is 0 journeys (since we are not certain there are NEVER any journeys
# in between location that in our sample reported as 0)
for (j in 1:x.max.cluster) {
for (k in 1:x.max.cluster) {
a.x[j,k] <- df.x[df.x$from == j &
df.x$to == k &
df.x$day == x.day,"journeys"] + 0.1
}
}
# add some row and column names
rownames(a.x) <- rownames(a.x, do.NULL = FALSE, prefix = "From.")
colnames(a.x) <- colnames(a.x, do.NULL = FALSE, prefix = "To.")
# add the marginal sums
a.x <- rbind(a.x,colSums(a.x))
a.x <- cbind(a.x,rowSums(a.x))
# great a marginal probability estimate, based on normalized observed frequency
b.x <- round(a.x/a.x[x.max.cluster+1,x.max.cluster+1],3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment