Skip to contents

Calculates prediction using the results in object. The newdata argument is an optional data frame or matrix in which to look for variables with which to predict. If newdata is omitted, the scores are used.

Usage

predict(object, ...)

Arguments

object

an object of class "Fa" or of a class derived from "Fa"

...

additional arguments, e.g., newdata: an optional data frame or matrix in which to look for variables with which to predict. If newdata is not missing, newdata should be scaled before "predict".

Methods

signature(object = "Fa")

generic functions - see print, summary, predict, plot, getCenter, getEigenvalues, getFa, getLoadings, getQuan, getScores, getSdev

References

Zhang, Y. Y. (2013), An Object Oriented Solution for Robust Factor Analysis.

Author

Ying-Ying Zhang (Robert) robertzhangyying@qq.com

Examples


data("hbk")
hbk.x = hbk[,1:3] 

faCovPcaRegMcd = FaCov(x = hbk.x, factors = 2, method = "pca",
scoresMethod = "regression", cov.control = rrcov::CovControlMcd()); faCovPcaRegMcd
#> An object of class "FaCov"
#> Slot "call":
#> FaCov(x = hbk.x, factors = 2, cov.control = rrcov::CovControlMcd(), 
#>     method = "pca", scoresMethod = "regression")
#> 
#> Slot "converged":
#> NULL
#> 
#> Slot "loadings":
#>        Factor1    Factor2
#> X1 -0.00357579  1.0407697
#> X2  1.01735884 -0.1074368
#> X3  0.55673070  0.4225046
#> 
#> Slot "communality":
#>        X1        X2        X3 
#> 1.0832144 1.0465617 0.4884592 
#> 
#> Slot "uniquenesses":
#>        X1        X2        X3 
#> 0.1436750 0.2022401 0.6713488 
#> 
#> Slot "cor":
#> [1] FALSE
#> 
#> Slot "covariance":
#>            X1         X2        X3
#> X1 1.22688941 0.05500588 0.1271656
#> X2 0.05500588 1.24880175 0.1525276
#> X3 0.12716557 0.15252762 1.1598081
#> 
#> Slot "correlation":
#>            X1         X2        X3
#> X1 1.00000000 0.04443853 0.1066041
#> X2 0.04443853 1.00000000 0.1267385
#> X3 0.10660407 0.12673854 1.0000000
#> 
#> Slot "usedMatrix":
#>            X1         X2        X3
#> X1 1.22688941 0.05500588 0.1271656
#> X2 0.05500588 1.24880175 0.1525276
#> X3 0.12716557 0.15252762 1.1598081
#> 
#> Slot "reducedCorrelation":
#> NULL
#> 
#> Slot "criteria":
#> NULL
#> 
#> Slot "factors":
#> [1] 2
#> 
#> Slot "dof":
#> NULL
#> 
#> Slot "method":
#> [1] "pca"
#> 
#> Slot "scores":
#>           Factor1     Factor2
#>  [1,] 23.37446110 12.08300643
#>  [2,] 24.34703619 11.62237670
#>  [3,] 24.83571062 13.27852593
#>  [4,] 26.17084132 12.61905004
#>  [5,] 25.59918577 12.83553071
#>  [6,] 24.28522312 12.79892797
#>  [7,] 24.65518678 12.44282588
#>  [8,] 23.58356104 12.06537192
#>  [9,] 25.29871081 12.37449155
#> [10,] 24.28794072 11.99663283
#> [11,] 29.29010377 14.10292664
#> [12,] 29.21602329 15.67548601
#> [13,] 30.36694090 14.31639870
#> [14,] 36.61093166 12.22469826
#> [15,]  0.87846757  1.48055428
#> [16,] -0.33469848  0.81353359
#> [17,] -0.59533115 -1.67797608
#> [18,] -0.07692585  0.74960673
#> [19,]  0.88664992 -0.81140980
#> [20,]  1.32586314  1.18338691
#> [21,]  0.32355720  0.87258307
#> [22,]  1.26507774 -1.10045701
#> [23,]  0.02132206  0.03769255
#> [24,] -0.04050523 -0.62810919
#> [25,] -1.82877805 -0.54081595
#> [26,]  1.53565293 -0.52706006
#> [27,]  0.88763579  1.69714971
#> [28,] -0.65476444  0.46386279
#> [29,] -0.99571552 -0.40049510
#> [30,] -0.14326046  0.39732743
#> [31,] -0.68032580  1.14648951
#> [32,]  0.02122339 -1.36276478
#> [33,]  0.94777315 -0.29514861
#> [34,] -1.66606969 -0.94732785
#> [35,]  0.86479203  1.57748129
#> [36,]  0.74968605 -0.12896299
#> [37,]  1.40520991 -1.10997137
#> [38,] -1.02005187 -0.37734893
#> [39,] -1.60441131  0.60226525
#> [40,]  0.06134748 -1.03421632
#> [41,]  0.18615684  1.92215530
#> [42,] -0.11319178 -0.59882173
#> [43,]  0.97827831 -1.65842318
#> [44,] -0.42141261  0.86497778
#> [45,] -1.74410700  0.24465420
#> [46,] -0.49884452  0.80604306
#> [47,] -1.75203599  1.21093123
#> [48,]  0.24815304  1.70416567
#> [49,]  0.51599641  1.23750475
#> [50,]  1.21199639  0.65981510
#> [51,] -0.77346099  0.29396447
#> [52,] -1.23505304  1.49707994
#> [53,] -0.34337530 -0.32700427
#> [54,]  0.43714215 -0.96286408
#> [55,]  0.25351912 -1.18596060
#> [56,]  0.76927705 -0.24033134
#> [57,] -1.23515172  0.09662262
#> [58,]  1.10917203  0.39971572
#> [59,]  0.74995363  0.38770297
#> [60,]  0.50450331 -0.50119529
#> [61,]  1.84511298 -0.80798712
#> [62,] -1.78265404 -0.66860453
#> [63,]  0.16718657 -0.94482480
#> [64,]  1.31183557  1.20554365
#> [65,] -0.47635280  0.85094120
#> [66,] -0.22439584 -1.36787167
#> [67,] -0.02401342 -0.12687401
#> [68,] -1.48792563 -1.07691539
#> [69,] -1.48261557  0.15956004
#> [70,]  0.83487804 -0.15251392
#> [71,]  0.74070096  0.61300322
#> [72,]  0.16973526 -0.86332857
#> [73,]  0.23278757 -0.85700738
#> [74,]  0.40917146 -1.36044628
#> [75,] -0.61438698 -0.53327581
#> 
#> Slot "scoresMethod":
#> [1] "regression"
#> 
#> Slot "scoringCoef":
#>                  X1         X2        X3
#> Factor1 -0.07760082  0.7707987 0.3871595
#> Factor2  0.82485664 -0.1583555 0.2946736
#> 
#> Slot "meanF":
#>  Factor1  Factor2 
#> 4.958958 2.405817 
#> 
#> Slot "corF":
#>           Factor1   Factor2
#> Factor1 1.0000000 0.9730208
#> Factor2 0.9730208 1.0000000
#> 
#> Slot "STATISTIC":
#> NULL
#> 
#> Slot "PVAL":
#> NULL
#> 
#> Slot "n.obs":
#> [1] 75
#> 
#> Slot "center":
#>       X1       X2       X3 
#> 1.537705 1.780328 1.686885 
#> 
#> Slot "eigenvalues":
#> [1] 1.436470 1.181766 1.017264
#> 
#> Slot "cov.control":
#> An object of class "CovControlMcd"
#> Slot "alpha":
#> [1] 0.5
#> 
#> Slot "nsamp":
#> [1] 500
#> 
#> Slot "scalefn":
#> NULL
#> 
#> Slot "maxcsteps":
#> [1] 200
#> 
#> Slot "seed":
#> NULL
#> 
#> Slot "use.correction":
#> [1] TRUE
#> 
#> Slot "trace":
#> [1] FALSE
#> 
#> Slot "tolSolve":
#> [1] 1e-14
#> 
#> 

## If missing newdata, the scores are used
predict(faCovPcaRegMcd)
#>           Factor1     Factor2
#>  [1,] 23.37446110 12.08300643
#>  [2,] 24.34703619 11.62237670
#>  [3,] 24.83571062 13.27852593
#>  [4,] 26.17084132 12.61905004
#>  [5,] 25.59918577 12.83553071
#>  [6,] 24.28522312 12.79892797
#>  [7,] 24.65518678 12.44282588
#>  [8,] 23.58356104 12.06537192
#>  [9,] 25.29871081 12.37449155
#> [10,] 24.28794072 11.99663283
#> [11,] 29.29010377 14.10292664
#> [12,] 29.21602329 15.67548601
#> [13,] 30.36694090 14.31639870
#> [14,] 36.61093166 12.22469826
#> [15,]  0.87846757  1.48055428
#> [16,] -0.33469848  0.81353359
#> [17,] -0.59533115 -1.67797608
#> [18,] -0.07692585  0.74960673
#> [19,]  0.88664992 -0.81140980
#> [20,]  1.32586314  1.18338691
#> [21,]  0.32355720  0.87258307
#> [22,]  1.26507774 -1.10045701
#> [23,]  0.02132206  0.03769255
#> [24,] -0.04050523 -0.62810919
#> [25,] -1.82877805 -0.54081595
#> [26,]  1.53565293 -0.52706006
#> [27,]  0.88763579  1.69714971
#> [28,] -0.65476444  0.46386279
#> [29,] -0.99571552 -0.40049510
#> [30,] -0.14326046  0.39732743
#> [31,] -0.68032580  1.14648951
#> [32,]  0.02122339 -1.36276478
#> [33,]  0.94777315 -0.29514861
#> [34,] -1.66606969 -0.94732785
#> [35,]  0.86479203  1.57748129
#> [36,]  0.74968605 -0.12896299
#> [37,]  1.40520991 -1.10997137
#> [38,] -1.02005187 -0.37734893
#> [39,] -1.60441131  0.60226525
#> [40,]  0.06134748 -1.03421632
#> [41,]  0.18615684  1.92215530
#> [42,] -0.11319178 -0.59882173
#> [43,]  0.97827831 -1.65842318
#> [44,] -0.42141261  0.86497778
#> [45,] -1.74410700  0.24465420
#> [46,] -0.49884452  0.80604306
#> [47,] -1.75203599  1.21093123
#> [48,]  0.24815304  1.70416567
#> [49,]  0.51599641  1.23750475
#> [50,]  1.21199639  0.65981510
#> [51,] -0.77346099  0.29396447
#> [52,] -1.23505304  1.49707994
#> [53,] -0.34337530 -0.32700427
#> [54,]  0.43714215 -0.96286408
#> [55,]  0.25351912 -1.18596060
#> [56,]  0.76927705 -0.24033134
#> [57,] -1.23515172  0.09662262
#> [58,]  1.10917203  0.39971572
#> [59,]  0.74995363  0.38770297
#> [60,]  0.50450331 -0.50119529
#> [61,]  1.84511298 -0.80798712
#> [62,] -1.78265404 -0.66860453
#> [63,]  0.16718657 -0.94482480
#> [64,]  1.31183557  1.20554365
#> [65,] -0.47635280  0.85094120
#> [66,] -0.22439584 -1.36787167
#> [67,] -0.02401342 -0.12687401
#> [68,] -1.48792563 -1.07691539
#> [69,] -1.48261557  0.15956004
#> [70,]  0.83487804 -0.15251392
#> [71,]  0.74070096  0.61300322
#> [72,]  0.16973526 -0.86332857
#> [73,]  0.23278757 -0.85700738
#> [74,]  0.40917146 -1.36044628
#> [75,] -0.61438698 -0.53327581

##
## If not missing newdata, newdata should be scaled first.
##
newdata = hbk.x[1, ]
cor = FALSE # the default
newdata = { 
if (cor == TRUE)
  # standardized transformation
  scale(newdata, center = faCovPcaRegMcd@center, 
  scale = sqrt(diag(faCovPcaRegMcd@covariance)))
else # cor == FALSE
  # centralized transformation
  scale(newdata, center = faCovPcaRegMcd@center, scale = FALSE)
}

##
## Now, prediction = predict(faCovPcaRegMcd)[1,] = faCovPcaRegMcd@scores[1,]
##
prediction = predict(faCovPcaRegMcd, newdata = newdata)
prediction
#>    Factor1  Factor2
#> 1 23.37446 12.08301