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

Make augment.factanal() more robust. Fixes #604. #650

Merged
merged 7 commits into from Apr 11, 2019
Merged

Make augment.factanal() more robust. Fixes #604. #650

merged 7 commits into from Apr 11, 2019

Conversation

IndrajeetPatil
Copy link
Contributor

I have also updated the examples to reflect how specifying different scoring methods won't break any of the methods.

Here is a reprex:

set.seed(123)

# data
v1 <- c(1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,4,5,6)
v2 <- c(1,2,1,1,1,1,2,1,2,1,3,4,3,3,3,4,6,5)
v3 <- c(3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,5,4,6)
v4 <- c(3,3,4,3,3,1,1,2,1,1,1,1,2,1,1,5,6,4)
v5 <- c(1,1,1,1,1,3,3,3,3,3,1,1,1,1,1,6,4,5)
v6 <- c(1,1,1,2,1,3,3,3,4,3,1,1,1,2,1,6,5,4)
m1 <- cbind.data.frame(v1,v2,v3,v4,v5,v6)

# new data
m2 <-
  cbind.data.frame(
    x1 = rev(v1),
    x2 = rev(v2),
    x3 = rev(v3),
    x4 = rev(v4),
    x5 = rev(v5),
    x6 = rev(v6)
  )

# objects
x1 <- stats::factanal(m1, factors = 3, scores = "Bartlett")
x2 <- stats::factanal(m1, factors = 3, scores = "regression")

# tidying the object
broom::tidy(x1)
#> # A tibble: 6 x 5
#>   variable uniqueness   fl1   fl2   fl3
#>   <chr>         <dbl> <dbl> <dbl> <dbl>
#> 1 v1           0.005  0.944 0.182 0.267
#> 2 v2           0.101  0.905 0.235 0.159
#> 3 v3           0.005  0.236 0.210 0.946
#> 4 v4           0.224  0.180 0.242 0.828
#> 5 v5           0.0843 0.242 0.881 0.286
#> 6 v6           0.005  0.193 0.959 0.196
broom::tidy(x2)
#> # A tibble: 6 x 5
#>   variable uniqueness   fl1   fl2   fl3
#>   <chr>         <dbl> <dbl> <dbl> <dbl>
#> 1 v1           0.005  0.944 0.182 0.267
#> 2 v2           0.101  0.905 0.235 0.159
#> 3 v3           0.005  0.236 0.210 0.946
#> 4 v4           0.224  0.180 0.242 0.828
#> 5 v5           0.0843 0.242 0.881 0.286
#> 6 v6           0.005  0.193 0.959 0.196

# augmented dataframe
broom::augment(x1)
#> # A tibble: 18 x 4
#>    .rownames   .fs1   .fs2   .fs3
#>    <chr>      <dbl>  <dbl>  <dbl>
#>  1 1         -0.904 -0.931  0.948
#>  2 2         -0.869 -0.933  0.935
#>  3 3         -0.908 -0.932  0.962
#>  4 4         -1.00  -0.253  0.818
#>  5 5         -0.904 -0.931  0.948
#>  6 6         -0.745  0.727 -0.788
#>  7 7         -0.710  0.725 -0.801
#>  8 8         -0.750  0.726 -0.774
#>  9 9         -0.808  1.40  -0.930
#> 10 10        -0.745  0.727 -0.788
#> 11 11         0.927 -0.931 -0.837
#> 12 12         0.963 -0.933 -0.849
#> 13 13         0.923 -0.932 -0.823
#> 14 14         0.829 -0.253 -0.967
#> 15 15         0.927 -0.931 -0.837
#> 16 16         0.422  2.05   1.29 
#> 17 17         1.47   1.29   0.545
#> 18 18         1.88   0.309  1.95
broom::augment(x2)
#> # A tibble: 18 x 4
#>    .rownames   .fs1   .fs2   .fs3
#>    <chr>      <dbl>  <dbl>  <dbl>
#>  1 1         -0.897 -0.925  0.936
#>  2 2         -0.861 -0.927  0.924
#>  3 3         -0.901 -0.926  0.950
#>  4 4         -0.993 -0.251  0.809
#>  5 5         -0.897 -0.925  0.936
#>  6 6         -0.741  0.720 -0.784
#>  7 7         -0.706  0.718 -0.796
#>  8 8         -0.745  0.719 -0.770
#>  9 9         -0.803  1.39  -0.923
#> 10 10        -0.741  0.720 -0.784
#> 11 11         0.917 -0.925 -0.830
#> 12 12         0.952 -0.927 -0.842
#> 13 13         0.913 -0.926 -0.816
#> 14 14         0.820 -0.252 -0.958
#> 15 15         0.917 -0.925 -0.830
#> 16 16         0.426  2.04   1.28 
#> 17 17         1.46   1.29   0.548
#> 18 18         1.88   0.314  1.95

# augmented dataframe (with new data)
broom::augment(x1, data = m2)
#> # A tibble: 18 x 10
#>    .rownames    x1    x2    x3    x4    x5    x6   .fs1   .fs2   .fs3
#>    <chr>     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>
#>  1 1             6     5     6     4     5     4 -0.904 -0.931  0.948
#>  2 2             5     6     4     6     4     5 -0.869 -0.933  0.935
#>  3 3             4     4     5     5     6     6 -0.908 -0.932  0.962
#>  4 4             3     3     1     1     1     1 -1.00  -0.253  0.818
#>  5 5             3     3     1     1     1     2 -0.904 -0.931  0.948
#>  6 6             3     3     1     2     1     1 -0.745  0.727 -0.788
#>  7 7             3     4     1     1     1     1 -0.710  0.725 -0.801
#>  8 8             3     3     1     1     1     1 -0.750  0.726 -0.774
#>  9 9             1     1     1     1     3     3 -0.808  1.40  -0.930
#> 10 10            1     2     1     1     3     4 -0.745  0.727 -0.788
#> 11 11            1     1     1     2     3     3  0.927 -0.931 -0.837
#> 12 12            1     2     1     1     3     3  0.963 -0.933 -0.849
#> 13 13            1     1     1     1     3     3  0.923 -0.932 -0.823
#> 14 14            1     1     3     3     1     1  0.829 -0.253 -0.967
#> 15 15            1     1     3     3     1     2  0.927 -0.931 -0.837
#> 16 16            1     1     3     4     1     1  0.422  2.05   1.29 
#> 17 17            1     2     3     3     1     1  1.47   1.29   0.545
#> 18 18            1     1     3     3     1     1  1.88   0.309  1.95
broom::augment(x2, data = m2)
#> # A tibble: 18 x 10
#>    .rownames    x1    x2    x3    x4    x5    x6   .fs1   .fs2   .fs3
#>    <chr>     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>
#>  1 1             6     5     6     4     5     4 -0.897 -0.925  0.936
#>  2 2             5     6     4     6     4     5 -0.861 -0.927  0.924
#>  3 3             4     4     5     5     6     6 -0.901 -0.926  0.950
#>  4 4             3     3     1     1     1     1 -0.993 -0.251  0.809
#>  5 5             3     3     1     1     1     2 -0.897 -0.925  0.936
#>  6 6             3     3     1     2     1     1 -0.741  0.720 -0.784
#>  7 7             3     4     1     1     1     1 -0.706  0.718 -0.796
#>  8 8             3     3     1     1     1     1 -0.745  0.719 -0.770
#>  9 9             1     1     1     1     3     3 -0.803  1.39  -0.923
#> 10 10            1     2     1     1     3     4 -0.741  0.720 -0.784
#> 11 11            1     1     1     2     3     3  0.917 -0.925 -0.830
#> 12 12            1     2     1     1     3     3  0.952 -0.927 -0.842
#> 13 13            1     1     1     1     3     3  0.913 -0.926 -0.816
#> 14 14            1     1     3     3     1     1  0.820 -0.252 -0.958
#> 15 15            1     1     3     3     1     2  0.917 -0.925 -0.830
#> 16 16            1     1     3     4     1     1  0.426  2.04   1.28 
#> 17 17            1     2     3     3     1     1  1.46   1.29   0.548
#> 18 18            1     1     3     3     1     1  1.88   0.314  1.95

Created on 2019-03-10 by the reprex package (v0.2.1.9000)

Copy link
Collaborator

@alexpghayes alexpghayes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. I believe the new column names are a breaking change from the previous factor1, ..., factorn. Can you:

  • Add a note in NEWS about the breaking change in column names
  • Add quick regression tests

@IndrajeetPatil
Copy link
Contributor Author

@alexpghayes Done!

Copy link
Collaborator

@alexpghayes alexpghayes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more thoughts

R/stats-factanal-tidiers.R Outdated Show resolved Hide resolved
R/stats-factanal-tidiers.R Outdated Show resolved Hide resolved
R/stats-factanal-tidiers.R Outdated Show resolved Hide resolved
R/stats-factanal-tidiers.R Show resolved Hide resolved
R/stats-factanal-tidiers.R Outdated Show resolved Hide resolved
R/stats-factanal-tidiers.R Show resolved Hide resolved
tests/testthat/test-stats-factanal.R Show resolved Hide resolved
@IndrajeetPatil
Copy link
Contributor Author

@alexpghayes Done!

@alexpghayes alexpghayes changed the title augment method for factanal changed; closes #604 Make augment.factanal() more robust. Fixes #604. Apr 9, 2019
Copy link
Collaborator

@alexpghayes alexpghayes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more comments. Thanks for cranking all these out!

tests/testthat/test-stats-factanal.R Outdated Show resolved Hide resolved
tests/testthat/test-stats-factanal.R Outdated Show resolved Hide resolved
tests/testthat/test-stats-factanal.R Outdated Show resolved Hide resolved
tests/testthat/test-stats-factanal.R Outdated Show resolved Hide resolved
@IndrajeetPatil
Copy link
Contributor Author

@alexpghayes All comments addressed.

@alexpghayes alexpghayes merged commit d1732cf into tidymodels:master Apr 11, 2019
@IndrajeetPatil IndrajeetPatil deleted the factanal_fix branch Apr 11, 2019
@github-actions
Copy link

github-actions bot commented Mar 8, 2021

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants