factors scores from a cfa into a .csv file, strange numbers...

18 views
Skip to first unread message

Ana Inés

unread,
Jul 12, 2019, 4:39:16 AM7/12/19
to lavaan
Dear all,

I used the predict() funtion to obtain the factor scores from a cfa, and then I exported them into a .csv file, but for some of the scores a see strange numbers in the .csv file...

See...

In in r output, I've got the following factor scores:

        TCN    MES    IFN    HCS    SDT    MOL    COF    VIC    HAP    EPE    INS
  [1,]  0.405  0.896  0.350  0.562  0.290  0.800 -0.214  0.356  0.291  0.756  0.178
  [2,]  0.222 -0.797 -0.553 -0.649 -1.256 -0.770 -1.284 -0.097 -0.390  0.381 -1.068
  [3,]  0.419  0.595  0.331  0.221 -0.604 -0.002  0.137  0.210  0.506  0.713 -0.600
  [4,] -0.289  0.384  0.045  0.253  0.947  0.939  0.880  0.228  0.490 -0.156  0.658
  [5,]  0.364  0.672  0.214  0.223  0.442  0.058  0.936 -0.114 -0.085 -0.525 -0.140
  [6,]  0.320  0.284  0.251  0.396  0.841  0.368  0.955  0.106  0.046  0.707  0.392
  [7,]  0.341 -0.790 -0.277 -0.319 -0.122 -0.142 -0.569  0.173 -0.077  0.487  0.651
  [8,]  0.360  0.455 -0.025  0.149 -0.473 -0.105 -0.574  0.269  0.186  0.617  0.302
  [9,]  0.374  0.125  0.030  0.173 -0.555 -0.083 -0.688  0.329  0.113  0.627  1.093
 [10,]  0.405 -0.047  0.086  0.189 -0.001  0.047  0.053  0.175 -0.013  0.605 -0.271
 [11,] -0.538 -0.114 -0.102 -0.113 -0.645 -0.087 -0.244 -0.010  0.146 -0.586  0.428
 [12,]  0.331 -0.814 -0.481 -0.457  0.644 -0.087 -0.234 -0.172 -0.655  0.567  0.181
 [13,] -4.798 -4.418 -3.414 -3.013 -3.064 -2.009 -3.346 -1.836 -2.712 -4.853 -5.108

But when I export them into .csv file, I've got the following (see row 13):


TCN MES IFN
1 0.405074360933509 0.89591263143014 0.350196636706386
2 0.222394337726696 -0.797197056821503 -0.552660809441361
3 0.419301994997666 0.594904648882837 0.331362904115146
4 -0.289239959044615 0.384487807504438 0.0450253372847181
5 0.364181426236668 0.671898219435669 0.213863510386007
6 0.319903709040565 0.284027348512496 0.251463467605034
7 0.341415192820549 -0.78976636134166 -0.277446764847075
8 0.360396181956539 0.454899971208634 -0.0247914437753449
9 0.374020763962752 0.124800733688989 0.0295652528335356
10 0.405444848713084 -0.0466328390313045 0.0864922346679264
11 -0.538182546183032 -0.114111653609613 -0.102171583707599
12 0.331482133914711 -0.813660156312539 -0.481033793251012
13 -479.846.560.899.043 -441.763.468.213.331 -341.441.407.255.377

This might be very easy answer, but I can not figure out why in the .csv file row 13 looks like that...

Thanks in advance for your help!

A.

Christopher David Desjardins

unread,
Jul 12, 2019, 7:49:24 AM7/12/19
to lavaan

What’s your exact code? Are you using write.csv() or something else (perhaps write_csv() from readr)? I have no problems with this:

library(lavaan)
HS.model <- ' visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9 '
fit <- cfa(HS.model, data=HolzingerSwineford1939)
head(predict(fit)); tail(predict(fit))
write.csv(predict(fit), "~/Desktop/fit.csv", row.names = FALSE)
read.fit <- read.csv("~/Desktop/fit.csv")
head(read.fit); tail(read.fit)
all.equal(read.fit[,1], predict(fit)[,1])
all.equal(read.fit[,2], predict(fit)[,2])
all.equal(read.fit[,3], predict(fit)[,3])

--
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 post to this group, send email to lav...@googlegroups.com.
Visit this group at https://groups.google.com/group/lavaan.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/4deb1627-ada2-4c41-b718-495057087007%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ana Inés

unread,
Jul 18, 2019, 4:55:18 AM7/18/19
to lavaan
Thank you very much, Christopher, for your answer!

I used the same code as you.

Then read carefully the document Data Output and and try write.csv2() ("write.csv2 uses a comma for the decimal point and a semicolon for the separator, the Excel convention for CSV files in some Western European locales." :-)

Problem solved!

A.
To unsubscribe from this group and stop receiving emails from it, send an email to lav...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages