# R - Logistic Regression

The general mathematical equation for logistic regression is −

y = 1/(1+e^-(a+b1x1+b2x2+b3x3+...))Following is the description of the parameters used −

**y**is the response variable.**x**is the predictor variable.**a**and**b**are the coefficients which are numeric constants.

**glm()**function.

## Syntax

The basic syntax for**glm()**function in logistic regression is −

glm(formula,data,family)Following is the description of the parameters used −

**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 binomial for logistic regression.

## Example

The in-built data set "mtcars" describes different models of a car with their various engine specifications. In "mtcars" data set, the transmission mode (automatic or manual) is described by the column am which is a binary value (0 or 1). We can create a logistic regression model between the columns "am" and 3 other columns - hp, wt and cyl.# Select some columns form mtcars. input <- mtcars[,c("am","cyl","hp","wt")] print(head(input))When we execute the above code, it produces the following result −

am cyl hp wt Mazda RX4 1 6 110 2.620 Mazda RX4 Wag 1 6 110 2.875 Datsun 710 1 4 93 2.320 Hornet 4 Drive 0 6 110 3.215 Hornet Sportabout 0 8 175 3.440 Valiant 0 6 105 3.460

## Create Regression Model

We use the**glm()**function to create the regression model and get its summary for analysis.

input <- mtcars[,c("am","cyl","hp","wt")] am.data = glm(formula = am ~ cyl + hp + wt, data = input, family = binomial) print(summary(am.data))When we execute the above code, it produces the following result −

Call: glm(formula = am ~ cyl + hp + wt, family = binomial, data = input) Deviance Residuals: Min 1Q Median 3Q Max -2.17272 -0.14907 -0.01464 0.14116 1.27641 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 19.70288 8.11637 2.428 0.0152 * cyl 0.48760 1.07162 0.455 0.6491 hp 0.03259 0.01886 1.728 0.0840 . wt -9.14947 4.15332 -2.203 0.0276 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 43.2297 on 31 degrees of freedom Residual deviance: 9.8415 on 28 degrees of freedom AIC: 17.841 Number of Fisher Scoring iterations: 8

### Conclusion

In the summary as the p-value in the last column is more than 0.05 for the variables "cyl" and "hp", we consider them to be insignificant in contributing to the value of the variable "am". Only weight (wt) impacts the "am" value in this regression model.

*Table of contents:*1. R - Overview

2. R - Environment Setup

3. R - Basic Syntax

4. R - Data Types

5. R - Variables

6. R - Operators

7. R - Decision Making

8. R - Loops

9. R - Functions

10. R - Strings

11. R - Vectors

12. R - Matrices

13. R - Arrays

14. R - Factors

15. R - Data Frames

16. R - Packages

17. R - Data Reshaping

18. R - CSV Files

19. R - Excel Files

20. R - Binary Files

21. R - XML Files

22. R - JSON Files

23. R - Web Data

24. R - Database

25. R - Pie Charts

26. R - Bar Charts

27. R - Boxplots

28. R - Histograms

29. R - Line Graphs

30. R - Scatterplots

31. R - Mean, Median and Mode

32. R - Linear Regression

33. R - Multiple Regression

34. R - Logistic Regression

35. R - Normal Distribution

36. R - Binomial Distribution

37. R - Poisson Regression

38. R - Analysis of Covariance

39. R - Time Series Analysis

40. R - Nonlinear Least Square

41. R - Decision Tree

42. R - Random Forest

43. R - Survival Analysis

44. R - Chi Square Tests

## No comments:

## Post a Comment