Fitting Linear Models to the Data Set in R Programming - glm() Function Last Updated : 20 Jul, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report glm() function in R Language is used to fit linear models to the dataset. Here, glm stands for a generalized linear model. Syntax: glm(formula)Parameters: formula: specified formula Example 1: Python3 # R program to illustrate # glm function # R growth of orange trees dataset Orange # Putting age, Tree, circumference into the R search path attach(Orange) # Calling glm() function g <- glm(circumference ~ age + Tree) g Output: Tree age circumference 1 1 118 30 2 1 484 58 3 1 664 87 4 1 1004 115 5 1 1231 120 6 1 1372 142 7 1 1582 145 8 2 118 33 9 2 484 69 10 2 664 111 11 2 1004 156 12 2 1231 172 13 2 1372 203 14 2 1582 203 15 3 118 30 16 3 484 51 17 3 664 75 18 3 1004 108 19 3 1231 115 20 3 1372 139 21 3 1582 140 22 4 118 32 23 4 484 62 24 4 664 112 25 4 1004 167 26 4 1231 179 27 4 1372 209 28 4 1582 214 29 5 118 30 30 5 484 49 31 5 664 81 32 5 1004 125 33 5 1231 142 34 5 1372 174 35 5 1582 177 Call: glm(formula = circumference ~ age + Tree) Coefficients: (Intercept) age Tree.L Tree.Q Tree.C Tree^4 17.3997 0.1068 39.9350 2.5199 -8.2671 -4.6955 Degrees of Freedom: 34 Total (i.e. Null); 29 Residual Null Deviance: 112400 Residual Deviance: 6754 AIC: 297.5 Example 2: Python3 # R program to illustrate # glm function # Initializing some vectors A <- c(0, 1, 2, 3) B <- c(2, 4, 6, 8) Y <- c(0.1, 0.2, 0.3, 0.4) # Creating data from my_data <- data.frame(A, B, Y) # Putting above data into the R search path attach(my_data) # Calling glm() function x <- glm( Y ~ A + B + A * B) x Output: Call: glm(formula = Y ~ A + B + A * B) Coefficients: (Intercept) A B A:B 1.000e-01 1.000e-01 NA 1.418e-17 Degrees of Freedom: 3 Total (i.e. Null); 1 Residual Null Deviance: 0.05 Residual Deviance: 2.542e-32 AIC: -277.2 The following objects are masked _by_.GlobalEnv: A, B, Y Comment More infoAdvertise with us Next Article Fitting Linear Models to the Data Set in R Programming - glm() Function K Kanchan_Ray Follow Improve Article Tags : R Language R DataFrame-Function Similar Reads Perform Linear Regression Analysis in R Programming - lm() Function lm() function in R Language is a linear model function, used for linear regression analysis. Syntax: lm(formula) Parameters: formula: model description, such as x ~ y Example 1: Python3 # R program to illustrate # lm function # Creating two vectors x and y x <- c(rep(1:20)) y <- x * 2 # Callin 1 min read How to Use lm() Function in R to Fit Linear Models? In this article, we will learn how to use the lm() function to fit linear models in the R Programming Language. A linear model is used to predict the value of an unknown variable based on independent variables. It is mostly used for finding out the relationship between variables and forecasting. The 4 min read Model predictions to find the best model fit using the juice() and bake() functions in R The juice() function in R Programming Language is used to extract the data from a recipe object. It is a part of the recipes package. juice() FunctionA recipe object is a data structure that represents a pre-processing pipeline. It can be used to transform data in a consistent way. The juice() funct 11 min read How to Extract a p-value When Performing anova() Between Two glm Models in R Generalized Linear Models (glm) are widely used in R for modeling relationships between variables when the dependent variable is not normally distributed. Once two or more models are fitted, we often want to compare them to determine which model is a better fit. The anova() function in R allows us t 4 min read How to Extract the Intercept from a Linear Regression Model in R Linear regression is a method of predictive analysis in machine learning. It is basically used to check two things: If a set of predictor variables (independent) does a good job predicting the outcome variable (dependent).Which of the predictor variables are significant in terms of predicting the ou 4 min read Like