Friday, 30 November 2018

Poisson Regression in R Language

Poisson Regression involves regression models in which the response variable is in the form of counts and not fractional numbers. For example, the count of number of births or number of wins in a football match series. Also the values of the response variables follow a Poisson distribution.

The general mathematical equation for Poisson regression is -


log(y) = a + b1x1 + b2x2 + bnxn ........

Following is the description of the parameters used -

       y is the response variable.
       a and b are the numeric coefficients.
       x is the predictor variables.

The function used to create the Poisson regression model is the glm( ) function.

Syntax

The basic syntax for glm( ) function in Poisson regression is -


glm( formula, data, family)

Following is the description of the parameters used in above followings -
  • formula is the symbol presenting the relationship between the variables.
  • data is the data set giving the values of these variables.
  • family is R object to specify the details of the model. It's value is 'Poisson' for Logistic Regression. 
Example

We have the in-built data set "warpbreaks" which describes the effect of wool type (A or B) and tension (low, medium or high) on the number of wrap breaks per loom. Let's consider "breaks" as the response variable which is a count of number of breaks. The wool "type" and "tension" are taken  as predictor variables.

INPUT DATA

input <-  warpbreaks
 print (head(input) )


When we execute the above code, it producers the following result -

              breaks            wool              tension

1              26                   A                     L

2              30                   A                     L

3              54                   A                     L

4              25                   A                     L

5              70                   A                     L

6              52                   A                     L


Create Regression Model

 output <- glm( formula = breaks ~ wool + tension,
                                  data = warpbreaks,
                                family = poisson)
print (summary (output) )


When we execute the above code, it produces the following result - 


Call :
glm( formula = breaks ~ wool + tension, family = poisson, data = warpbreaks)

Deviance Residuals :

 Min           1Q          Median          3Q         Max
-3.6871   -1.6503      -0.4269        1.1902    4.2616

Coefficients :
                        Estimate Std. Error z value Pr (> | z |)
(Intercept)   3.69196     0.04541    81.302   < 2e-16     ***
woolB        -0.020599   0.05157    -3.994  6.49e-05     ***
tensionM    -0.32132    0.06027    -5.332   9.73e-08     ***
tensionH    -0.51849     0.06396    -8.107   5.21e-16     ***
---
Signif.  codes:  0  '***'  0.001  '**'  0.01 '*'  0.05 '.'  0.1 '  '  1

(Dispersion parameter for poisson family taken to be 1 )

       Null deviance: 297.37  on 53  degrees of freedom
Residual deviance: 210.39  on 50  degrees of freedom
AIC:  493.05

Number of Fisher Scoring iterations: 4



In the summary we look for the p-value in the last column to be less than 0.05 to consider an impact of the predictor variable on the response variable. As seen the wooltype B having tension type M and H have impact on the count of breaks.

0 Comments:

Post a Comment

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (893) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses