# R语言 逻辑回归

```y = 1/(1+e^-(a+b1x1+b2x2+b3x3+...))
```

• y是响应变量。

• x是预测变量。

• ab是作为数字常数的系数。

## 语法

```glm(formula,data,family)
```

• formula是表示变量之间的关系的符号。

• data是给出这些变量的值的数据集。

• family是R语言对象来指定模型的细节。 它的值是二项逻辑回归。

## 例

```# Select some columns form mtcars.
input <- mtcars[,c("am","cyl","hp","wt")]

```

```                  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
```

## 创建回归模型

```input <- mtcars[,c("am","cyl","hp","wt")]

am.data = glm(formula = am ~ cyl + hp + wt, data = input, family = binomial)

print(summary(am.data))
```

```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
```

## 您可能还喜欢：

App下载
{"ft_lang_label":"__label__zh","ft_lang_prob":0.6180847,"math_prob":0.9900718,"size":1702,"snap":"2019-51-2020-05","text_gpt3_token_len":980,"char_repetition_ratio":0.07538281,"word_repetition_ratio":0.04587156,"special_character_ratio":0.40716803,"punctuation_ratio":0.1898017,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96460724,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-10T06:09:13Z\",\"WARC-Record-ID\":\"<urn:uuid:36ff5f9a-16e6-4837-98dc-d4c0c3e22eb3>\",\"Content-Length\":\"48245\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:43561eba-7422-4624-918a-0d32b3816385>\",\"WARC-Concurrent-To\":\"<urn:uuid:723a930b-43e6-4e60-9d9d-07808eb2ef7c>\",\"WARC-IP-Address\":\"120.79.88.157\",\"WARC-Target-URI\":\"https://www.w3cschool.cn/r/r_logistic_regression.html\",\"WARC-Payload-Digest\":\"sha1:LRIVGWX2SP2CCTVVUSASBARQ34SZAMZ2\",\"WARC-Block-Digest\":\"sha1:ILMH67ZQ3MT2FOCLOG3P3F2LE7KNXKTO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540525821.56_warc_CC-MAIN-20191210041836-20191210065836-00541.warc.gz\"}"}