Package glm.predict
glm.predict is an R package to calculate discete changes and predicted probabilities for glm(), glm.nb(), polr() and mutinom() models in R.
Instructions
The package (Version 4.1-0) can be installed in R GNU from CRAN with install.packages(“glm.predict”).
Installation from Github (sometimes never version, current: 4.1-0)
Now you can use the package after calling library(glm.predict).
Examples:
Logit
model1 = glm(Sex ~ Height + Smoke + Pulse, data=MASS::survey, family=binomial(link=logit))
predicts(model1, "150-190,20;F;mean", position = 1)
Output:
val1_mean val1_lower val1_upper val2_mean val2_lower val2_upper dc_mean dc_lower dc_upper Height_val1 Height_val2 Smoke Pulse
1 0.003754422 6.416653e-05 0.02347907 0.2205801 0.02386385 0.6755021 -0.2168256 -0.6564188 -0.02383041 150 170 Heavy 73.91765
2 0.230413362 2.299203e-02 0.67612521 0.9483000 0.74619052 0.9973208 -0.7178866 -0.9046614 -0.31965927 170 190 Heavy 73.91765
3 0.005345389 6.704097e-04 0.01793030 0.3805691 0.26853635 0.4944005 -0.3752237 -0.4846952 -0.26699094 150 170 Never 73.91765
4 0.380840374 2.714908e-01 0.49181163 0.9870918 0.95660990 0.9979539 -0.6062514 -0.7128564 -0.49470534 170 190 Never 73.91765
5 0.004514978 2.684470e-04 0.02415392 0.3227317 0.09511918 0.6451244 -0.3182167 -0.6308845 -0.09435875 150 170 Occas 73.91765
6 0.320307791 8.985924e-02 0.63693113 0.9775326 0.90781972 0.9979703 -0.6572248 -0.8651870 -0.36084935 170 190 Occas 73.91765
7 0.022051902 7.331628e-04 0.13345632 0.6037829 0.17968510 0.9314196 -0.5817310 -0.8623370 -0.17817883 150 170 Regul 73.91765
8 0.607473580 1.794164e-01 0.93276376 0.9926966 0.96210660 0.9996929 -0.3852230 -0.7868720 -0.06623296 170 190 Regul 73.91765
Ordinal Logit
data = MASS::survey
data$Smoke = ordered(MASS::survey$Smoke,levels=c("Never","Occas","Regul","Heavy"))
model1 = MASS::polr(Smoke ~ Sex + Height, data=data, Hess=TRUE)
predicts(model1, "0-1;160-190,10", position = 1)
Output:
val1_mean val1_lower val1_upper val2_mean val2_lower val2_upper dc_mean dc_lower dc_upper Sex_val1 Sex_val2 Height level
1 0.86189124 0.78350953 0.92487189 0.84422806 0.67346409 0.94455508 0.017663183 -0.08002334 0.16668471 0 1 160 Never
2 0.05755684 0.02758524 0.10091440 0.06309025 0.02054631 0.12792508 -0.005533413 -0.05489999 0.03249771 0 1 160 Occas
3 0.04650977 0.02076754 0.08307244 0.05302636 0.01468784 0.12330033 -0.006516590 -0.06150231 0.02887024 0 1 160 Regul
4 0.03404214 0.01394501 0.06563210 0.03965532 0.01015508 0.10259902 -0.005613180 -0.05506648 0.02355552 0 1 160 Heavy
5 0.82361963 0.73735589 0.89190772 0.81055102 0.70779431 0.89591321 0.013068607 -0.11471452 0.15461203 0 1 170 Never
6 0.07160838 0.03606365 0.11443818 0.07594783 0.03601350 0.12521659 -0.004339450 -0.05438045 0.03963146 0 1 170 Occas
7 0.05988414 0.02616595 0.10703945 0.06463207 0.02580184 0.12366123 -0.004747925 -0.05972496 0.04475614 0 1 170 Regul
8 0.04488785 0.02002333 0.08897468 0.04886908 0.01959936 0.09672055 -0.003981232 -0.04662336 0.04025011 0 1 170 Heavy
9 0.77816143 0.62533833 0.89323496 0.76749554 0.68851622 0.83806735 0.010665883 -0.16204560 0.15364828 0 1 180 Never
10 0.08359698 0.03638965 0.14554327 0.08845416 0.04913634 0.13511383 -0.004857178 -0.05276956 0.04333923 0 1 180 Occas
11 0.07821059 0.02739384 0.15436096 0.08168649 0.03953441 0.13258659 -0.003475902 -0.05613698 0.06557512 0 1 180 Regul
12 0.06003101 0.02158756 0.13857642 0.06236381 0.03008886 0.11053993 -0.002332803 -0.05104203 0.05945853 0 1 180 Heavy
13 0.72251979 0.43300233 0.90045335 0.71561067 0.56908726 0.83224209 0.006909118 -0.19864140 0.14935021 0 1 190 Never
14 0.09884230 0.03583751 0.17662974 0.10471086 0.05248814 0.16602667 -0.005868560 -0.05395550 0.03686024 0 1 190 Occas
15 0.09718106 0.03025593 0.20892751 0.09995277 0.04579923 0.17683267 -0.002771710 -0.05882896 0.06960340 0 1 190 Regul
16 0.08145684 0.02049168 0.21537936 0.07972569 0.03298974 0.15011082 0.001731152 -0.05442303 0.10185948 0 1 190 Heavy
Multinomial
data = MASS::survey
data$Clap = relevel(data$Clap,ref="Neither")
model1 = nnet::multinom(Clap ~ Sex + Height, data=data)
predicts(model1, "0-1;160-190,10", position = 1)
Output:
val1_mean val1_lower val1_upper val2_mean val2_lower val2_upper dc_mean dc_lower dc_upper Sex_val1 Sex_val2 Height level
1 0.2179474 0.1481373 0.2941971 0.14527643 0.07351452 0.2530004 0.07267094 -0.044157072 0.168301048 0 1 160 Neither
2 0.1215233 0.0588563 0.2101143 0.04780782 0.01582787 0.1135261 0.07371553 -0.006382481 0.172339327 0 1 160 Left
3 0.6605293 0.5299565 0.7760621 0.80691574 0.66641769 0.9031933 -0.14638647 -0.304830255 0.020986361 0 1 160 Right
4 0.2311916 0.1653571 0.3088097 0.16566888 0.10316591 0.2508359 0.06552273 -0.051613705 0.175910831 0 1 170 Neither
5 0.2021789 0.1086277 0.3380053 0.08739813 0.03404116 0.1784070 0.11478078 -0.025004445 0.266783752 0 1 170 Left
6 0.5666295 0.4182749 0.6996767 0.74693299 0.61424865 0.8483228 -0.18030351 -0.375599818 0.019453825 0 1 170 Right
7 0.2316073 0.1495377 0.3333594 0.18677012 0.12847096 0.2541183 0.04483719 -0.057819872 0.162482956 0 1 180 Neither
8 0.3141199 0.1535481 0.5121523 0.14619712 0.06688544 0.2600857 0.16792282 -0.016632881 0.390902440 0 1 180 Left
9 0.4542728 0.2671435 0.6598986 0.66703276 0.52848677 0.7834621 -0.21276001 -0.435216410 0.019061782 0 1 180 Right
10 0.2125071 0.1266763 0.3269603 0.19507569 0.13111220 0.2788754 0.01743137 -0.079012456 0.132780244 0 1 190 Neither
11 0.4498845 0.2349146 0.6696044 0.23834609 0.11499362 0.4096180 0.21153838 -0.025393969 0.448376254 0 1 190 Left
12 0.3376085 0.1448809 0.5952227 0.56657822 0.39241894 0.7245775 -0.22896975 -0.436913146 -0.003295236 0 1 190 Right
To install the package in R GNU from Github (sometimes never versions available), you have to do the following steps:
Windows
- Download and install Rtools.
- Install devtools with the command install.packages(“devtools”)
- Install the package glm.predict from github with the command devtools::install_git(“git://github.com/benjaminschlegel/glm.predict”)
- Restart R / R Studio (otherwise there is sometimes a memory error)
If there is an error with “C:Program” during the installation, you have to install R in a path without spaces (not like \Program Files\).
Mac
- Install Xcode from the Mac App Store.
- Install devtools in R with the command install.packages(“devtools”)
- Install the package glm.predict in R from github with the command devtools::install_git(“git://github.com/benjaminschlegel/glm.predict”)
- Restart R / R Studio (otherwise there is sometimes a memory error)