Skip to content

Commit

Permalink
More lines of code to help understand TOWT
Browse files Browse the repository at this point in the history
  • Loading branch information
simularis committed May 18, 2021
1 parent d594623 commit 2c832ca
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
36 changes: 36 additions & 0 deletions understand/understand_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,42 @@ print(regOut$bmod)
# * Also, model improvement suggestion: choose tempKnots via k-means cluster analysis.



## New stuff
# Dump all the regression coefficients to a CSV file.

#library(plyr)
library(stringr)
rol=Project[["model_obj_list"]][["models_list"]][["Data_pre_2_2.csv"]][["towt_model"]][["regOutList"]]
mylist=list()
for (irun in 1:length(rol)) {
cat(paste(rep("=",80),collapse=""),"\n")
cat(paste("Model run ",irun,"\n"))
cat(paste(rep("=",80),collapse=""),"\n")
print(summary.lm(rol[[irun]]$amod))


x=data.frame(t(rol[[irun]]$amod$coefficients))
x["_occ"]="occ"
y=data.frame(t(rol[[irun]]$bmod$coefficients))
y["_occ"]="unocc"
x["_irun"]=irun
y["_irun"]=irun
#newcoeffs=merge(x,y,all=T)
mylist=rbind(mylist,list(x),list(y))


}
newcoeffs=Reduce(function(x,y){merge(x,y,all=T)},mylist)
mycoeffs=newcoeffs[order(newcoeffs$`_irun`,newcoeffs$`_occ`),str_sort(names(newcoeffs),numeric=T)]
View(mycoeffs)
write.table(mycoeffs,file="mycoeffsfile.csv",sep=",",row.names=F)






## Clean up: remove global variables from workspace and detach scope
## Comment out these lines if you want to inspect further
rm(rds_file, isSavings, projectType, i, df, df2, tCenters, nModelRuns, irun, regOut)
Expand Down
30 changes: 26 additions & 4 deletions understand/understand_occupancy.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ TempF <- tempVecF[okload]
# Figure out which times of week a building is in one of two modes
# (called 'occupied' or 'unoccupied')
uTOW = unique(intervalOfWeek2)
# RMV2.0 does not sort this vector. Although that doesn't matter,
# I prefer to have it sorted now.
#uTOW = unique(intervalOfWeek2)
uTOW = sort(unique(intervalOfWeek2))
nTOW = length(uTOW)
# Define 'occupied' and 'unoccupied' based on a regression
Expand Down Expand Up @@ -289,7 +292,7 @@ ggplot() +
```{r}
# To repeat the same data in every panel, simply construct a data frame
# that does not contain the faceting variable.
mydata=data.frame(x=TempF,y=loadVec2,ypred=predict(amod),tow=intervalOfWeek2)
mydata=data.frame(x=TempF,y=loadVec2,ypred=predict(amod),tow=intervalOfWeek2,timeVec=timeVec)
mydata[mydata$tow<10,]
```

Expand Down Expand Up @@ -375,15 +378,23 @@ Here is how I export the data to the widget.
```{r}
# Convert to JSON for Chart.js
dataByTOW=uTOW %>% purrr::map(~ mydata[mydata$tow==.x,])
dataByTOWjson = jsonlite::toJSON(dataByTOW, pretty=F)
#fitLine = unique(mydata[with(mydata,order(x)),c('x','ypred')])
fitLine = data.frame(x=TempF,y=predict(amod)) %>% unique() %>% arrange(x)
# Here's how to save the data directly as a block of javascript code.
# This is a temporary hack to pass the data to a widget that is just hand-coded HTML.
dataByTOWjson = jsonlite::toJSON(dataByTOW, pretty=F)
fitLinejson = jsonlite::toJSON(fitLine, pretty=F)
sink('datafile.js')
cat(glue('const dataByTOW = {dataByTOWjson};
const fitLine = {fitLinejson};'))
sink()
#mydata %>% mutate(x=x,y=ypred)
# Here's how to store the data in native R objects.
# This is for testing the next version of the widget, using htmlwidgets package.
saveRDS(list(dataByTOW=dataByTOW, fitLine=fitLine, mydata=mydata), file="mydatafile.RDS")
```

Here is the widget. Try pointing the mouse over the time-of-week grid:
Expand All @@ -392,7 +403,18 @@ Here is the widget. Try pointing the mouse over the time-of-week grid:
* Horizontal: from midnight to midnight
* Collapsed on: week of the year

<iframe width="900" height="1200" src="./mychart3.html"></iframe>
Here is the old code to insert an iframe. It should not actually render one.

```
iframe width="900" height="1200" src="./mychart3.html" /iframe
```

```{r}
library(mywidget)
#occupancy_widget("Hello, globals!", dataByTOWjson, fitLinejson, 'json')
occupancy_widget("hello, solar system!",dataByTOW, fitLine, 'r', width='auto', height='auto')
```


That is all. Thank you for reading.
Find more from [my fork of RMV2.0](https://github.com/simularis/RMV2.0/tree/dev/demo)

0 comments on commit 2c832ca

Please sign in to comment.