How to set the coefficient of one variable to 1 for logistic regression model in R?

R ProgrammingServer Side ProgrammingProgramming

To set the coefficient of one variable to 1 for logistic regression model, we can use offset function.

For example, if we have a data frame called df that contains a binary column say y and three independent variables say x1, x2, and x3 and we want to create a logistic regression model with x1 coefficient equal to 1 then we can use the below given command −

glm(y~x2+x3,offset=x1,data=df,family=binomial)

Example 1

Following snippet creates a sample data frame −

y1<-sample(0:1,20,replace=TRUE)
iv1<-rpois(20,2)
iv2<-rpois(20,2)
iv3<-rpois(20,5)
df1<-data.frame(y1,iv1,iv2,iv3)
df1

Output

The following dataframe is created −

  y1 iv1 iv2 iv3
1  1  1  5   8
2  1  2  3   6
3  1  2  3  11
4  1  2  1   5
5  1  2  2   2
6  1  2  1   2
7  1  3  2   1
8  1  1  2   5
9  1  0  1  10
10 0  0  0   5
11 1  0  3   5
12 1  2  4   1
13 1  4  5  11
14 0  3  2   2
15 0  1  1   3
16 1  1  3   1
17 0  1  2   7
18 1  1  3   8
19 1  3  6   9
20 1  6  3   9

In order to create logistic regression model with coefficient of x1 set to 1, add the following code to the above snippet −

y1<-sample(0:1,20,replace=TRUE)
iv1<-rpois(20,2)
iv2<-rpois(20,2)
iv3<-rpois(20,5)
df1<-data.frame(y1,iv1,iv2,iv3)
Model_1<-glm(y1~iv1+iv2,offset=iv3,data=df1,family=binomial)
summary(Model_1)

Output

If you execute all the above given snippets as a single program, it generates the following output −

Call:
glm(formula = y1 ~ iv1 + iv2, family = binomial, data = df1,
offset = iv3)

Deviance Residuals:
   Min    1Q       Median     3Q    Max
-2.95608 0.00035 0.04659 0.45252 1.67587

Coefficients:
            Estimate Std. Error  z value Pr(>|z|)
(Intercept) -7.6349  2.4801    -3.078    0.00208 **
iv1          1.3445  0.9276     1.449    0.14723
iv2          1.8234  0.9462     1.927    0.05396 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 31.099 on 19 degrees of freedom
Residual deviance: 19.741 on 17 degrees of freedom
AIC: 25.741

Number of Fisher Scoring iterations: 7

Example 2

Following snippet creates a sample data frame −

Response<-sample(0:1,20,replace=TRUE)
Rate<-sample(1:10,20,replace=TRUE)
Score<-sample(0:9,20,replace=TRUE)
df2<-data.frame(Response,Rate,Score)
df2

The following dataframe is created −

Response Rate Score
1  0     8    9
2  1     7    6
3  1     2    9
4  0     8    9
5  0     9    8
6  0     1    2
7  1     3    9
8  1     7    2
9  0     7    2
10 0    10    5
11 0     3    9
12 0     7    8
13 1     3    6
14 1     7    5
15 0     2    8
16 1     8    9
17 1     7    9
18 1     6    8
19 0     4    4
20 0     8    7

In order to create logistic regression model with coefficient of Rate set to 1, add the following code to the above snippet −

Response<-sample(0:1,20,replace=TRUE)
Rate<-sample(1:10,20,replace=TRUE)
Score<-sample(0:9,20,replace=TRUE)
df2<-data.frame(Response,Rate,Score)
Model_2<-glm(Response~Score,offset=Rate,data=df2,family=binomial)
summary(Model_2)

Output

If you execute all the above given snippets as a single program, it generates the following output −

Call:
glm(formula = Response ~ Score, family = binomial, data = df2,
offset = Rate)

Deviance Residuals:
   Min     1Q      Median 3Q    Max
-2.5313 -1.4891 -0.1062 1.1435 2.9733

Coefficients:
            Estimate Std. Error  z value   Pr(>|z|)
(Intercept) -7.3748       1.6822  -4.384   1.16e-05 ***
Score        0.1074        0.2432   0.442   0.659
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 55.769 on 19 degrees of freedom
Residual deviance: 55.571 on 18 degrees of freedom
AIC: 59.571

Number of Fisher Scoring iterations: 4
raja
Updated on 05-Nov-2021 08:12:35

Advertisements