Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update vis-matrix #168

Merged
merged 6 commits into from
Jan 24, 2022
Merged

update vis-matrix #168

merged 6 commits into from
Jan 24, 2022

Conversation

strengejacke
Copy link
Member

@strengejacke strengejacke commented Jan 23, 2022

Tries to address issues #161, #167 and easystats/datawizard#44

@strengejacke strengejacke changed the title #161, #167 #167 Jan 23, 2022
@strengejacke
Copy link
Member Author

The example from easystats/datawizard#44 works now.

library(insight)
library(modelbased)

m1 <- lm(mpg ~ hp + factor(cyl), mtcars)
m2 <- lm(mpg ~ hp + cyl, mtcars)
m3 <- brms::brm(mpg ~ hp + factor(cyl), mtcars, refresh = 0, iter = 100, chains = 2)

predict(m1, newdata = visualisation_matrix(m1, at = "cyl"))
#>        1        2        3 
#> 25.12392 19.15627 16.60307
predict(m2, newdata = visualisation_matrix(m2, at = "cyl"))
#>        1        2        3        4        5        6        7        8 
#> 25.04464 24.03811 23.03158 22.02505 21.01852 20.01199 19.00546 17.99893 
#>        9       10 
#> 16.99240 15.98587
predict(m3, newdata = visualisation_matrix(m3, at = "cyl"))
#>      Estimate Est.Error      Q2.5    Q97.5
#> [1,] 25.45195  3.352775 19.823601 33.23774
#> [2,] 19.61568  3.250236 14.367664 26.16743
#> [3,] 15.68444  3.502450  8.791082 23.09514

Created on 2022-01-23 by the reprex package (v2.0.1)

@strengejacke strengejacke changed the title #167 update vis-matrix Jan 23, 2022
@strengejacke
Copy link
Member Author

Everything seems to work now... It actually was only necessary to fix the data-frame method.

library(insight)
library(modelbased)

m1 <- lm(mpg ~ hp + factor(cyl), mtcars)
m2 <- lm(mpg ~ hp + cyl, mtcars)
m3 <- brms::brm(mpg ~ hp + factor(cyl), mtcars, refresh = 0, iter = 100, chains = 2)

predict(m1, newdata = visualisation_matrix(m1, at = "cyl"))
#>        1        2        3 
#> 25.12392 19.15627 16.60307
predict(m2, newdata = visualisation_matrix(m2, at = "cyl"))
#>        1        2        3        4        5        6        7        8 
#> 25.04464 24.03811 23.03158 22.02505 21.01852 20.01199 19.00546 17.99893 
#>        9       10 
#> 16.99240 15.98587
predict(m3, newdata = visualisation_matrix(m3, at = "cyl"))
#>      Estimate Est.Error      Q2.5    Q97.5
#> [1,] 24.81862  3.692832 17.698192 29.61386
#> [2,] 20.03004  3.467089 13.918569 25.82578
#> [3,] 16.58598  3.978293  9.534934 27.00101


m <- lm(mpg ~ factor(cyl) * hp, data = mtcars)
estimate_slopes(m, trend = "hp", at = "cyl")
#> Estimated Marginal Effects
#> 
#> cyl  | Coefficient |   SE |         95% CI | t(26) |     p
#> ----------------------------------------------------------
#> 4.00 |       -0.11 | 0.05 | [-0.21, -0.02] | -2.47 | 0.021
#> 6.00 |   -7.61e-03 | 0.05 | [-0.11,  0.10] | -0.15 | 0.882
#> 8.00 |       -0.01 | 0.02 | [-0.05,  0.02] | -0.86 | 0.395
#> Marginal effects estimated for hp

mtcars$cyl <- factor(mtcars$cyl)
m <- lm(mpg ~ cyl * hp, data = mtcars)
estimate_slopes(m, trend = "hp", at = "cyl")
#> Estimated Marginal Effects
#> 
#> cyl | Coefficient |   SE |         95% CI | t(26) |     p
#> ---------------------------------------------------------
#> 4   |       -0.11 | 0.05 | [-0.21, -0.02] | -2.47 | 0.021
#> 6   |   -7.61e-03 | 0.05 | [-0.11,  0.10] | -0.15 | 0.882
#> 8   |       -0.01 | 0.02 | [-0.05,  0.02] | -0.86 | 0.395
#> Marginal effects estimated for hp

data(mtcars)
lm(mpg ~ as.factor(cyl) * hp, data = mtcars) |> 
  estimate_slopes(trend = "hp", at = "cyl")
#> Estimated Marginal Effects
#> 
#> cyl  | Coefficient |   SE |         95% CI | t(26) |     p
#> ----------------------------------------------------------
#> 4.00 |       -0.11 | 0.05 | [-0.21, -0.02] | -2.47 | 0.021
#> 6.00 |   -7.61e-03 | 0.05 | [-0.11,  0.10] | -0.15 | 0.882
#> 8.00 |       -0.01 | 0.02 | [-0.05,  0.02] | -0.86 | 0.395
#> Marginal effects estimated for hp

lm(mpg ~ factor(cyl) * hp, data = mtcars) |> 
  estimate_slopes(trend = "hp", at = "cyl")
#> Estimated Marginal Effects
#> 
#> cyl  | Coefficient |   SE |         95% CI | t(26) |     p
#> ----------------------------------------------------------
#> 4.00 |       -0.11 | 0.05 | [-0.21, -0.02] | -2.47 | 0.021
#> 6.00 |   -7.61e-03 | 0.05 | [-0.11,  0.10] | -0.15 | 0.882
#> 8.00 |       -0.01 | 0.02 | [-0.05,  0.02] | -0.86 | 0.395
#> Marginal effects estimated for hp

data(mtcars)
m <- lm(mpg ~ factor(cyl) * hp, data = mtcars)
estimate_means(m, at = "cyl")
#> NOTE: Results may be misleading due to involvement in interactions
#> Estimated Marginal Means
#> 
#> cyl  |  Mean |   SE |         95% CI
#> ------------------------------------
#> 4.00 | 19.44 | 3.07 | [13.13, 25.75]
#> 6.00 | 19.56 | 1.69 | [16.08, 23.03]
#> 8.00 | 15.99 | 1.31 | [13.30, 18.68]
#> 
#> Marginal means estimated at cyl

data(mtcars)
m <- lm(mpg ~ cyl * hp, data = mtcars)
estimate_means(m, at = "cyl")
#> NOTE: Results may be misleading due to involvement in interactions
#> Estimated Marginal Means
#> 
#> cyl  |  Mean |   SE |         95% CI
#> ------------------------------------
#> 4.00 | 20.82 | 2.29 | [16.14, 25.50]
#> 4.44 | 20.27 | 2.00 | [16.19, 24.36]
#> 4.89 | 19.73 | 1.71 | [16.22, 23.24]
#> 5.33 | 19.19 | 1.45 | [16.22, 22.15]
#> 5.78 | 18.64 | 1.20 | [16.18, 21.11]
#> 6.22 | 18.10 | 1.00 | [16.04, 20.15]
#> 6.67 | 17.56 | 0.87 | [15.77, 19.34]
#> 7.11 | 17.01 | 0.85 | [15.27, 18.75]
#> 7.56 | 16.47 | 0.94 | [14.55, 18.38]
#> 8.00 | 15.92 | 1.11 | [13.65, 18.20]
#> 
#> Marginal means estimated at cyl

data(mtcars)
mtcars$cyl <- factor(mtcars$cyl)
m <- lm(mpg ~ cyl * hp, data = mtcars)
estimate_means(m)
#> We selected `at = c("cyl")`.
#> NOTE: Results may be misleading due to involvement in interactions
#> Estimated Marginal Means
#> 
#> cyl |  Mean |   SE |         95% CI
#> -----------------------------------
#> 4   | 19.44 | 3.07 | [13.13, 25.75]
#> 6   | 19.56 | 1.69 | [16.08, 23.03]
#> 8   | 15.99 | 1.31 | [13.30, 18.68]
#> 
#> Marginal means estimated at cyl

Created on 2022-01-23 by the reprex package (v2.0.1)

@strengejacke
Copy link
Member Author

strengejacke commented Jan 23, 2022

named list now also works:

library(modelbased)
data <- rbind(iris, iris[149, ], make.row.names = FALSE)

# Multiple variables are of interest, creating a combination
visualisation_matrix(data, at = c("Sepal.Length = c(3, 1)", "Species = 'setosa'"))
#> Visualisation Grid
#> 
#> Sepal.Length | Species | Sepal.Width | Petal.Length | Petal.Width
#> -----------------------------------------------------------------
#>            3 |  setosa |        3.06 |         3.77 |        1.21
#>            1 |  setosa |        3.06 |         3.77 |        1.21
#> 
#> Maintained constant: Sepal.Width, Petal.Length, Petal.Width

# with list-style at-argument
visualisation_matrix(data, at = list(Sepal.Length = c(3, 1), Species = "setosa"))
#> Visualisation Grid
#> 
#> Sepal.Length | Species | Sepal.Width | Petal.Length | Petal.Width
#> -----------------------------------------------------------------
#>            3 |  setosa |        3.06 |         3.77 |        1.21
#>            1 |  setosa |        3.06 |         3.77 |        1.21
#> 
#> Maintained constant: Sepal.Width, Petal.Length, Petal.Width

Created on 2022-01-24 by the reprex package (v2.0.1)

@strengejacke
Copy link
Member Author

I think this PR fixes some issues and makes the API a bit more flexible, so that at can also be a named list. @DominiqueMakowski @bwiernik - if you approve the changes, we could merge this PR.

@IndrajeetPatil regarding easystats/datawizard#44, we could move the vis-mat functions from here to datawizard and then re-export in modelbased. Maybe you can wait a few days with the submission of datawizard until we made a decision on this. Would be great then to have this stuff already available in the CRAN version.

@strengejacke
Copy link
Member Author

we should add some tests for this PR, will do later.

@IndrajeetPatil
Copy link
Member

@strengejacke Yepp, will wait for your signal before I submit to CRAN.

I am also waiting for your review on the PR in datawizard. Can't submit before that happens either.

@DominiqueMakowski
Copy link
Member

awesome

@strengejacke strengejacke merged commit 90a5454 into master Jan 24, 2022
@IndrajeetPatil IndrajeetPatil deleted the visualization_matrix branch January 24, 2022 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants