當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R broom tidy.poLCA 整理 a(n) poLCA 對象


Tidy 總結了有關模型組件的信息。模型組件可能是回歸中的單個項、單個假設、聚類或類。 tidy 所認為的模型組件的確切含義因模型而異,但通常是不言而喻的。如果模型具有多種不同類型的組件,您將需要指定要返回哪些組件。

用法

# S3 method for poLCA
tidy(x, ...)

參數

x

poLCA::poLCA() 返回的 poLCA 對象。

...

附加參數。不曾用過。僅需要匹配通用簽名。注意:拚寫錯誤的參數將被吸收到 ... 中,並被忽略。如果拚寫錯誤的參數有默認值,則將使用默認值。例如,如果您傳遞 conf.lvel = 0.9 ,所有計算將使用 conf.level = 0.95 進行。這裏有兩個異常:

  • tidy() 方法在提供 exponentiate 參數時會發出警告(如果該參數將被忽略)。

  • augment() 方法在提供 newdata 參數時會發出警告(如果該參數將被忽略)。

也可以看看

tidy() , poLCA::poLCA()

其他 poLCA 整理器:augment.poLCA()glance.poLCA()

帶有列的 tibble::tibble()

class

正在考慮的類。

outcome

清單變量的結果。

std.error

回歸項的標準誤差。

variable

清單變量

estimate

估計class-conditional響應概率

例子


# load libraries for models and data
library(poLCA)
library(dplyr)

# generate data
data(values)

f <- cbind(A, B, C, D) ~ 1

# fit model
M1 <- poLCA(f, values, nclass = 2, verbose = FALSE)

M1
#> Conditional item response (column) probabilities,
#>  by outcome variable, for each class (row) 
#>  
#> $A
#>            Pr(1)  Pr(2)
#> class 1:  0.2864 0.7136
#> class 2:  0.0068 0.9932
#> 
#> $B
#>            Pr(1)  Pr(2)
#> class 1:  0.6704 0.3296
#> class 2:  0.0602 0.9398
#> 
#> $C
#>            Pr(1)  Pr(2)
#> class 1:  0.6460 0.3540
#> class 2:  0.0735 0.9265
#> 
#> $D
#>            Pr(1)  Pr(2)
#> class 1:  0.8676 0.1324
#> class 2:  0.2309 0.7691
#> 
#> Estimated class population shares 
#>  0.7208 0.2792 
#>  
#> Predicted class memberships (by modal posterior prob.) 
#>  0.6713 0.3287 
#>  
#> ========================================================= 
#> Fit for 2 latent classes: 
#> ========================================================= 
#> number of observations: 216 
#> number of estimated parameters: 9 
#> residual degrees of freedom: 6 
#> maximum log-likelihood: -504.4677 
#>  
#> AIC(2): 1026.935
#> BIC(2): 1057.313
#> G^2(2): 2.719922 (Likelihood ratio/deviance statistic) 
#> X^2(2): 2.719764 (Chi-square goodness of fit) 
#>  

# summarize model fit with tidiers + visualization
tidy(M1)
#> # A tibble: 16 × 5
#>    variable class outcome estimate std.error
#>    <chr>    <int>   <dbl>    <dbl>     <dbl>
#>  1 A            1       1  0.286      0.0393
#>  2 A            2       1  0.00681    0.0254
#>  3 A            1       2  0.714      0.0393
#>  4 A            2       2  0.993      0.0254
#>  5 B            1       1  0.670      0.0489
#>  6 B            2       1  0.0602     0.0649
#>  7 B            1       2  0.330      0.0489
#>  8 B            2       2  0.940      0.0649
#>  9 C            1       1  0.646      0.0482
#> 10 C            2       1  0.0735     0.0642
#> 11 C            1       2  0.354      0.0482
#> 12 C            2       2  0.927      0.0642
#> 13 D            1       1  0.868      0.0379
#> 14 D            2       1  0.231      0.0929
#> 15 D            1       2  0.132      0.0379
#> 16 D            2       2  0.769      0.0929
augment(M1)
#> # A tibble: 216 × 7
#>        A     B     C     D X.Intercept. .class .probability
#>    <dbl> <dbl> <dbl> <dbl>        <dbl>  <int>        <dbl>
#>  1     2     2     2     2            1      2        0.959
#>  2     2     2     2     2            1      2        0.959
#>  3     2     2     2     2            1      2        0.959
#>  4     2     2     2     2            1      2        0.959
#>  5     2     2     2     2            1      2        0.959
#>  6     2     2     2     2            1      2        0.959
#>  7     2     2     2     2            1      2        0.959
#>  8     2     2     2     2            1      2        0.959
#>  9     2     2     2     2            1      2        0.959
#> 10     2     2     2     2            1      2        0.959
#> # ℹ 206 more rows
glance(M1)
#> # A tibble: 1 × 8
#>   logLik   AIC   BIC g.squared chi.squared    df df.residual  nobs
#>    <dbl> <dbl> <dbl>     <dbl>       <dbl> <dbl>       <dbl> <int>
#> 1  -504. 1027. 1057.      2.72        2.72     9           6   216

library(ggplot2)

ggplot(tidy(M1), aes(factor(class), estimate, fill = factor(outcome))) +
  geom_bar(stat = "identity", width = 1) +
  facet_wrap(~variable)


# three-class model with a single covariate.
data(election)

f2a <- cbind(
  MORALG, CARESG, KNOWG, LEADG, DISHONG, INTELG,
  MORALB, CARESB, KNOWB, LEADB, DISHONB, INTELB
) ~ PARTY

nes2a <- poLCA(f2a, election, nclass = 3, nrep = 5, verbose = FALSE)
#> Error in eval(predvars, data, env): object 'MORALG' not found

td <- tidy(nes2a)
#> Error in eval(expr, envir, enclos): object 'nes2a' not found
td
#> Error in eval(expr, envir, enclos): object 'td' not found

ggplot(td, aes(outcome, estimate, color = factor(class), group = class)) +
  geom_line() +
  facet_wrap(~variable, nrow = 2) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
#> Error in eval(expr, envir, enclos): object 'td' not found

au <- augment(nes2a)
#> Error in eval(expr, envir, enclos): object 'nes2a' not found

au
#> Error in eval(expr, envir, enclos): object 'au' not found

count(au, .class)
#> Error in eval(expr, envir, enclos): object 'au' not found

# if the original data is provided, it leads to NAs in new columns
# for rows that weren't predicted
au2 <- augment(nes2a, data = election)
#> Error in eval(expr, envir, enclos): object 'nes2a' not found

au2
#> Error in eval(expr, envir, enclos): object 'au2' not found

dim(au2)
#> Error in eval(expr, envir, enclos): object 'au2' not found
源代碼:R/polca-tidiers.R

相關用法


注:本文由純淨天空篩選整理自大神的英文原創作品 Tidy a(n) poLCA object。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。