After doing ps matching, I'm running a poisson model like so:
model <- glm(outcome ~ x1 + x2 + x3 ... ,
data = d,
weights = psweights$weights,
family = "poisson")
And then want to create a new data frame with the variable names, coefficients, and upper and lower confidence intervals. Just doing:
d2 <- summary(model)$coef
gets me the variable names, coefficients, standard errors, and z values. What is the easiest way to compute confidence intervals, convert them into columns and bind it all into one data frame?
How about this, using the broom package:
library(broom)
mod <- glm(hp ~ disp + drat + cyl, data=mtcars, family=poisson)
tidy(mod, conf.int=TRUE)
#> # A tibble: 4 × 7
#> term estimate std.error statistic p.value conf.low conf.high
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) 2.40 0.196 12.3 1.30e-34 2.02 2.79
#> 2 disp 0.000766 0.000259 2.96 3.07e- 3 0.000258 0.00127
#> 3 drat 0.240 0.0386 6.22 4.89e-10 0.164 0.315
#> 4 cyl 0.236 0.0195 12.1 1.21e-33 0.198 0.274
Created on 2022-06-30 by the reprex package (v2.0.1)
Related
I have trained lm model on a dataset and generated the summary of the model using summary() function. How to get the summary in a table?
You can use broom::tidy :
model <- lm(mpg~cyl, mtcars)
broom::tidy(model)
# term estimate std.error statistic p.value
# <chr> <dbl> <dbl> <dbl> <dbl>
#1 (Intercept) 37.9 2.07 18.3 8.37e-18
#2 cyl -2.88 0.322 -8.92 6.11e-10
how do I create a data.table in r with coefficient, std.err and Pvlaues with rqpd regression type? It's easy with the coefficients using summary(myregression)[2] but don't know how to get std.err and Pval. Thanks
Try with broom:
library(broom)
library(dplyr)
#Model
mod <- lm(Sepal.Length~.,data=iris)
#Broom
summaryobj <- tidy(mod)
Output:
# A tibble: 6 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 2.17 0.280 7.76 1.43e-12
2 Sepal.Width 0.496 0.0861 5.76 4.87e- 8
3 Petal.Length 0.829 0.0685 12.1 1.07e-23
4 Petal.Width -0.315 0.151 -2.08 3.89e- 2
5 Speciesversicolor -0.724 0.240 -3.01 3.06e- 3
6 Speciesvirginica -1.02 0.334 -3.07 2.58e- 3
Found a solution that is working
summ <- summary(myregression, se = "boot")
summ
str(summ)
PValues <- summ$coefficients[,4]
Does exist any package which can help me to export results of multinomial logit to excel for example like a table?
The broom package does a reasonable job of tidying multinomial output.
library(broom)
library(nnet)
fit.gear <- multinom(gear ~ mpg + factor(am), data = mtcars)
summary(fit.gear)
Call:
multinom(formula = gear ~ mpg + factor(am), data = mtcars)
Coefficients:
(Intercept) mpg factor(am)1
4 -11.15154 0.5249369 11.90045
5 -18.39374 0.3662580 22.44211
Std. Errors:
(Intercept) mpg factor(am)1
4 5.317047 0.2680456 66.895845
5 67.931319 0.2924021 2.169944
Residual Deviance: 28.03075
AIC: 40.03075
tidy(fit.gear)
# A tibble: 6 x 6
y.level term estimate std.error statistic p.value
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 4 (Intercept) 1.44e-5 5.32 -2.10 3.60e- 2
2 4 mpg 1.69e+0 0.268 1.96 5.02e- 2
3 4 factor(am)1 1.47e+5 66.9 0.178 8.59e- 1
4 5 (Intercept) 1.03e-8 67.9 -0.271 7.87e- 1
5 5 mpg 1.44e+0 0.292 1.25 2.10e- 1
6 5 factor(am)1 5.58e+9 2.17 10.3 4.54e-25
Then use the openxlsx package to send that to Excel.
library(openxlsx)
write.xlsx(file="E:/.../fitgear.xlsx", tidy(fit.gear))
(Note that the tidy function exponentiates the coefficients by default, although the help page incorrectly says the default is FALSE). So these are relative risk ratios, which is why they don't match the output of summary. And if you want confidence intervals, you have to ask for them.)
I am currently struggling to run weighted regression models on multiple variables in R.
When using (non-weighted) glm, I was successful by running the following:
mtcars_1 <- mtcars %>%
nest(-gear)%>%
mutate(model_0 = map(data, ~ glm(vs ~ drat, family = "binomial", data = .)))%>%
mutate(model_0_tidy = map(model_0, tidy))%>%
select(gear, model_0_tidy)%>%
ungroup()%>%
unnest(model_0_tidy)
That is I receive the following:
# A tibble: 6 x 6
gear term estimate std.error statistic p.value
<dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 4 (Intercept) -15.3 22.6 -0.677 0.499
2 4 drat 4.26 5.76 0.740 0.459
3 3 (Intercept) -3.91 7.39 -0.529 0.597
4 3 drat 0.801 2.32 0.345 0.730
5 5 (Intercept) 5.20 14.4 0.362 0.718
6 5 drat -1.71 3.77 -0.453 0.651
However, when I would like to weight my observations and thus use svyglm from the survey-package, nesting does not work.
This was my approach:
design_0 <- svydesign(ids=~0, data = mtcars, weights = mtaars$wt)
mtcars_2 <- mtcars%>%
nest(-gear)%>%
mutate(model_1 = map(data, ~ svyglm(vs ~ drat, family = quasibinomial(logit), design = design_0, data = .)))%>%
mutate(model_1_tidy = map(model_1, tidy))%>%
select(gear, model_1_tidy)%>%
ungroup()%>%
unnest(model_1_tidy)
# If suggested that wt serves as frequency weight
# Outcome
gear term estimate std.error statistic p.value
<dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 4 (Intercept) -8.12 3.88 -2.09 0.0451
2 4 drat 2.12 1.07 1.99 0.0554
3 3 (Intercept) -8.12 3.88 -2.09 0.0451
4 3 drat 2.12 1.07 1.99 0.0554
5 5 (Intercept) -8.12 3.88 -2.09 0.0451
6 5 drat 2.12 1.07 1.99 0.0554
Estimates for each type of gear (that is 3,4,5) turns out to be the same.
It appears as if nesting was essentially ignored here.
Are there any solutions for combining svyglm with nest-map-unnest? Or will I have to look for other, less comfortable ways?
Thank you!
try to do it this way
mtcars%>%
nest(-gear) %>%
mutate(design = map(data, ~ svydesign(ids=~0, data = .x, weights = ~ wt)),
model = map(.x = design,
.f = ~ svyglm(vs ~ drat,
family = quasibinomial(logit),
design = .x))) %>%
mutate(model_tidy = map(model, tidy)) %>%
select(gear, model_tidy)%>%
ungroup()%>%
unnest(model_tidy)
I would like to create a function where the dependent variable (y) regressed with individual independent variables (x1, x2, etc.) but not in the form of multiple regression. And I would like to include another function in the same formula is to calculate AIC value. So, both of these functions in the same formula. Can somebody have any idea how to do it? I have a huge dataset and I need to find a regression for an individual dependent variable with multiple independent variables. I would really appreciate it if somebody guides me here.
The following code will give you the results of the dependent variable (y) regressed with individual independent variables
data(mtcars)
x = names(mtcars[,-1])
out <- unlist(lapply(1, function(n) combn(x, 1, FUN=function(row) paste0("mpg ~ ", paste0(row, collapse = "+")))))
out
#> [1] "mpg ~ cyl" "mpg ~ disp" "mpg ~ hp" "mpg ~ drat" "mpg ~ wt"
#> [6] "mpg ~ qsec" "mpg ~ vs" "mpg ~ am" "mpg ~ gear" "mpg ~ carb"
library(broom)
#> Warning: package 'broom' was built under R version 3.5.3
library(dplyr)
#> Warning: package 'dplyr' was built under R version 3.5.3
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
#To have the regression coefficients
tmp1 = bind_rows(lapply(out, function(frml) {
a = tidy(lm(frml, data=mtcars))
a$frml = frml
return(a)
}))
head(tmp1)
#> # A tibble: 6 x 6
#> term estimate std.error statistic p.value frml
#> <chr> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 (Intercept) 37.9 2.07 18.3 8.37e-18 mpg ~ cyl
#> 2 cyl -2.88 0.322 -8.92 6.11e-10 mpg ~ cyl
#> 3 (Intercept) 29.6 1.23 24.1 3.58e-21 mpg ~ disp
#> 4 disp -0.0412 0.00471 -8.75 9.38e-10 mpg ~ disp
#> 5 (Intercept) 30.1 1.63 18.4 6.64e-18 mpg ~ hp
#> 6 hp -0.0682 0.0101 -6.74 1.79e- 7 mpg ~ hp
#To have the regression results i.e. R2, AIC, BIC
tmp2 = bind_rows(lapply(out, function(frml) {
a = glance(lm(frml, data=mtcars))
a$frml = frml
return(a)
}))
head(tmp2)
#> # A tibble: 6 x 12
#> r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
#> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 0.726 0.717 3.21 79.6 6.11e-10 2 -81.7 169. 174.
#> 2 0.718 0.709 3.25 76.5 9.38e-10 2 -82.1 170. 175.
#> 3 0.602 0.589 3.86 45.5 1.79e- 7 2 -87.6 181. 186.
#> 4 0.464 0.446 4.49 26.0 1.78e- 5 2 -92.4 191. 195.
#> 5 0.753 0.745 3.05 91.4 1.29e-10 2 -80.0 166. 170.
#> 6 0.175 0.148 5.56 6.38 1.71e- 2 2 -99.3 205. 209.
#> # ... with 3 more variables: deviance <dbl>, df.residual <int>, frml <chr>
write.csv(tmp1, "Try_lm_coefficients.csv")
write.csv(tmp2, "Try_lm_results.csv")
Created on 2019-11-11 by the reprex package (v0.3.0)
The results can be found in "Try_lm_coefficients.csv" and "Try_lm_results.csv" files.