Skip to content

Instantly share code, notes, and snippets.

@gghatano
Created June 15, 2015 21:35
Show Gist options
  • Save gghatano/64568fab7971486e3b70 to your computer and use it in GitHub Desktop.
Save gghatano/64568fab7971486e3b70 to your computer and use it in GitHub Desktop.
Rmd
dummiesでダミー変数を作る
========================================================
```{r}
library(data.table)
library(dplyr)
library(dummies)
dat = fread("data.csv")
dat %>% head
```
```{r}
dat.dummy = dat %>%
mutate(hour = hour(datetime)) %>%
mutate(hour = as.factor(hour)) %>%
mutate(season = as.factor(season)) %>%
mutate(holiday = as.factor(holiday)) %>%
mutate(workingday = as.factor(workingday)) %>%
mutate(weather = as.factor(weather)) %>%
select(-datetime) %>%
dummy.data.frame()
library(glmnet)
dat.dummy.mat =
dat.dummy %>%
select(-hour23, -weather4, -holiday1, -workingday1, -season4) %>%
select(-casual, -registered, -count) %>% as.matrix
dat.dummy.count =
dat.dummy %>% select(count) %>% unlist
dat.dummy.mat %>% head
glm_res =
glmnet(dat.dummy.mat, dat.dummy.count, family = "poisson", alpha = 0.1)
glm_res
predict(glm_res, dat.dummy.mat)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment