Merge Predicted individual factor scores with data frame?

263 views
Skip to first unread message

Dai Duong

unread,
Mar 31, 2018, 7:32:02 PM3/31/18
to lavaan
How to merge Predicted individual factor scores with data frame when predicted scores are less than total observations in the original data frame?

My data frame has 10077 observations, when I run sem(), it takes into account 9745 observations. Hence, when I use lavPredict() to predict individual factor scores, there are 9745 predicted factor scores. Then, I cannot merge such 9745 scores to the 10077 obs data frame.
Here is my code:
> ctrl.labor <- "
control_labor =~ lincome+lwh+njob+skill
lincome ~~lwh
control_labor~ urbanrate+lhincome+spland_hec+owing+poor+manu_mine+uti+construction+lowser+hiser+lm1ac5+ lf_prov14+og_gov+og_busi+og_coop+og_pri+og_fdi
lincome+lwh+njob~m1ac2+major+migrated++ttnt+married+manu_mine+uti+construction+lowser+hiser+og_gov+og_busi+og_coop+og_pri+og_fdi+spland_hec
skill~lm1ac5+m1ac2+major+ttnt+married"
> fit.l <- sem(ctrl.labor, data=x180108_lkh)
> summary(fit.l, fit.measures=TRUE)
lavaan (0.5-23.1097) converged normally after 177 iterations

                                                  Used       Total
  Number of observations          9745       10077

  Estimator                                       DWLS      Robust
  Minimum Function Test Statistic              431.108     439.811
  Degrees of freedom                                19          19
  P-value (Chi-square)                           0.000       0.000
  Scaling correction factor                                  0.982
  Shift parameter                                            0.605
    for simple second-order correction (Mplus variant)
******omitted result*******

I use two ways to merge predicted values:
#This one has this error:
> newData <- cbind(x180108_lkh, lavPredict(fit.l))
Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 10077, 9745
# This one has another error:
> factorscores <- do.call(rbind, predict(fit.l, newdata = x180108_lkh))
Error in do.call(rbind, predict(fit.l, newdata = x180108_lkh)) : 
  second argument must be a list


I think, overall, there is a mismatch between 10077-observations dataframe and 9745-predicted values.

Thank you very much,
Dai

Terrence Jorgensen

unread,
Apr 5, 2018, 5:15:32 AM4/5/18
to lavaan
This issue was resolved.


You can install the development version using this:

install.packages("lavaan", repos="http://www.da.ugent.be", type="source")


Terrence D. Jorgensen
Postdoctoral Researcher, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Christopher Hübel

unread,
May 13, 2020, 1:22:57 PM5/13/20
to lavaan
It still does not work for me even though I am using lavaan 0.6-5
It only creates scores for individuals without NAs
when I use "append.data" it only appends the columns that I have used for the original model and not the whole new data frame with the new data

Any solution?

Yves Rosseel

unread,
May 13, 2020, 2:21:49 PM5/13/20
to lav...@googlegroups.com
Please open an issue about this on github:

https://github.com/yrosseel/lavaan/issues

Ideally, add a small reprex.

Yves.

On 5/13/20 7:22 PM, 'Christopher Hübel' via lavaan wrote:
> It still does not work for me even though I am using lavaan 0.6-5
> It only creates scores for individuals without NAs
> when I use "append.data" it only appends the columns that I have used
> for the original model and not the whole new data frame with the new data
>
> Any solution?
>
> On Thursday, April 5, 2018 at 10:15:32 AM UTC+1, Terrence Jorgensen wrote:
>
> This issue was resolved.
>
> https://groups.google.com/d/msg/lavaan/zfadQQpamng/tukY-tZlAgAJ
> <https://groups.google.com/d/msg/lavaan/zfadQQpamng/tukY-tZlAgAJ>
>
> You can install the development version using this:
>
> |install.packages("lavaan",repos="http://www.da.ugent.be
> <http://www.da.ugent.be/>", type="source")|
>
>
> Terrence D. Jorgensen
> Postdoctoral Researcher, Methods and Statistics
> Research Institute for Child Development and Education, the
> University of Amsterdam
> UvA web page: http://www.uva.nl/profile/t.d.jorgensen
> <http://www.uva.nl/profile/t.d.jorgensen>
>
> --
> You received this message because you are subscribed to the Google
> Groups "lavaan" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to lavaan+un...@googlegroups.com
> <mailto:lavaan+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/lavaan/58315fa5-3575-4c9e-86cc-fd7da97e5a78%40googlegroups.com
> <https://groups.google.com/d/msgid/lavaan/58315fa5-3575-4c9e-86cc-fd7da97e5a78%40googlegroups.com?utm_medium=email&utm_source=footer>.

Huebel, Christopher

unread,
May 18, 2020, 9:00:59 AM5/18/20
to 'Christopher Hübel' via lavaan
I have created two issues on github.

Where can I send you the data? Would prefer not to upload it

Topher

> On 13 May 2020, at 19:21, Yves Rosseel <yros...@gmail.com> wrote:
>
> Please open an issue about this on github:
>
> https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fyrosseel%2Flavaan%2Fissues&amp;data=01%7C01%7Cchristopher.huebel%40kcl.ac.uk%7C263a3e1458ad4558b0a208d7f76a8076%7C8370cf1416f34c16b83c724071654356%7C0&amp;sdata=tdIRnGxgTh5Rlay%2Fl93k6i21bcdjZBSRqNU82aO40oA%3D&amp;reserved=0
>
> Ideally, add a small reprex.
>
> Yves.
>
> On 5/13/20 7:22 PM, 'Christopher Hübel' via lavaan wrote:
>> It still does not work for me even though I am using lavaan 0.6-5
>> It only creates scores for individuals without NAs
>> when I use "append.data" it only appends the columns that I have used for the original model and not the whole new data frame with the new data
>> Any solution?
>> On Thursday, April 5, 2018 at 10:15:32 AM UTC+1, Terrence Jorgensen wrote:
>> This issue was resolved.
>> https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsg%2Flavaan%2FzfadQQpamng%2FtukY-tZlAgAJ&amp;data=01%7C01%7Cchristopher.huebel%40kcl.ac.uk%7C263a3e1458ad4558b0a208d7f76a8076%7C8370cf1416f34c16b83c724071654356%7C0&amp;sdata=KC8aPaKk5HjcgFwKDc27a%2BTUI1YwvDLqsiEwDow5xHU%3D&amp;reserved=0
>> <https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsg%2Flavaan%2FzfadQQpamng%2FtukY-tZlAgAJ&amp;data=01%7C01%7Cchristopher.huebel%40kcl.ac.uk%7C263a3e1458ad4558b0a208d7f76a8076%7C8370cf1416f34c16b83c724071654356%7C0&amp;sdata=KC8aPaKk5HjcgFwKDc27a%2BTUI1YwvDLqsiEwDow5xHU%3D&amp;reserved=0>
>> You can install the development version using this:
>> |install.packages("lavaan",repos="https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.da.ugent.be%2F&amp;data=01%7C01%7Cchristopher.huebel%40kcl.ac.uk%7C263a3e1458ad4558b0a208d7f76a8076%7C8370cf1416f34c16b83c724071654356%7C0&amp;sdata=0uFvYAbHM%2FBdG8rcDOrBRHBZ%2FnYgmSSa6WHegKOAz1w%3D&amp;reserved=0
>> <https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.da.ugent.be%2F&amp;data=01%7C01%7Cchristopher.huebel%40kcl.ac.uk%7C263a3e1458ad4558b0a208d7f76a8076%7C8370cf1416f34c16b83c724071654356%7C0&amp;sdata=0uFvYAbHM%2FBdG8rcDOrBRHBZ%2FnYgmSSa6WHegKOAz1w%3D&amp;reserved=0>", type="source")|
>> Terrence D. Jorgensen
>> Postdoctoral Researcher, Methods and Statistics
>> Research Institute for Child Development and Education, the
>> University of Amsterdam
>> UvA web page: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.uva.nl%2Fprofile%2Ft.d.jorgensen&amp;data=01%7C01%7Cchristopher.huebel%40kcl.ac.uk%7C263a3e1458ad4558b0a208d7f76a8076%7C8370cf1416f34c16b83c724071654356%7C0&amp;sdata=y8nAZsTOVuuJTCgPNvBRUQSoa9YNjcvJ%2FxV1F%2Bzbpb4%3D&amp;reserved=0
>> <https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.uva.nl%2Fprofile%2Ft.d.jorgensen&amp;data=01%7C01%7Cchristopher.huebel%40kcl.ac.uk%7C263a3e1458ad4558b0a208d7f76a8076%7C8370cf1416f34c16b83c724071654356%7C0&amp;sdata=y8nAZsTOVuuJTCgPNvBRUQSoa9YNjcvJ%2FxV1F%2Bzbpb4%3D&amp;reserved=0>
>> --
>> You received this message because you are subscribed to the Google Groups "lavaan" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com <mailto:lavaan+un...@googlegroups.com>.
>> To view this discussion on the web visit https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Flavaan%2F58315fa5-3575-4c9e-86cc-fd7da97e5a78%2540googlegroups.com&amp;data=01%7C01%7Cchristopher.huebel%40kcl.ac.uk%7C263a3e1458ad4558b0a208d7f76a8076%7C8370cf1416f34c16b83c724071654356%7C0&amp;sdata=L61PS26e%2FKL17XQzo1GMOVLGne12Ioqxt7AS50L2g9o%3D&amp;reserved=0 <https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Flavaan%2F58315fa5-3575-4c9e-86cc-fd7da97e5a78%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&amp;data=01%7C01%7Cchristopher.huebel%40kcl.ac.uk%7C263a3e1458ad4558b0a208d7f76a8076%7C8370cf1416f34c16b83c724071654356%7C0&amp;sdata=%2B0I7kFQ7HS5soiMo%2FUYRktv5pHSS2x%2FWhyIgfGL67ag%3D&amp;reserved=0>.
>
> --
> You received this message because you are subscribed to the Google Groups "lavaan" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
> To view this discussion on the web visit https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Flavaan%2F7a14863a-a394-c94b-19f8-43857b19c0cd%2540gmail.com&amp;data=01%7C01%7Cchristopher.huebel%40kcl.ac.uk%7C263a3e1458ad4558b0a208d7f76a8076%7C8370cf1416f34c16b83c724071654356%7C0&amp;sdata=jfa9r1IeJRA7iBBo959%2BwT%2Bg82AU9hOGDug%2FRs02u7I%3D&amp;reserved=0.

Yves Rosseel

unread,
May 18, 2020, 9:56:42 AM5/18/20
to lav...@googlegroups.com
You can email it to me (Yves dot Rosseel at UGent dot be).

Yves.

Luis Anunciação

unread,
Jun 22, 2024, 6:32:13 AM (11 days ago) Jun 22
to lavaan
Hello, I had this same issue in this year -- 2024.
I'm using this code to add all factor scores to the dataset. I'm happy if any new solution is available.

idx <- lavInspect(cfa_tead, "case.idx")
data_lavaan = lavPredict(cfa_tead)
for (fs in colnames(data_lavaan)) {
  df_tead[idx, fs] <- data_lavaan[ , fs]
}

replace cfa_tead to the CFA model. Replace df_tead to your dataframe
Reply all
Reply to author
Forward
0 new messages