Skip to content

Commit

Permalink
Update yhat stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rdpeng committed May 2, 2014
1 parent 07bee3e commit 6858d93
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
40 changes: 10 additions & 30 deletions 09_DevelopingDataProducts/yhat/AP_example.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
## Read in the 2013 Annual Data

d <- read.csv("annual_all_2013.csv", nrow = 68210)

sub <- subset(d, Parameter.Name %in% c("PM2.5 - Local Conditions", "Ozone")
& Pullutant.Standard %in% c("Ozone 8-Hour 2008", "PM25 Annual 2006"),
c(State.Code, County.Code, Site.Num, Longitude, Latitude, Parameter.Name, Arithmetic.Mean, County.Name, City.Name, Pullutant.Standard))
c(Longitude, Latitude, Parameter.Name, Arithmetic.Mean))

pollavg <- aggregate(sub[, "Arithmetic.Mean"],
sub[, c("Longitude", "Latitude", "Parameter.Name")],
Expand All @@ -21,40 +20,15 @@ names(pollavg)[4] <- "level"
## Remove unneeded objects
rm(d, sub)

## library(fields)
rdist <- function (x1, x2, miles = TRUE, R = NULL) {
if (is.null(R)) {
if (miles)
R <- 3963.34
else R <- 6378.388
}
coslat1 <- cos((x1[, 2] * pi)/180)
sinlat1 <- sin((x1[, 2] * pi)/180)
coslon1 <- cos((x1[, 1] * pi)/180)
sinlon1 <- sin((x1[, 1] * pi)/180)
if (missing(x2)) {
pp <- cbind(coslat1 * coslon1, coslat1 * sinlon1, sinlat1) %*%
t(cbind(coslat1 * coslon1, coslat1 * sinlon1, sinlat1))
return(R * acos(ifelse(abs(pp) > 1, 1 * sign(pp), pp)))
}
else {
coslat2 <- cos((x2[, 2] * pi)/180)
sinlat2 <- sin((x2[, 2] * pi)/180)
coslon2 <- cos((x2[, 1] * pi)/180)
sinlon2 <- sin((x2[, 1] * pi)/180)
pp <- cbind(coslat1 * coslon1, coslat1 * sinlon1, sinlat1) %*%
t(cbind(coslat2 * coslon2, coslat2 * sinlon2, sinlat2))
return(R * acos(ifelse(abs(pp) > 1, 1 * sign(pp), pp)))
}
}

## Write function
monitors <- data.matrix(pollavg[, c("Longitude", "Latitude")])

library(fields)

pollutant <- function(df) {
x <- data.matrix(df[, c("lon", "lat")])
r <- df$radius
d <- rdist(monitors, x)
d <- rdist.earth(monitors, x)
use <- lapply(seq_len(ncol(d)), function(i) {
which(d[, i] < r[i])
})
Expand All @@ -65,8 +39,14 @@ pollutant <- function(df) {
data.frame(df, dlevel)
}

## Send to yhat

library(yhatr)

model.require <- function() {
library(fields)
}

model.transform <- function(df) {
df
}
Expand Down
11 changes: 9 additions & 2 deletions 09_DevelopingDataProducts/yhat/ozone.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ model.require <- function() {
}

model.transform <- function(df) {
df
transform(df, Wind = as.numeric(as.character(Wind)),
Temp = as.integer(as.character(Temp)))
}

model.predict <- function(df) {
data.frame(Ozone = predict(fit, newdata = df))
result <- data.frame(Ozone = predict(fit, newdata = df))
cl <- data.frame(clWind = class(df$Wind), clTemp = class(df$Temp))
data.frame(result, Temp = as.character(df$Temp),
Wind = as.character(df$Wind), cl)
}

fit <- lm(Ozone ~ Wind + Temp, data = airquality)
Expand All @@ -22,3 +26,6 @@ yhat.config <- c(


yhat.deploy("ozone")


## yhat.predict("ozone", data.frame(Wind = 9.7, Temp = 67))
2 changes: 1 addition & 1 deletion 09_DevelopingDataProducts/yhat/predictozone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ echo $2

curl -X POST -H "Content-Type: application/json" \
--user [email protected]:90d2a80bb532cabb2387aa51ac4553cc \
--data '{"Wind": $1, "Temp": $2 }' \
--data '{"Wind": "$1", "Temp": "$2" }' \
http://sandbox.yhathq.com/[email protected]/models/ozone/

0 comments on commit 6858d93

Please sign in to comment.