<jupyter_start><jupyter_text>Welcome to day one of the 5-Day Regression Challenge! To get started, click the blue "fork notebook" button in the upper right hand corner. This will make a copy of this notebook that you can edit. To run cells once you've forked the notebook, click inside a code cell and then hit CTRL + ENTER (CMD + ENTER on a Mac).![The button to fork this notebook](https://image.ibb.co/kNKUZ5/Screenshot_from_2017_08_30_16_47_40.png)___ **What is regression?**Regression is one way of modeling the strength and direction of the relationship between a dependent or output variable (usually represented by y) and one or more independent or input variables (usually represented by x). It differs from correlation analysis because it allows you to predict the outcome for new input or inputs you haven’t seen yet.**What types of regression are there?**There are many different types of regression. The specific family of regressions we’ll be learning are called “generalized linear models”. The important thing for you to know is that with this family of models, you need to pick a specific type of regression you’re interested in. The type of regression will depend on what type of data you’re trying to predict.* **Linear**: When you’re predicting a continuous value. (What temperature will it be today?)* **Logistic**: When you’re predicting which category your observation is in. (Is this is a cat or a dog?)What is regression?* **Poisson**: When you’re predicting a count value. (How many dogs will I see in the park?)Today, we’re going to practice picking the right model for our dataset and plotting it. I will demonstrate first on one dataset, then you'll try your hand at another.___[**You can check out a video that goes with this notebook by clicking here.**](https://www.youtube.com/embed/UOj-GJAuvPM)  Example: Recipes____For our example, I'm going be working with a dataset of information on various recipes. I want to predict whether or not a recipe is a dessert based on how many calories it has. First, let's read in the library & datasets we'll need.<jupyter_code># library we'll need
library(tidyverse)

# read in all three datasets (you'll pick one to use later)
recpies <- read_csv("../input/epirecipes/epi_r.csv")
bikes <- read_csv("../input/nyc-east-river-bicycle-crossings/nyc-east-river-bicycle-counts.csv")
weather <- read_csv("../input/szeged-weather/weatherHistory.csv")

head(recpies)<jupyter_output>── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
✔ ggplot2 2.2.1.9000     ✔ purrr   0.2.4     
✔ tibble  1.3.4          ✔ dplyr   0.7.4     
✔ tidyr   0.7.2          ✔ stringr 1.2.0     
✔ readr   1.1.1.9000     ✔ forcats 0.2.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
Parsed with column specification:
cols(
  .default = col_double(),
  title = col_character()
)
See spec(...) for full column specifications.
Warning message:
“Missing column names filled in: 'X1' [1]”Parsed with column specification:
cols(
  X1 = col_double(),
  Date = col_datetime(format = ""),
  Day = col_datetime(format = ""),
  `High Temp (°F)` = col_double(),
  `Low Temp (°F)` = col_double(),
  Precipitation = col_character(),
  `Brooklyn Bridge` = col_double(),
  `Manhattan Bridge` = col_double(),
  `Williamsburg Bridge` = col_double(),
  `Queensboro Bridge` = col_[...]<jupyter_text>I'm going to do a little bit of data cleaning. I know that this has some really outrageous outliers, so I want to ignore any recipe with more than ten thousand calories. I also know that there are a lot of missing values, so I'm going to remove them. (You shouldn't) need to do this with the other datasets.<jupyter_code># quickly clean our dataset
recpies <- recpies %>%
    filter(calories < 10000) %>% # remove outliers
    na.omit() # remove rows with NA values<jupyter_output><empty_output><jupyter_text>So I already have my question, "Can I predict whether or not a recipe is a dessert based on how many calories it has?". (With your dataset you'll need to come up with your question by picking one variable to predict and one to use to predict it).In order to build a model, I need to know what type of variable whether or not a recipe is a dessert is. There are three choices:* **Continuous**. These are numeric variables that can take any value, including decimals. Examples include temperatures or prices.* **Count**. These are numeric variables that can only take positive integer values. Examples include the number of people at a party or the number of cars in a parking lot.* **Categorical**. These are not numbers. Categorical variables tell you whether or not an observation is part of a specific group. Good examples include gender or whether someone owns more than ten hats.The best way to figure out which one is a variable is is to use your knowledge of a dataset and the world in general. For example, I know that whether or not a recipie is a dessert is a *categorical* variable, while the number of calories in a dessert is probably a *count* variable (unless you have a very, very accurate measure of calories, in which case it might be continuous).There are some quick tests you can do to double-check, though. For example, I might want to know if the ratings for a recipie are categorical, count, or continuous. First, I can check if they're numeric:<jupyter_code># are the ratings all numeric?
print("Is this variable numeric?")
is.numeric(recpies$rating)<jupyter_output>[1] "Is this variable numeric?"<jupyter_text>So I know it's represented by a numeric variable in this dataframe. This means that ratings are probably not things like "very good" or "very bad".Next, I can check if all the values are integers (whole numbers). If so, it's probably a count variable. If not, it's probably continuous.<jupyter_code># are the ratings all integers?
print("Is this variable only integers?")
all.equal(recpies$rating, as.integer(recpies$rating)) == T<jupyter_output>[1] "Is this variable only integers?"<jupyter_text>So, in this dataset, ratings are a continuous variable. It's a number that can have decimal values. However, I'm not interested in predicting ratings. I'm interested in predicting if something is a dessert or not based on how many calories it has. To do this, I can plot the number of calories by whether or not the recipe is for a dessert.<jupyter_code># plot calories by whether or not it's a dessert
ggplot(recpies, aes(x = calories, y = dessert)) + # draw a 
    geom_point()  # add points<jupyter_output><empty_output><jupyter_text>Since whether or not something is a dessert is a categorical variable, we want to use logistic regression to answer this question. As you can see in the chart below, the link function for logistic regression is "binomial".___![Regression guide](https://image.ibb.co/ducqSw/regression_guide.png)___We can fit and plot a regression line to our data using the geom_smooth() layer. To make sure we do logistic regression, we need to make sure that we tell geom_smooth to fit a regression model from the binomial family, like so:        geom_smooth(method = "glm", method.args = list(family = "binomial"))  If we were fitting a linear regression model instead, for example, we'd ask geom_smooth() to fit a model from the gaussian family, like so:        geom_smooth(method = "glm", method.args = list(family = "gaussian"))    geom_smooth() relies on the x and y arguments that you pass to ggplot (in the aes() argument), so you want to make sure you put the value you're interested in predicting in the y argument slot. (Here, that's "dessert".)<jupyter_code># plot & add a regression line
ggplot(recpies, aes(x = calories, y = dessert)) + # draw a 
    geom_point() + # add points
    geom_smooth(method = "glm", # plot a regression...
    method.args = list(family = "binomial")) # ...from the binomial family<jupyter_output><empty_output><jupyter_text>That blue line we've plotted is actually a regression model! Looking at the regression line, we can tell that as the number of calories in a recipe increases, it's more likely that that recipe is a dessert. (We can tell this because the line moves towards 1, which means "is a dessert" and away from 0, which means "isn't a dessert"). The grey shape behind it is the standard error. Looking at the error, we can tell that we're pretty sure about our prediction when there's fewer calories (towards the left side of the graph), but as we move to the right we get less certain and our error increases.We'll learn more about fitting and interpreting models tomorrow, but for now it's time for you to try your own hand at picking the right type of model and fitting it.  Your turn!___![Regression guide](https://image.ibb.co/ducqSw/regression_guide.png)A quick guide to the three types of regression we've talked about.___Now it's your turn to come up with a question, pick the right model for your data and plot it.1. Pick one of the two datasets ("weather" or "bikes", your choice! You can find out more about these datasets by expanding the "Input" section at the very top of this notebook.)2. Identify which variables are continuous, categorical and count using the dataset documentation. (You can also check out a summary of the dataset using summary() or str())3. Pick a variable to predict and one varaible to use to predict it    * For this challange, if you're picking a categorical value, I'd recommend choosing one with only two possible categories (like dessert or not dessert)4. Plot your two variables5. Use "geom_smooth" and the appropriate family to fit and plot a model6. Optional: If you want to share your analysis with friends or to ask for help, you’ll need to make it public so that other people can see it.  * Publish your kernel by hitting the big blue “publish” button. (This may take a second.)  * Change the visibility to “public” by clicking on the blue “Make Public” text (right above the “Fork Notebook” button).  * Tag your notebook with 5daychallenge<jupyter_code># your work goes here! :)
head(weather)
summary(weather)
names(weather)[names(weather) == "Temperature (C)"] <- "Temperature"
weather %>%
    ggplot(aes(Humidity, Temperature)) +
    geom_point(alpha=0.2, pch=4) +
    geom_smooth(method="loess", method.args=list(family="gaussian"))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08621187337631098, 'fraction_numerical': 0.0071201770422399694, 'mean_word_length': 4.558288770053476, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 29, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '310769', 'license': 'MIT', 'n_tokens_mistral': 2991, 'n_tokens_neox': 2774, 'n_words': 1479, 'path': 'ML/Regression/Kaggle - 5 day challenge/Regression/day1.ipynb', 'repo_name': 'AravindVasudev/Machine-Learning-Playground', 'size': '10393'}