Data from R to SPSS Modeler

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Data from R to SPSS Modeler

Lumar
Dear Team,
I have a problem concerning the integration of estimated R-Statistics Data back to SPSS Modeler.

FIrst I want to explain what I wanted to do:
1) I do my data preparations in SPSS MOdeler
2) I want to estimate some parameters in R-Statistics
3) These parameters I want to use in the SPSS Modeler Stream again.

How I tried to do it:
R-Statistics Plug-IN is installed and works correctly. After installing and testing the R-Plug-In I wrote following Code:

BEGIN PROGRAM R.
spss=read.table(file="C://Users//Backup//Desktop//test.txt", header=TRUE, sep=",")

               
attach(spss)
library(survival)
surv=Surv(IPT_HIS_REAL)

survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss, dist="weibull",scale=2.0)
summary(survival.data.weibull)

coeff=summary(survival.data.weibull)$coefficients[1:4]
coeff
coeff2=as.data.frame(coeff)
coeff2

wert=c("coeff2"," ",0, "F10.7", "scale")
dict=spssdictionary.CreateSPSSDictionary(wert)
spssdictionary.SetDictionaryToSPSS("results",dict)
spssdata.SetDataToSPSS("results", coeff2)
spssdictionary.EndDataStep()


END PROGRAM.

At the end of the code I tried to create a new active dataset which I hoped to save as external Data on my desktop. The Problem is that I´m not sure how to handle this problem.

Please could you help me to integrate the parameters into the Modeler?!

Thanks in advance.
Best regards,

Stefan
Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Jon K Peck
You are almost there.  The result-saving step creates a new Statistics dataset, but it does not save it.  After the END PROGRAM, you need standard Statistics syntax to activate the results dataset and a SAVE command.  This will not make Modeler pick it up in that stream, since you are presumably using a Statistics Output node, but it can be picked up in a new stream, and you could use Modeler scripting to tie the two together.

Other notes.
No need for the attach command.
Usually the data coming into a Statistics Output node is determined by the Modeler stream, so you might want to use that dataset, which is available via spssdata.GetDataFromSPSS rather than reading an unrelated file.

HTH,

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Lumar <[hidden email]>
To:        [hidden email],
Date:        11/19/2012 07:35 AM
Subject:        [SPSSX-L] Data from R to SPSS Modeler
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Dear Team,
I have a problem concerning the integration of estimated R-Statistics Data
back to SPSS Modeler.

*FIrst I want to explain what I wanted to do:*
1) I do my data preparations in SPSS MOdeler
2) I want to estimate some parameters in R-Statistics
3) These parameters I want to use in the SPSS Modeler Stream again.

*How I tried to do it:*
R-Statistics Plug-IN is installed and works correctly. After installing and
testing the R-Plug-In I wrote following Code:

BEGIN PROGRAM R.
spss=read.table(file="C://Users//Backup//Desktop//test.txt", header=TRUE,
sep=",")


attach(spss)
library(survival)
surv=Surv(IPT_HIS_REAL)

survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
dist="weibull",scale=2.0)
summary(survival.data.weibull)

coeff=summary(survival.data.weibull)$coefficients[1:4]
coeff
coeff2=as.data.frame(coeff)
coeff2

wert=c("coeff2"," ",0, "F10.7", "scale")
dict=spssdictionary.CreateSPSSDictionary(wert)
spssdictionary.SetDictionaryToSPSS("results",dict)
spssdata.SetDataToSPSS("results", coeff2)
spssdictionary.EndDataStep()


END PROGRAM.

At the end of the code I tried to create a new active dataset which I hoped
to save as external Data on my desktop. The Problem is that I´m not sure how
to handle this problem.

Please could you help me to integrate the parameters into the Modeler?!

Thanks in advance.
Best regards,

Stefan



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Lumar
First, Thank you for your Help! :-)

I added some code for activating and saving the dataset. Until the Activation it works without problem. But after the Save-Code I got an error.

My Code:

BEGIN PROGRAM R.
spss=read.table(file="C://Users//Backup//Desktop//test.txt", header=TRUE, sep=",")

               
attach(spss)
library(survival)
surv=Surv(IPT_HIS_REAL)

survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss, dist="weibull",scale=2.0)
summary(survival.data.weibull)

coeff=summary(survival.data.weibull)$coefficients[1:4]
coeff
coeff2=as.data.frame(coeff)
coeff2

wert=c("coeff2"," ",0, "F10.7", "scale")
dict=spssdictionary.CreateSPSSDictionary(wert)
spssdictionary.SetDictionaryToSPSS("results",dict)
spssdata.SetDataToSPSS("results", coeff2)
spssdictionary.EndDataStep()

END PROGRAM.
DATASET ACTIVATE results
SAVE OUTFILE="/data/test567.sav".

There must be some error but I couldn´t find it. The Error-Text means following to me:
After the equal sign should stand a name but the programm could only find /data/test567.sav.


Please could you help me to solve that problem?!

Best Regards,
Stefan
Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Jon K Peck
Try this simple program in the Statistics Output node.  It produces a proper results.sav file for me.  Perhaps you need a drive letter in the SAVE command.

data list list/x y.
begin data
1 4
2 5
3 6
end data
dataset name fred.
begin program r.
dta = spssdata.GetDataFromSPSS()
print(summary(dta))
dict = spssdictionary.CreateSPSSDictionary(c('x','',0,"F8.0", "scale"),
c('y', '',0,"F8.0", "scale"))
spssdictionary.SetDictionaryToSPSS("results", dict)
print(dta)
spssdata.SetDataToSPSS("results", dta)
spssdictionary.EndDataStep()
end program.
dataset activate results.
save outfile="c:/temp/results.sav".


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Lumar <[hidden email]>
To:        [hidden email],
Date:        11/19/2012 08:46 AM
Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




First, Thank you for your Help! :-)

I added some code for activating and saving the dataset. Until the
Activation it works without problem. But after the Save-Code I got an error.

*My Code:*

BEGIN PROGRAM R.
spss=read.table(file="C://Users//Backup//Desktop//test.txt", header=TRUE,
sep=",")


attach(spss)
library(survival)
surv=Surv(IPT_HIS_REAL)

survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
dist="weibull",scale=2.0)
summary(survival.data.weibull)

coeff=summary(survival.data.weibull)$coefficients[1:4]
coeff
coeff2=as.data.frame(coeff)
coeff2

wert=c("coeff2"," ",0, "F10.7", "scale")
dict=spssdictionary.CreateSPSSDictionary(wert)
spssdictionary.SetDictionaryToSPSS("results",dict)
spssdata.SetDataToSPSS("results", coeff2)
spssdictionary.EndDataStep()

END PROGRAM.
DATASET ACTIVATE results
SAVE OUTFILE="/data/test567.sav".

There must be some error but I couldn´t find it. The Error-Text means
following to me:
After the equal sign should stand a name but the programm could only find
/data/test567.sav.


Please could you help me to solve that problem?!

Best Regards,
Stefan



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716315.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Maguin, Eugene
In reply to this post by Lumar
Lumar,
I see that you have already gotten a reply from Jon who knows a lot more about R & spss than I do. You don't offer the text of the error message but it seems to be in one of these two lines.

DATASET ACTIVATE results
SAVE OUTFILE="/data/test567.sav".

Perhaps I'm wrong about the syntax rules in this context but I'll point out that there is a '.' missing after 'results'.

Gene Maguin

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Lumar
Sent: Monday, November 19, 2012 10:30 AM
To: [hidden email]
Subject: Re: Data from R to SPSS Modeler

First, Thank you for your Help! :-)

I added some code for activating and saving the dataset. Until the Activation it works without problem. But after the Save-Code I got an error.

*My Code:*

BEGIN PROGRAM R.
spss=read.table(file="C://Users//Backup//Desktop//test.txt", header=TRUE,
sep=",")


attach(spss)
library(survival)
surv=Surv(IPT_HIS_REAL)

survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
dist="weibull",scale=2.0)
summary(survival.data.weibull)

coeff=summary(survival.data.weibull)$coefficients[1:4]
coeff
coeff2=as.data.frame(coeff)
coeff2

wert=c("coeff2"," ",0, "F10.7", "scale")
dict=spssdictionary.CreateSPSSDictionary(wert)
spssdictionary.SetDictionaryToSPSS("results",dict)
spssdata.SetDataToSPSS("results", coeff2)
spssdictionary.EndDataStep()

END PROGRAM.
DATASET ACTIVATE results
SAVE OUTFILE="/data/test567.sav".

There must be some error but I couldn´t find it. The Error-Text means following to me:
After the equal sign should stand a name but the programm could only find /data/test567.sav.


Please could you help me to solve that problem?!

Best Regards,
Stefan



--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716315.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Albert-Jan Roskam
In reply to this post by Jon K Peck
It's a little cumbersome (though flexible) to write things like  "wert=c("coeff2"," ",0, "F10.7", "scale")"
Isn't it possible to coerce a dataframe into spss data where double/float becomes F10.7/scale, numeric/int becomes F8.0/scale, factor becomes F8.0/nominal and character becomes A-format/nominal?
something like: as.savFile(as.date.frame(summary(survival.data.weibull)$coefficients[1:4]))
 
Regards,
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
From: Jon K Peck <[hidden email]>
To: [hidden email]
Sent: Monday, November 19, 2012 5:29 PM
Subject: Re: [SPSSX-L] Data from R to SPSS Modeler

Try this simple program in the Statistics Output node.  It produces a proper results.sav file for me.  Perhaps you need a drive letter in the SAVE command.

data list list/x y.
begin data
1 4
2 5
3 6
end data
dataset name fred.
begin program r.
dta = spssdata.GetDataFromSPSS()
print(summary(dta))
dict = spssdictionary.CreateSPSSDictionary(c('x','',0,"F8.0", "scale"),
c('y', '',0,"F8.0", "scale"))
spssdictionary.SetDictionaryToSPSS("results", dict)
print(dta)
spssdata.SetDataToSPSS("results", dta)
spssdictionary.EndDataStep()
end program.
dataset activate results.
save outfile="c:/temp/results.sav".


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Lumar <[hidden email]>
To:        [hidden email],
Date:        11/19/2012 08:46 AM
Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
Sent by:        "SPSSX(r) Discussion" <[hidden email]>



First, Thank you for your Help! :-)

I added some code for activating and saving the dataset. Until the
Activation it works without problem. But after the Save-Code I got an error.

*My Code:*

BEGIN PROGRAM R.
spss=read.table(file="C://Users//Backup//Desktop//test.txt", header=TRUE,
sep=",")


attach(spss)
library(survival)
surv=Surv(IPT_HIS_REAL)

survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
dist="weibull",scale=2.0)
summary(survival.data.weibull)

coeff=summary(survival.data.weibull)$coefficients[1:4]
coeff
coeff2=as.data.frame(coeff)
coeff2

wert=c("coeff2"," ",0, "F10.7", "scale")
dict=spssdictionary.CreateSPSSDictionary(wert)
spssdictionary.SetDictionaryToSPSS("results",dict)
spssdata.SetDataToSPSS("results", coeff2)
spssdictionary.EndDataStep()

END PROGRAM.
DATASET ACTIVATE results
SAVE OUTFILE="/data/test567.sav".

There must be some error but I couldn´t find it. The Error-Text means
following to me:
After the equal sign should stand a name but the programm could only find
/data/test567.sav.


Please could you help me to solve that problem?!

Best Regards,
Stefan



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716315.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD




Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Jon K Peck
It would be straightforward (as much as anything in R is straightforward) to write a function to simplify the dictionary specification for a new dataset taking a data frame as you describe.  I wouldn't call it savFile, though, since it could only generate a dataset, not an actual sav file.  It would be worth, also, mapping ordered factors to ordinal level.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Albert-Jan Roskam <[hidden email]>
To:        Jon K Peck/Chicago/IBM@IBMUS, "[hidden email]" <[hidden email]>,
Date:        11/20/2012 06:29 AM
Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler




It's a little cumbersome (though flexible) to write things like  "wert=c("coeff2"," ",0, "F10.7", "scale")"
Isn't it possible to coerce a dataframe into spss data where double/float becomes F10.7/scale, numeric/int becomes F8.0/scale, factor becomes F8.0/nominal and character becomes A-format/nominal?

something like: as.savFile(as.date.frame(summary(survival.data.weibull)$coefficients[1:4]))

Regards,
Albert-Jan



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From: Jon K Peck <[hidden email]>
To:
[hidden email]
Sent:
Monday, November 19, 2012 5:29 PM
Subject:
Re: [SPSSX-L] Data from R to SPSS Modeler


Try this simple program in the Statistics Output node.  It produces a proper results.sav file for me.  Perhaps you need a drive letter in the SAVE command.

data list list/x y.

begin data

1 4

2 5

3 6

end data

dataset name fred.

begin program r.

dta = spssdata.GetDataFromSPSS()

print(summary(dta))

dict = spssdictionary.CreateSPSSDictionary(c('x','',0,"F8.0", "scale"),

c('y', '',0,"F8.0", "scale"))

spssdictionary.SetDictionaryToSPSS("results", dict)

print(dta)

spssdata.SetDataToSPSS("results", dta)

spssdictionary.EndDataStep()

end program.

dataset activate results.

save outfile="c:/temp/results.sav".



Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621





From:        
Lumar <[hidden email]>
To:        
[hidden email],
Date:        
11/19/2012 08:46 AM
Subject:        
Re: [SPSSX-L] Data from R to SPSS Modeler
Sent by:        
"SPSSX(r) Discussion" <[hidden email]>



First, Thank you for your Help! :-)

I added some code for activating and saving the dataset. Until the
Activation it works without problem. But after the Save-Code I got an error.

*My Code:*

BEGIN PROGRAM R.
spss=read.table(file="C://Users//Backup//Desktop//test.txt", header=TRUE,
sep=",")


attach(spss)
library(survival)
surv=Surv(IPT_HIS_REAL)

survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
dist="weibull",scale=2.0)
summary(survival.data.weibull)

coeff=summary(survival.data.weibull)$coefficients[1:4]
coeff
coeff2=as.data.frame(coeff)
coeff2

wert=c("coeff2"," ",0, "F10.7", "scale")
dict=spssdictionary.CreateSPSSDictionary(wert)
spssdictionary.SetDictionaryToSPSS("results",dict)
spssdata.SetDataToSPSS("results", coeff2)
spssdictionary.EndDataStep()

END PROGRAM.
DATASET ACTIVATE results
SAVE OUTFILE="/data/test567.sav".

There must be some error but I couldn´t find it. The Error-Text means
following to me:
After the equal sign should stand a name but the programm could only find
/data/test567.sav.


Please could you help me to solve that problem?!

Best Regards,
Stefan



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716315.html
Sent from the SPSSX Discussion mailing list archive at
Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD




Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Albert-Jan Roskam
"as much as anything in R is straightforward": so True!
 
begin program r.
options(stringsAsFactors=FALSE)
dta <- data.frame(aFactor=factor(c(1, 2, 3)), aFloat=as.double(c(1, 2, 3)), aOrdered=ordered(c(1, 2, 3)), aChar=as.character(c("1", "2", "3")))
width <- max(sapply(dta[sapply(dta, is.character)], nchar), na.rm=TRUE) # max is not really ideal
strVar <- sprintf("A%d", width)
printTypes <- c(factor="F8.0", numeric="F12.2", ordered="F8.0", character=strVar)
varTypes <- c(factor=0, numeric=0, ordered=0, character=width)
measLevels <- c(factor="nominal", numeric="scale", ordered="ordinal", character="nominal")
dataTypes <- lapply(sapply(dta, class), function(x) x[[1]])
varNames <- names(dataTypes)
names(varNames) <- names(varTypes)
spssDictInfo <- lapply(dataTypes, function(arg)  c(varNames[arg], "''", varTypes[arg][[1]], printTypes[arg][[1]], measLevels[arg][[1]]))
dict = spssdictionary.CreateSPSSDictionary( spssDictInfo )
spssdictionary.SetDictionaryToSPSS("results", dict)
end program.
 
Regards,
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
From: Jon K Peck <[hidden email]>
To: [hidden email]
Sent: Tuesday, November 20, 2012 3:34 PM
Subject: Re: [SPSSX-L] Data from R to SPSS Modeler

It would be straightforward (as much as anything in R is straightforward) to write a function to simplify the dictionary specification for a new dataset taking a data frame as you describe.  I wouldn't call it savFile, though, since it could only generate a dataset, not an actual sav file.  It would be worth, also, mapping ordered factors to ordinal level.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Albert-Jan Roskam <[hidden email]>
To:        Jon K Peck/Chicago/IBM@IBMUS, "[hidden email]" <[hidden email]>,
Date:        11/20/2012 06:29 AM
Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler



It's a little cumbersome (though flexible) to write things like  "wert=c("coeff2"," ",0, "F10.7", "scale")"
Isn't it possible to coerce a dataframe into spss data where double/float becomes F10.7/scale, numeric/int becomes F8.0/scale, factor becomes F8.0/nominal and character becomes A-format/nominal?

something like: as.savFile(as.date.frame(summary(survival.data.weibull)$coefficients[1:4]))

Regards,
Albert-Jan



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From: Jon K Peck <[hidden email]>
To:
[hidden email]
Sent:
Monday, November 19, 2012 5:29 PM
Subject:
Re: [SPSSX-L] Data from R to SPSS Modeler


Try this simple program in the Statistics Output node.  It produces a proper results.sav file for me.  Perhaps you need a drive letter in the SAVE command.

data list list/x y.

begin data

1 4

2 5

3 6

end data

dataset name fred.

begin program r.

dta = spssdata.GetDataFromSPSS()

print(summary(dta))

dict = spssdictionary.CreateSPSSDictionary(c('x','',0,"F8.0", "scale"),

c('y', '',0,"F8.0", "scale"))

spssdictionary.SetDictionaryToSPSS("results", dict)

print(dta)

spssdata.SetDataToSPSS("results", dta)

spssdictionary.EndDataStep()

end program.

dataset activate results.

save outfile="c:/temp/results.sav".



Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621





From:        
Lumar <[hidden email]>
To:        
[hidden email],
Date:        
11/19/2012 08:46 AM
Subject:        
Re: [SPSSX-L] Data from R to SPSS Modeler
Sent by:        
"SPSSX(r) Discussion" <[hidden email]>



First, Thank you for your Help! :-)

I added some code for activating and saving the dataset. Until the
Activation it works without problem. But after the Save-Code I got an error.

*My Code:*

BEGIN PROGRAM R.
spss=read.table(file="C://Users//Backup//Desktop//test.txt", header=TRUE,
sep=",")


attach(spss)
library(survival)
surv=Surv(IPT_HIS_REAL)

survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
dist="weibull",scale=2.0)
summary(survival.data.weibull)

coeff=summary(survival.data.weibull)$coefficients[1:4]
coeff
coeff2=as.data.frame(coeff)
coeff2

wert=c("coeff2"," ",0, "F10.7", "scale")
dict=spssdictionary.CreateSPSSDictionary(wert)
spssdictionary.SetDictionaryToSPSS("results",dict)
spssdata.SetDataToSPSS("results", coeff2)
spssdictionary.EndDataStep()

END PROGRAM.
DATASET ACTIVATE results
SAVE OUTFILE="/data/test567.sav".

There must be some error but I couldn´t find it. The Error-Text means
following to me:
After the equal sign should stand a name but the programm could only find
/data/test567.sav.


Please could you help me to solve that problem?!

Best Regards,
Stefan



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716315.html
Sent from the SPSSX Discussion mailing list archive at
Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD






Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

David Marso
Administrator
I am truly bRamaged after staring at that mess ;-)
How about a few in line comments for the benefit of pedagogical purposes.
I am convinced that writing tons of uncommented R code is a built in step to job security.
Poke it with a stick?  Jello code jumping off the table!
B0 + B1*(Free software + expensive consultants - documentation ) + B2 * time =$$$$


Albert-Jan Roskam wrote
"as much as anything in R is straightforward": so True!
 
begin program r.
options(stringsAsFactors=FALSE)
dta <- data.frame(aFactor=factor(c(1, 2, 3)), aFloat=as.double(c(1, 2, 3)), aOrdered=ordered(c(1, 2, 3)), aChar=as.character(c("1", "2", "3")))

width <- max(sapply(dta[sapply(dta, is.character)], nchar), na.rm=TRUE) # max is not really ideal
strVar <- sprintf("A%d", width)
printTypes <- c(factor="F8.0", numeric="F12.2", ordered="F8.0", character=strVar)
varTypes <- c(factor=0, numeric=0, ordered=0, character=width)
measLevels <- c(factor="nominal", numeric="scale", ordered="ordinal", character="nominal")
dataTypes <- lapply(sapply(dta, class), function(x) x[[1]])
varNames <- names(dataTypes)
names(varNames) <- names(varTypes)
spssDictInfo <- lapply(dataTypes, function(arg)  c(varNames[arg], "''", varTypes[arg][[1]], printTypes[arg][[1]], measLevels[arg][[1]]))
dict = spssdictionary.CreateSPSSDictionary( spssDictInfo )
spssdictionary.SetDictionaryToSPSS("results", dict)
end program.


Regards,
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 


>________________________________
>From: Jon K Peck <[hidden email]>
>To: [hidden email] 
>Sent: Tuesday, November 20, 2012 3:34 PM
>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>
>
>It would be straightforward (as much as anything in R is straightforward) to write a function to simplify the dictionary specification for a new dataset taking a data frame as you describe.  I wouldn't call it savFile, though, since it could only generate a dataset, not an actual sav file.  It would be worth, also, mapping ordered factors to ordinal level.
>
>
>Jon Peck (no "h") aka Kim
>Senior Software Engineer, IBM
>[hidden email]
>new phone: 720-342-5621
>
>
>
>
>From:        Albert-Jan Roskam <[hidden email]>
>To:        Jon K Peck/Chicago/IBM@IBMUS, "[hidden email]" <[hidden email]>,
>Date:        11/20/2012 06:29 AM
>Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
>
>>________________________________
>
>
>
>It's a little cumbersome (though flexible) to write things like  "wert=c("coeff2"," ",0, "F10.7", "scale")"
>Isn't it possible to coerce a dataframe into spss data where double/float becomes F10.7/scale, numeric/int becomes F8.0/scale, factor becomes F8.0/nominal and character becomes A-format/nominal?
>something like: as.savFile(as.date.frame(summary(survival.data.weibull)$coefficients[1:4]))
>
>Regards,
>Albert-Jan
>
>
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
>fresh water system, and public health, what have the Romans ever done for us?
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>From: Jon K Peck <[hidden email]>
>To: [hidden email] 
>Sent: Monday, November 19, 2012 5:29 PM
>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>
>Try this simple program in the Statistics Output node.  It produces a proper results.sav file for me.  Perhaps you need a drive letter in the SAVE command.
>
>data list list/x y.
>begin data
>1 4
>2 5
>3 6
>end data
>dataset name fred.
>begin program r.
>dta = spssdata.GetDataFromSPSS()
>print(summary(dta))
>dict = spssdictionary.CreateSPSSDictionary(c('x','',0,"F8.0", "scale"),
>c('y', '',0,"F8.0", "scale"))
>spssdictionary.SetDictionaryToSPSS("results", dict)
>print(dta)
>spssdata.SetDataToSPSS("results", dta)
>spssdictionary.EndDataStep()
>end program.
>dataset activate results.
>save outfile="c:/temp/results.sav".
>
>
>Jon Peck (no "h") aka Kim
>Senior Software Engineer, IBM
>[hidden email]
>new phone: 720-342-5621
>
>
>
>
>From:        Lumar <[hidden email]>
>To:        [hidden email],
>Date:        11/19/2012 08:46 AM
>Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
>Sent by:        "SPSSX(r) Discussion" <[hidden email]>
>
>
>
>First, Thank you for your Help! :-)
>
>I added some code for activating and saving the dataset. Until the
>Activation it works without problem. But after the Save-Code I got an error.
>
>*My Code:*
>
>BEGIN PROGRAM R.
>spss=read.table(file="C://Users//Backup//Desktop//test.txt", header=TRUE,
>sep=",")
>
>
>attach(spss)
>library(survival)
>surv=Surv(IPT_HIS_REAL)
>
>survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
>dist="weibull",scale=2.0)
>summary(survival.data.weibull)
>
>coeff=summary(survival.data.weibull)$coefficients[1:4]
>coeff
>coeff2=as.data.frame(coeff)
>coeff2
>
>wert=c("coeff2"," ",0, "F10.7", "scale")
>dict=spssdictionary.CreateSPSSDictionary(wert)
>spssdictionary.SetDictionaryToSPSS("results",dict)
>spssdata.SetDataToSPSS("results", coeff2)
>spssdictionary.EndDataStep()
>
>END PROGRAM.
>DATASET ACTIVATE results
>SAVE OUTFILE="/data/test567.sav".
>
>There must be some error but I couldn´t find it. The Error-Text means
>following to me:
>After the equal sign should stand a name but the programm could only find
>/data/test567.sav.
>
>
>Please could you help me to solve that problem?!
>
>Best Regards,
>Stefan
>
>
>
>--
>View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716315.html
>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
>=====================
>To manage your subscription to SPSSX-L, send a message to
>[hidden email] (not to SPSSX-L), with no body text except the
>command. To leave the list, send the command
>SIGNOFF SPSSX-L
>For a list of commands to manage subscriptions, send the command
>INFO REFCARD
>
>
>
>
>
>
>
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Jon K Peck
As R code goes, this is actually pretty straightforward :-(  But I think this was just an example.  Some say that R is easy to learn.  My take is that R is just about the most difficult to learn of any of the dozens of programming languages that I have experienced.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email],
Date:        11/20/2012 09:31 AM
Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I am truly bRamaged after staring at that mess ;-)
How about a few in line comments for the benefit of pedagogical purposes.
I am convinced that writing tons of uncommented R code is a built in step to
job security.
Poke it with a stick?  Jello code jumping off the table!
B0 + B1*(Free software + expensive consultants - documentation ) + B2 * time
=$$$$



Albert-Jan Roskam wrote
> "as much as anything in R is straightforward": so True!
>
> begin program r.
> options(stringsAsFactors=FALSE)
> dta <- data.frame(aFactor=factor(c(1, 2, 3)), aFloat=as.double(c(1, 2,
> 3)), aOrdered=ordered(c(1, 2, 3)), aChar=as.character(c("1", "2", "3")))
>
> width <- max(sapply(dta[sapply(dta, is.character)], nchar), na.rm=TRUE) #
> max is not really ideal
> strVar <- sprintf("A%d", width)
> printTypes <- c(factor="F8.0", numeric="F12.2", ordered="F8.0",
> character=strVar)
> varTypes <- c(factor=0, numeric=0, ordered=0, character=width)
> measLevels <- c(factor="nominal", numeric="scale", ordered="ordinal",
> character="nominal")
> dataTypes <- lapply(sapply(dta, class), function(x) x[[1]])
> varNames <- names(dataTypes)
> names(varNames) <- names(varTypes)
> spssDictInfo <- lapply(dataTypes, function(arg)  c(varNames[arg], "''",
> varTypes[arg][[1]], printTypes[arg][[1]], measLevels[arg][[1]]))
> dict = spssdictionary.CreateSPSSDictionary( spssDictInfo )
> spssdictionary.SetDictionaryToSPSS("results", dict)
> end program.
>
>
> Regards,
> Albert-Jan
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> All right, but apart from the sanitation, the medicine, education, wine,
> public order, irrigation, roads, a
> fresh water system, and public health, what have the Romans ever done for
> us?
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>>________________________________
>>From: Jon K Peck &lt;

> peck@.ibm

> &gt;
>>To:

> SPSSX-L@.UGA

>
>>Sent: Tuesday, November 20, 2012 3:34 PM
>>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>>
>>
>>It would be straightforward (as much as anything in R is straightforward)
to write a function to simplify the dictionary specification for a new
dataset taking a data frame as you describe.  I wouldn't call it savFile,
though, since it could only generate a dataset, not an actual sav file.  It
would be worth, also, mapping ordered factors to ordinal level.
>>
>>
>>Jon Peck (no "h") aka Kim
>>Senior Software Engineer, IBM
>>

> peck@.ibm

>>new phone: 720-342-5621
>>
>>
>>
>>
>>From:        Albert-Jan Roskam &lt;

> fomcl@

> &gt;
>>To:        Jon K Peck/Chicago/IBM@IBMUS, "

> SPSSX-L@.UGA

> " &lt;

> SPSSX-L@.UGA

> &gt;,
>>Date:        11/20/2012 06:29 AM
>>Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
>>
>>>________________________________
>>
>>
>>
>>It's a little cumbersome (though flexible) to write things like
"wert=c("coeff2"," ",0, "F10.7", "scale")"
>>Isn't it possible to coerce a dataframe into spss data where double/float
becomes F10.7/scale, numeric/int becomes F8.0/scale, factor becomes
F8.0/nominal and character becomes A-format/nominal?
>>something like:
as.savFile(as.date.frame(summary(survival.data.weibull)$coefficients[1:4]))
>>
>>Regards,
>>Albert-Jan
>>
>>
>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>All right, but apart from the sanitation, the medicine, education, wine,
public order, irrigation, roads, a
>>fresh water system, and public health, what have the Romans ever done for
us?
>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>From: Jon K Peck &lt;

> peck@.ibm

> &gt;
>>To:

> SPSSX-L@.UGA

>
>>Sent: Monday, November 19, 2012 5:29 PM
>>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>>
>>Try this simple program in the Statistics Output node.  It produces a
proper results.sav file for me.  Perhaps you need a drive letter in the SAVE
command.
>>
>>data list list/x y.
>>begin data
>>1 4
>>2 5
>>3 6
>>end data
>>dataset name fred.
>>begin program r.
>>dta = spssdata.GetDataFromSPSS()
>>print(summary(dta))
>>dict = spssdictionary.CreateSPSSDictionary(c('x','',0,"F8.0", "scale"),
>>c('y', '',0,"F8.0", "scale"))
>>spssdictionary.SetDictionaryToSPSS("results", dict)
>>print(dta)
>>spssdata.SetDataToSPSS("results", dta)
>>spssdictionary.EndDataStep()
>>end program.
>>dataset activate results.
>>save outfile="c:/temp/results.sav".
>>
>>
>>Jon Peck (no "h") aka Kim
>>Senior Software Engineer, IBM
>>

> peck@.ibm

>>new phone: 720-342-5621
>>
>>
>>
>>
>>From:        Lumar &lt;

> Stefili@

> &gt;
>>To:      

> SPSSX-L@.uga

> ,
>>Date:        11/19/2012 08:46 AM
>>Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
>>Sent by:        "SPSSX(r) Discussion" &lt;

> SPSSX-L@.uga

> &gt;
>>
>>
>>
>>First, Thank you for your Help! :-)
>>
>>I added some code for activating and saving the dataset. Until the
>>Activation it works without problem. But after the Save-Code I got an
error.
>>
>>*My Code:*
>>
>>BEGIN PROGRAM R.
>>spss=read.table(file="C://Users//Backup//Desktop//test.txt", header=TRUE,
>>sep=",")
>>
>>
>>attach(spss)
>>library(survival)
>>surv=Surv(IPT_HIS_REAL)
>>
>>survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
>>dist="weibull",scale=2.0)
>>summary(survival.data.weibull)
>>
>>coeff=summary(survival.data.weibull)$coefficients[1:4]
>>coeff
>>coeff2=as.data.frame(coeff)
>>coeff2
>>
>>wert=c("coeff2"," ",0, "F10.7", "scale")
>>dict=spssdictionary.CreateSPSSDictionary(wert)
>>spssdictionary.SetDictionaryToSPSS("results",dict)
>>spssdata.SetDataToSPSS("results", coeff2)
>>spssdictionary.EndDataStep()
>>
>>END PROGRAM.
>>DATASET ACTIVATE results
>>SAVE OUTFILE="/data/test567.sav".
>>
>>There must be some error but I couldn´t find it. The Error-Text means
>>following to me:
>>After the equal sign should stand a name but the programm could only find
>>/data/test567.sav.
>>
>>
>>Please could you help me to solve that problem?!
>>
>>Best Regards,
>>Stefan
>>
>>
>>
>>--
>>View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716315.html
>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>
>>=====================
>>To manage your subscription to SPSSX-L, send a message to
>>

> LISTSERV@.UGA

>  (not to SPSSX-L), with no body text except the
>>command. To leave the list, send the command
>>SIGNOFF SPSSX-L
>>For a list of commands to manage subscriptions, send the command
>>INFO REFCARD
>>
>>
>>
>>
>>
>>
>>





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716350.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

David Marso
Administrator
"As R code goes, this is actually pretty straightforward :-(  "..
I was afraid that was the case!
So far my experience as well.  Currently creating SPSS code to implement methods in a couple of articles.  Also have R code which would hopefully have illuminated some of the trickier parts of the equations (no such luck).
Just for my edification Jon:  
What is it about R that makes it the 'most difficult to learn' in your experience?
---  
Jon K Peck wrote
As R code goes, this is actually pretty straightforward :-(  But I think
this was just an example.  Some say that R is easy to learn.  My take is
that R is just about the most difficult to learn of any of the dozens of
programming languages that I have experienced.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:   David Marso <[hidden email]>
To:     [hidden email],
Date:   11/20/2012 09:31 AM
Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
Sent by:        "SPSSX(r) Discussion" <[hidden email]>



I am truly bRamaged after staring at that mess ;-)
How about a few in line comments for the benefit of pedagogical purposes.
I am convinced that writing tons of uncommented R code is a built in step
to
job security.
Poke it with a stick?  Jello code jumping off the table!
B0 + B1*(Free software + expensive consultants - documentation ) + B2 *
time
=$$$$



Albert-Jan Roskam wrote
> "as much as anything in R is straightforward": so True!
>
> begin program r.
> options(stringsAsFactors=FALSE)
> dta <- data.frame(aFactor=factor(c(1, 2, 3)), aFloat=as.double(c(1, 2,
> 3)), aOrdered=ordered(c(1, 2, 3)), aChar=as.character(c("1", "2", "3")))
>
> width <- max(sapply(dta[sapply(dta, is.character)], nchar), na.rm=TRUE)
#
> max is not really ideal
> strVar <- sprintf("A%d", width)
> printTypes <- c(factor="F8.0", numeric="F12.2", ordered="F8.0",
> character=strVar)
> varTypes <- c(factor=0, numeric=0, ordered=0, character=width)
> measLevels <- c(factor="nominal", numeric="scale", ordered="ordinal",
> character="nominal")
> dataTypes <- lapply(sapply(dta, class), function(x) x[[1]])
> varNames <- names(dataTypes)
> names(varNames) <- names(varTypes)
> spssDictInfo <- lapply(dataTypes, function(arg)  c(varNames[arg], "''",
> varTypes[arg][[1]], printTypes[arg][[1]], measLevels[arg][[1]]))
> dict = spssdictionary.CreateSPSSDictionary( spssDictInfo )
> spssdictionary.SetDictionaryToSPSS("results", dict)
> end program.
>
>
> Regards,
> Albert-Jan
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> All right, but apart from the sanitation, the medicine, education, wine,
> public order, irrigation, roads, a
> fresh water system, and public health, what have the Romans ever done
for
> us?
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>>________________________________
>>From: Jon K Peck <

> peck@.ibm

> >
>>To:

> SPSSX-L@.UGA

>
>>Sent: Tuesday, November 20, 2012 3:34 PM
>>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>>
>>
>>It would be straightforward (as much as anything in R is
straightforward)
to write a function to simplify the dictionary specification for a new
dataset taking a data frame as you describe.  I wouldn't call it savFile,
though, since it could only generate a dataset, not an actual sav file. It
would be worth, also, mapping ordered factors to ordinal level.
>>
>>
>>Jon Peck (no "h") aka Kim
>>Senior Software Engineer, IBM
>>

> peck@.ibm

>>new phone: 720-342-5621
>>
>>
>>
>>
>>From:        Albert-Jan Roskam <

> fomcl@

> >
>>To:        Jon K Peck/Chicago/IBM@IBMUS, "

> SPSSX-L@.UGA

> " <

> SPSSX-L@.UGA

> >,
>>Date:        11/20/2012 06:29 AM
>>Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
>>
>>>________________________________
>>
>>
>>
>>It's a little cumbersome (though flexible) to write things like
 "wert=c("coeff2"," ",0, "F10.7", "scale")"
>>Isn't it possible to coerce a dataframe into spss data where
double/float
becomes F10.7/scale, numeric/int becomes F8.0/scale, factor becomes
F8.0/nominal and character becomes A-format/nominal?
>>something like:
as.savFile(as.date.frame(summary(survival.data.weibull)$coefficients[1:4]))
>>
>>Regards,
>>Albert-Jan
>>
>>
>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>All right, but apart from the sanitation, the medicine, education, wine,
public order, irrigation, roads, a
>>fresh water system, and public health, what have the Romans ever done
for
us?
>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>From: Jon K Peck <

> peck@.ibm

> >
>>To:

> SPSSX-L@.UGA

>
>>Sent: Monday, November 19, 2012 5:29 PM
>>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>>
>>Try this simple program in the Statistics Output node.  It produces a
proper results.sav file for me.  Perhaps you need a drive letter in the
SAVE
command.
>>
>>data list list/x y.
>>begin data
>>1 4
>>2 5
>>3 6
>>end data
>>dataset name fred.
>>begin program r.
>>dta = spssdata.GetDataFromSPSS()
>>print(summary(dta))
>>dict = spssdictionary.CreateSPSSDictionary(c('x','',0,"F8.0", "scale"),
>>c('y', '',0,"F8.0", "scale"))
>>spssdictionary.SetDictionaryToSPSS("results", dict)
>>print(dta)
>>spssdata.SetDataToSPSS("results", dta)
>>spssdictionary.EndDataStep()
>>end program.
>>dataset activate results.
>>save outfile="c:/temp/results.sav".
>>
>>
>>Jon Peck (no "h") aka Kim
>>Senior Software Engineer, IBM
>>

> peck@.ibm

>>new phone: 720-342-5621
>>
>>
>>
>>
>>From:        Lumar <

> Stefili@

> >
>>To:

> SPSSX-L@.uga

> ,
>>Date:        11/19/2012 08:46 AM
>>Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
>>Sent by:        "SPSSX(r) Discussion" <

> SPSSX-L@.uga

> >
>>
>>
>>
>>First, Thank you for your Help! :-)
>>
>>I added some code for activating and saving the dataset. Until the
>>Activation it works without problem. But after the Save-Code I got an
error.
>>
>>*My Code:*
>>
>>BEGIN PROGRAM R.
>>spss=read.table(file="C://Users//Backup//Desktop//test.txt",
header=TRUE,
>>sep=",")
>>
>>
>>attach(spss)
>>library(survival)
>>surv=Surv(IPT_HIS_REAL)
>>
>>survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
>>dist="weibull",scale=2.0)
>>summary(survival.data.weibull)
>>
>>coeff=summary(survival.data.weibull)$coefficients[1:4]
>>coeff
>>coeff2=as.data.frame(coeff)
>>coeff2
>>
>>wert=c("coeff2"," ",0, "F10.7", "scale")
>>dict=spssdictionary.CreateSPSSDictionary(wert)
>>spssdictionary.SetDictionaryToSPSS("results",dict)
>>spssdata.SetDataToSPSS("results", coeff2)
>>spssdictionary.EndDataStep()
>>
>>END PROGRAM.
>>DATASET ACTIVATE results
>>SAVE OUTFILE="/data/test567.sav".
>>
>>There must be some error but I couldn´t find it. The Error-Text means
>>following to me:
>>After the equal sign should stand a name but the programm could only
find
>>/data/test567.sav.
>>
>>
>>Please could you help me to solve that problem?!
>>
>>Best Regards,
>>Stefan
>>
>>
>>
>>--
>>View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716315.html

>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>
>>=====================
>>To manage your subscription to SPSSX-L, send a message to
>>

> LISTSERV@.UGA

>  (not to SPSSX-L), with no body text except the
>>command. To leave the list, send the command
>>SIGNOFF SPSSX-L
>>For a list of commands to manage subscriptions, send the command
>>INFO REFCARD
>>
>>
>>
>>
>>
>>
>>





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to
email me.
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716350.html

Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Albert-Jan Roskam
In reply to this post by David Marso
>I am truly bRamaged after staring at that mess ;-)
>How about a few in line comments for the benefit of pedagogical purposes.
>I am convinced that writing tons of uncommented R code is a built in step to
>job security.
>Poke it with a stick?  Jello code jumping off the table!
>B0 + B1*(Free software + expensive consultants - documentation ) + B2 * time
>=$$$$


Hahaha, sorry about that, but don't shoot the messenger! R is a language of which one forgets the details after about 15mins not using it. With lots of prentheses, curly braces and square brackets, and most defaults not what you'd like them to be.  The *ply functions may be a bit awkward in the beginning, but they're really handy once you "get" them. But it can be so hard not to think in terms of "for" loops! One option() that is missing in R is: options(dontCoerceEverything=True). I hate it that dataframes (or other data types) are coerced into lists, matrices, get additional attributes, etc. etc.


The code below is just a mapping between R datatypes and SPSS data/printtypes (spss has just two datatypes, of course) and measurement levels.
double --> F12.2, scale

numeric (int) --> F8, scale

factor --> F8, nominal

ordered --> F8. ordinal

character --> A<width>, nominal


I wrote the code because I found it awkward to specify the arguments of spssdictionary.SetDictionaryToSPSS manually. Too many lines of code ;-))))


>Albert-Jan Roskam wrote
>> "as much as anything in R is straightforward": so True!
>> Â
>> begin program r.
>> options(stringsAsFactors=FALSE)
>> dta <- data.frame(aFactor=factor(c(1, 2, 3)), aFloat=as.double(c(1, 2,
>> 3)), aOrdered=ordered(c(1, 2, 3)), aChar=as.character(c("1", "2", "3")))
>>
>> width <- max(sapply(dta[sapply(dta, is.character)], nchar), na.rm=TRUE) #
>> max is not really ideal
>> strVar <- sprintf("A%d", width)
>> printTypes <- c(factor="F8.0", numeric="F12.2", ordered="F8.0",
>> character=strVar)
>> varTypes <- c(factor=0, numeric=0, ordered=0, character=width)
>> measLevels <- c(factor="nominal", numeric="scale", ordered="ordinal",
>> character="nominal")
>> dataTypes <- lapply(sapply(dta, class), function(x) x[[1]])
>> varNames <- names(dataTypes)
>> names(varNames) <- names(varTypes)
>> spssDictInfo <- lapply(dataTypes, function(arg)Â  c(varNames[arg], "''",
>> varTypes[arg][[1]], printTypes[arg][[1]], measLevels[arg][[1]]))
>> dict = spssdictionary.CreateSPSSDictionary( spssDictInfo )
>> spssdictionary.SetDictionaryToSPSS("results", dict)
>> end program.
>>
>>
>> Regards,
>> Albert-Jan
>>
>>
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> All right, but apart from the sanitation, the medicine, education, wine,
>> public order, irrigation, roads, a
>> fresh water system, and public health, what have the Romans ever done for
>> us?
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Â
>>
>>
>>>________________________________
>>>From: Jon K Peck &lt;
>
>> peck@.ibm
>
>> &gt;
>>>To:
>
>> SPSSX-L@.UGA
>
>>
>>>Sent: Tuesday, November 20, 2012 3:34 PM
>>>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>>>
>>>
>>>It would be straightforward (as much as anything in R is straightforward)
>to write a function to simplify the dictionary specification for a new
>dataset taking a data frame as you describe. Â I wouldn't call it savFile,
>though, since it could only generate a dataset, not an actual sav file. Â It
>would be worth, also, mapping ordered factors to ordinal level.
>>>
>>>
>>>Jon Peck (no "h") aka Kim
>>>Senior Software Engineer, IBM
>>>
>
>> peck@.ibm
>
>>>new phone: 720-342-5621
>>>
>>>
>>>
>>>
>>>From: Â  Â  Â  Â Albert-Jan Roskam &lt;
>
>> fomcl@
>
>> &gt;
>>>To: Â  Â  Â  Â Jon K Peck/Chicago/IBM@IBMUS, "
>
>> SPSSX-L@.UGA
>
>> " &lt;
>
>> SPSSX-L@.UGA
>
>> &gt;,
>>>Date: Â  Â  Â  Â 11/20/2012 06:29 AM
>>>Subject: Â  Â  Â  Â Re: [SPSSX-L] Data from R to SPSS Modeler
>>>
>>>>________________________________
>>>
>>>
>>>
>>>It's a little cumbersome (though flexible) to write things like
>Â "wert=c("coeff2"," ",0, "F10.7", "scale")"
>>>Isn't it possible to coerce a dataframe into spss data where double/float
>becomes F10.7/scale, numeric/int becomes F8.0/scale, factor becomes
>F8.0/nominal and character becomes A-format/nominal?
>>>something like:
>as.savFile(as.date.frame(summary(survival.data.weibull)$coefficients[1:4]))
>>>
>>>Regards,
>>>Albert-Jan
>>>
>>>
>>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>All right, but apart from the sanitation, the medicine, education, wine,
>public order, irrigation, roads, a
>>>fresh water system, and public health, what have the Romans ever done for
>us?
>>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>From: Jon K Peck &lt;
>
>> peck@.ibm
>
>> &gt;
>>>To:
>
>> SPSSX-L@.UGA
>
>>
>>>Sent: Monday, November 19, 2012 5:29 PM
>>>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>>>
>>>Try this simple program in the Statistics Output node. Â It produces a
>proper results.sav file for me. Â Perhaps you need a drive letter in the SAVE
>command.
>>>
>>>data list list/x y.
>>>begin data
>>>1 4
>>>2 5
>>>3 6
>>>end data
>>>dataset name fred.
>>>begin program r.
>>>dta = spssdata.GetDataFromSPSS()
>>>print(summary(dta))
>>>dict = spssdictionary.CreateSPSSDictionary(c('x','',0,"F8.0", "scale"),
>>>c('y', '',0,"F8.0", "scale"))
>>>spssdictionary.SetDictionaryToSPSS("results", dict)
>>>print(dta)
>>>spssdata.SetDataToSPSS("results", dta)
>>>spssdictionary.EndDataStep()
>>>end program.
>>>dataset activate results.
>>>save outfile="c:/temp/results.sav".
>>>
>>>
>>>Jon Peck (no "h") aka Kim
>>>Senior Software Engineer, IBM
>>>
>
>> peck@.ibm
>
>>>new phone: 720-342-5621
>>>
>>>
>>>
>>>
>>>From: Â  Â  Â  Â Lumar &lt;
>
>> Stefili@
>
>> &gt;
>>>To: Â  Â  Â  Â
>
>> SPSSX-L@.uga
>
>> ,
>>>Date: Â  Â  Â  Â 11/19/2012 08:46 AM
>>>Subject: Â  Â  Â  Â Re: [SPSSX-L] Data from R to SPSS Modeler
>>>Sent by: Â  Â  Â  Â "SPSSX(r) Discussion" &lt;
>
>> SPSSX-L@.uga
>
>> &gt;
>>>
>>>
>>>
>>>First, Thank you for your Help! :-)
>>>
>>>I added some code for activating and saving the dataset. Until the
>>>Activation it works without problem. But after the Save-Code I got an
>error.
>>>
>>>*My Code:*
>>>
>>>BEGIN PROGRAM R.
>>>spss=read.table(file="C://Users//Backup//Desktop//test.txt", header=TRUE,
>>>sep=",")
>>>
>>>
>>>attach(spss)
>>>library(survival)
>>>surv=Surv(IPT_HIS_REAL)
>>>
>>>survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
>>>dist="weibull",scale=2.0)
>>>summary(survival.data.weibull)
>>>
>>>coeff=summary(survival.data.weibull)$coefficients[1:4]
>>>coeff
>>>coeff2=as.data.frame(coeff)
>>>coeff2
>>>
>>>wert=c("coeff2"," ",0, "F10.7", "scale")
>>>dict=spssdictionary.CreateSPSSDictionary(wert)
>>>spssdictionary.SetDictionaryToSPSS("results",dict)
>>>spssdata.SetDataToSPSS("results", coeff2)
>>>spssdictionary.EndDataStep()
>>>
>>>END PROGRAM.
>>>DATASET ACTIVATE results
>>>SAVE OUTFILE="/data/test567.sav".
>>>
>>>There must be some error but I couldn´t find it. The Error-Text means
>>>following to me:
>>>After the equal sign should stand a name but the programm could only find
>>>/data/test567.sav.
>>>
>>>
>>>Please could you help me to solve that problem?!
>>>
>>>Best Regards,
>>>Stefan
>>>
>>>
>>>
>>>--
>>>View this message in context:
>http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716315.html
>>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>>
>>>=====================
>>>To manage your subscription to SPSSX-L, send a message to
>>>
>
>> LISTSERV@.UGA
>
>>  (not to SPSSX-L), with no body text except the
>>>command. To leave the list, send the command
>>>SIGNOFF SPSSX-L
>>>For a list of commands to manage subscriptions, send the command
>>>INFO REFCARD
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>
>
>
>
>
>-----
>Please reply to the list and not to my personal email.
>Those desiring my consulting or training services please feel free to email me.
>--
>View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716350.html
>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
>=====================
>To manage your subscription to SPSSX-L, send a message to
>[hidden email] (not to SPSSX-L), with no body text except the
>command. To leave the list, send the command
>SIGNOFF SPSSX-L
>For a list of commands to manage subscriptions, send the command
>INFO REFCARD
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Jon K Peck
In reply to this post by David Marso
R has a huge number of packages (a problem in itself, sometimes), and powerful tools for implementing statistical and graphical algorithms.  Those libraries are probably the biggest benefit for SPSS users.  But as a language, it's pretty incoherent - somewhat object oriented, somewhat not, notationally inconsistent, (those dots in names do not indicate attributes of objects).  Syntax is C like but many things are different.  Scoping of data and functions is way too complex and does not do a good job of information hiding or abstraction.  

There are thousands of functions, so there are usually twelve different ways to do something where one general method would do - okay, maybe two.  For example, it's hard to remember which of the workhorse apply functions to use: apply, lapply, tapply, sapply, mapply, or eapply.  They all do similar but not identical things.  Many functions have quite complex argument sets, albeit often with defaults, that make them hard to use.  Because many ordinary operations are vectorized, simple things like pasting strings together can be hard to figure out.  

And package authors - anyone can contribute - are often inconsistent from one to another.  To top it off, R documentation is written "by experts for experts".  Debugging is a trial.  I spent several hours this morning figuring out how to do something that should be simple and could be done trivially in Python, but I waded through at least 30 R functions and a bunch of Googling figuring out most of what I needed.

I've copied an extract from the doc for the do.call function below as a somewhat more difficult than average example of the documentation.

Getting that off my chest!  If R were being designed today by skilled language designers, many things would be different, I believe, both in the design and in the internals, but for better or worse, those libraries and the power of the language mean that for statistical development, it's still a great resource.  And if IBM or someone else wraps R functionality in a Statistics extension command, you get the power without the pain.

-------------------------
do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it.

Usage
do.call(what, args, quote = FALSE, envir = parent.frame())

Arguments

what either a function or a non-empty character string naming the function to be called.
args a list of arguments to the function call. The names attribute of args gives the argument names.
quote a logical value indicating whether to quote the arguments.
envir an environment within which to evaluate the call. This will be most useful if what is a character string and the arguments are symbols or quoted expressions.

Details

If quote is FALSE, the default, then the arguments are evaluated (in the calling environment, not in envir). If quote is TRUE then each argument is quoted (see quote) so that the effect of argument evaluation is to remove the quotes – leaving the original arguments unevaluated when the call is constructed.

The behavior of some functions, such as substitute, will not be the same for functions evaluated using do.call as if they were evaluated from the interpreter. The precise semantics are currently undefined and subject to change.



Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email],
Date:        11/20/2012 12:13 PM
Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
Sent by:        "SPSSX(r) Discussion" <[hidden email]>





"As R code goes, this is actually pretty straightforward :-(  "..
I was afraid that was the case!
So far my experience as well.  Currently creating SPSS code to implement
methods in a couple of articles.  Also have R code which would hopefully
have illuminated some of the trickier parts of the equations (no such luck).
Just for my edification Jon:
What is it about R that makes it the 'most difficult to learn' in your
experience?
---

Jon K Peck wrote
> As R code goes, this is actually pretty straightforward :-(  But I think
> this was just an example.  Some say that R is easy to learn.  My take is
> that R is just about the most difficult to learn of any of the dozens of
> programming languages that I have experienced.
>
>
> Jon Peck (no "h") aka Kim
> Senior Software Engineer, IBM

> peck@.ibm

> new phone: 720-342-5621
>
>
>
>
> From:   David Marso &lt;

> david.marso@

> &gt;
> To:

> SPSSX-L@.uga

> ,
> Date:   11/20/2012 09:31 AM
> Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
> Sent by:        "SPSSX(r) Discussion" &lt;

> SPSSX-L@.uga

> &gt;
>
>
>
> I am truly bRamaged after staring at that mess ;-)
> How about a few in line comments for the benefit of pedagogical purposes.
> I am convinced that writing tons of uncommented R code is a built in step
> to
> job security.
> Poke it with a stick?  Jello code jumping off the table!
> B0 + B1*(Free software + expensive consultants - documentation ) + B2 *
> time
> =$$$$
>
>
>
> Albert-Jan Roskam wrote
>> "as much as anything in R is straightforward": so True!
>>
>> begin program r.
>> options(stringsAsFactors=FALSE)
>> dta <- data.frame(aFactor=factor(c(1, 2, 3)), aFloat=as.double(c(1, 2,
>> 3)), aOrdered=ordered(c(1, 2, 3)), aChar=as.character(c("1", "2", "3")))
>>
>> width <- max(sapply(dta[sapply(dta, is.character)], nchar), na.rm=TRUE)
> #
>> max is not really ideal
>> strVar <- sprintf("A%d", width)
>> printTypes <- c(factor="F8.0", numeric="F12.2", ordered="F8.0",
>> character=strVar)
>> varTypes <- c(factor=0, numeric=0, ordered=0, character=width)
>> measLevels <- c(factor="nominal", numeric="scale", ordered="ordinal",
>> character="nominal")
>> dataTypes <- lapply(sapply(dta, class), function(x) x[[1]])
>> varNames <- names(dataTypes)
>> names(varNames) <- names(varTypes)
>> spssDictInfo <- lapply(dataTypes, function(arg)  c(varNames[arg], "''",
>> varTypes[arg][[1]], printTypes[arg][[1]], measLevels[arg][[1]]))
>> dict = spssdictionary.CreateSPSSDictionary( spssDictInfo )
>> spssdictionary.SetDictionaryToSPSS("results", dict)
>> end program.
>>
>>
>> Regards,
>> Albert-Jan
>>
>>
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> All right, but apart from the sanitation, the medicine, education, wine,
>> public order, irrigation, roads, a
>> fresh water system, and public health, what have the Romans ever done
> for
>> us?
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>>
>>>________________________________
>>>From: Jon K Peck &lt;
>
>> peck@.ibm
>
>> &gt;
>>>To:
>
>> SPSSX-L@.UGA
>
>>
>>>Sent: Tuesday, November 20, 2012 3:34 PM
>>>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>>>
>>>
>>>It would be straightforward (as much as anything in R is
> straightforward)
> to write a function to simplify the dictionary specification for a new
> dataset taking a data frame as you describe.  I wouldn't call it savFile,
> though, since it could only generate a dataset, not an actual sav file. It
> would be worth, also, mapping ordered factors to ordinal level.
>>>
>>>
>>>Jon Peck (no "h") aka Kim
>>>Senior Software Engineer, IBM
>>>
>
>> peck@.ibm
>
>>>new phone: 720-342-5621
>>>
>>>
>>>
>>>
>>>From:        Albert-Jan Roskam &lt;
>
>> fomcl@
>
>> &gt;
>>>To:        Jon K Peck/Chicago/IBM@IBMUS, "
>
>> SPSSX-L@.UGA
>
>> " &lt;
>
>> SPSSX-L@.UGA
>
>> &gt;,
>>>Date:        11/20/2012 06:29 AM
>>>Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
>>>
>>>>________________________________
>>>
>>>
>>>
>>>It's a little cumbersome (though flexible) to write things like
>  "wert=c("coeff2"," ",0, "F10.7", "scale")"
>>>Isn't it possible to coerce a dataframe into spss data where
> double/float
> becomes F10.7/scale, numeric/int becomes F8.0/scale, factor becomes
> F8.0/nominal and character becomes A-format/nominal?
>>>something like:
> as.savFile(as.date.frame(summary(survival.data.weibull)$coefficients[1:4]))
>>>
>>>Regards,
>>>Albert-Jan
>>>
>>>
>>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>All right, but apart from the sanitation, the medicine, education, wine,
> public order, irrigation, roads, a
>>>fresh water system, and public health, what have the Romans ever done
> for
> us?
>>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>From: Jon K Peck &lt;
>
>> peck@.ibm
>
>> &gt;
>>>To:
>
>> SPSSX-L@.UGA
>
>>
>>>Sent: Monday, November 19, 2012 5:29 PM
>>>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>>>
>>>Try this simple program in the Statistics Output node.  It produces a
> proper results.sav file for me.  Perhaps you need a drive letter in the
> SAVE
> command.
>>>
>>>data list list/x y.
>>>begin data
>>>1 4
>>>2 5
>>>3 6
>>>end data
>>>dataset name fred.
>>>begin program r.
>>>dta = spssdata.GetDataFromSPSS()
>>>print(summary(dta))
>>>dict = spssdictionary.CreateSPSSDictionary(c('x','',0,"F8.0", "scale"),
>>>c('y', '',0,"F8.0", "scale"))
>>>spssdictionary.SetDictionaryToSPSS("results", dict)
>>>print(dta)
>>>spssdata.SetDataToSPSS("results", dta)
>>>spssdictionary.EndDataStep()
>>>end program.
>>>dataset activate results.
>>>save outfile="c:/temp/results.sav".
>>>
>>>
>>>Jon Peck (no "h") aka Kim
>>>Senior Software Engineer, IBM
>>>
>
>> peck@.ibm
>
>>>new phone: 720-342-5621
>>>
>>>
>>>
>>>
>>>From:        Lumar &lt;
>
>> Stefili@
>
>> &gt;
>>>To:
>
>> SPSSX-L@.uga
>
>> ,
>>>Date:        11/19/2012 08:46 AM
>>>Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
>>>Sent by:        "SPSSX(r) Discussion" &lt;
>
>> SPSSX-L@.uga
>
>> &gt;
>>>
>>>
>>>
>>>First, Thank you for your Help! :-)
>>>
>>>I added some code for activating and saving the dataset. Until the
>>>Activation it works without problem. But after the Save-Code I got an
> error.
>>>
>>>*My Code:*
>>>
>>>BEGIN PROGRAM R.
>>>spss=read.table(file="C://Users//Backup//Desktop//test.txt",
> header=TRUE,
>>>sep=",")
>>>
>>>
>>>attach(spss)
>>>library(survival)
>>>surv=Surv(IPT_HIS_REAL)
>>>
>>>survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
>>>dist="weibull",scale=2.0)
>>>summary(survival.data.weibull)
>>>
>>>coeff=summary(survival.data.weibull)$coefficients[1:4]
>>>coeff
>>>coeff2=as.data.frame(coeff)
>>>coeff2
>>>
>>>wert=c("coeff2"," ",0, "F10.7", "scale")
>>>dict=spssdictionary.CreateSPSSDictionary(wert)
>>>spssdictionary.SetDictionaryToSPSS("results",dict)
>>>spssdata.SetDataToSPSS("results", coeff2)
>>>spssdictionary.EndDataStep()
>>>
>>>END PROGRAM.
>>>DATASET ACTIVATE results
>>>SAVE OUTFILE="/data/test567.sav".
>>>
>>>There must be some error but I couldn´t find it. The Error-Text means
>>>following to me:
>>>After the equal sign should stand a name but the programm could only
> find
>>>/data/test567.sav.
>>>
>>>
>>>Please could you help me to solve that problem?!
>>>
>>>Best Regards,
>>>Stefan
>>>
>>>
>>>
>>>--
>>>View this message in context:
>
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716315.html
>
>>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>>
>>>=====================
>>>To manage your subscription to SPSSX-L, send a message to
>>>
>
>> LISTSERV@.UGA
>
>>  (not to SPSSX-L), with no body text except the
>>>command. To leave the list, send the command
>>>SIGNOFF SPSSX-L
>>>For a list of commands to manage subscriptions, send the command
>>>INFO REFCARD
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>
>
>
>
>
> -----
> Please reply to the list and not to my personal email.
> Those desiring my consulting or training services please feel free to
> email me.
> --
> View this message in context:
>
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716350.html
>
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716359.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Thomas MacFarland

Everyone:

 

I’ve been looking at this thread with interest but I did not intend to reply until the value of R was questioned.

 

I’ve used SPSS for more than 20 years and I plan to continue using it in our office since I am responsible for an extensive set of analyses that are based on our long history with SPSS and I would not want to even think of what we would need to do to use another program.

 

However, for new projects where we have no history I increasingly use R and as you can imagine, I do not agree with Dr. Peck’s comments (below) about the difficulty of R.  The SPSS developers would do well to look at the many graphics options available through R that simply do not exist in SPSS. 

 

If you wish to look at R-based syntax that is easy to follow and nicely documented then review the text at http://www.springer.com/statistics/social+sciences+%26+law/book/978-1-4614-2133-7, specifically:

No – I’m not touting my book.  I just know this resource better than others.

 

Best wishes and Happy Thanksgiving.

 

Tom

 

----------

Thomas W. MacFarland, Ed.D.

Senior Research Associate; Institutional Effectiveness and Associate Professor

Nova Southeastern University

Voice 954-262-5395 [hidden email]

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Jon K Peck
Sent: Tuesday, November 20, 2012 3:28 PM
To: [hidden email]
Subject: Re: Data from R to SPSS Modeler

 

R has a huge number of packages (a problem in itself, sometimes), and powerful tools for implementing statistical and graphical algorithms.  Those libraries are probably the biggest benefit for SPSS users.  But as a language, it's pretty incoherent - somewhat object oriented, somewhat not, notationally inconsistent, (those dots in names do not indicate attributes of objects).  Syntax is C like but many things are different.  Scoping of data and functions is way too complex and does not do a good job of information hiding or abstraction.  

There are thousands of functions, so there are usually twelve different ways to do something where one general method would do - okay, maybe two.  For example, it's hard to remember which of the workhorse apply functions to use: apply, lapply, tapply, sapply, mapply, or eapply.  They all do similar but not identical things.  Many functions have quite complex argument sets, albeit often with defaults, that make them hard to use.  Because many ordinary operations are vectorized, simple things like pasting strings together can be hard to figure out.  

And package authors - anyone can contribute - are often inconsistent from one to another.  To top it off, R documentation is written "by experts for experts".  Debugging is a trial.  I spent several hours this morning figuring out how to do something that should be simple and could be done trivially in Python, but I waded through at least 30 R functions and a bunch of Googling figuring out most of what I needed.

I've copied an extract from the doc for the do.call function below as a somewhat more difficult than average example of the documentation.

Getting that off my chest!  If R were being designed today by skilled language designers, many things would be different, I believe, both in the design and in the internals, but for better or worse, those libraries and the power of the language mean that for statistical development, it's still a great resource.  And if IBM or someone else wraps R functionality in a Statistics extension command, you get the power without the pain.

-------------------------
do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it.

Usage
do.call(what, args, quote = FALSE, envir = parent.frame())

Arguments

what

either a function or a non-empty character string naming the function to be called.

args

a list of arguments to the function call. The names attribute of args gives the argument names.

quote

a logical value indicating whether to quote the arguments.

envir

an environment within which to evaluate the call. This will be most useful if what is a character string and the arguments are symbols or quoted expressions.

 

Details

If quote is FALSE, the default, then the arguments are evaluated (in the calling environment, not in envir). If quote is TRUE then each argument is quoted (see quote) so that the effect of argument evaluation is to remove the quotes – leaving the original arguments unevaluated when the call is constructed.

The behavior of some functions, such as substitute, will not be the same for functions evaluated using do.call as if they were evaluated from the interpreter. The precise semantics are currently undefined and subject to change.



Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email],
Date:        11/20/2012 12:13 PM
Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
Sent by:        "SPSSX(r) Discussion" <[hidden email]>





"As R code goes, this is actually pretty straightforward :-(  "..
I was afraid that was the case!
So far my experience as well.  Currently creating SPSS code to implement
methods in a couple of articles.  Also have R code which would hopefully
have illuminated some of the trickier parts of the equations (no such luck).
Just for my edification Jon:
What is it about R that makes it the 'most difficult to learn' in your
experience?
---

Jon K Peck wrote
> As R code goes, this is actually pretty straightforward :-(  But I think
> this was just an example.  Some say that R is easy to learn.  My take is
> that R is just about the most difficult to learn of any of the dozens of
> programming languages that I have experienced.
>
>
> Jon Peck (no "h") aka Kim
> Senior Software Engineer, IBM

> [hidden email]

> new phone: 720-342-5621
>
>
>
>
> From:   David Marso &lt;

> [hidden email]

> &gt;
> To:

> [hidden email]

> ,
> Date:   11/20/2012 09:31 AM
> Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
> Sent by:        "SPSSX(r) Discussion" &lt;

> [hidden email]

> &gt;
>
>
>
> I am truly bRamaged after staring at that mess ;-)
> How about a few in line comments for the benefit of pedagogical purposes.
> I am convinced that writing tons of uncommented R code is a built in step
> to
> job security.
> Poke it with a stick?  Jello code jumping off the table!
> B0 + B1*(Free software + expensive consultants - documentation ) + B2 *
> time
> =$$$$
>
>
>
> Albert-Jan Roskam wrote
>> "as much as anything in R is straightforward": so True!
>>
>> begin program r.
>> options(stringsAsFactors=FALSE)
>> dta <- data.frame(aFactor=factor(c(1, 2, 3)), aFloat=as.double(c(1, 2,
>> 3)), aOrdered=ordered(c(1, 2, 3)), aChar=as.character(c("1", "2", "3")))
>>
>> width <- max(sapply(dta[sapply(dta, is.character)], nchar), na.rm=TRUE)
> #
>> max is not really ideal
>> strVar <- sprintf("A%d", width)
>> printTypes <- c(factor="F8.0", numeric="F12.2", ordered="F8.0",
>> character=strVar)
>> varTypes <- c(factor=0, numeric=0, ordered=0, character=width)
>> measLevels <- c(factor="nominal", numeric="scale", ordered="ordinal",
>> character="nominal")
>> dataTypes <- lapply(sapply(dta, class), function(x) x[[1]])
>> varNames <- names(dataTypes)
>> names(varNames) <- names(varTypes)
>> spssDictInfo <- lapply(dataTypes, function(arg)  c(varNames[arg], "''",
>> varTypes[arg][[1]], printTypes[arg][[1]], measLevels[arg][[1]]))
>> dict = spssdictionary.CreateSPSSDictionary( spssDictInfo )
>> spssdictionary.SetDictionaryToSPSS("results", dict)
>> end program.
>>
>>
>> Regards,
>> Albert-Jan
>>
>>
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> All right, but apart from the sanitation, the medicine, education, wine,
>> public order, irrigation, roads, a
>> fresh water system, and public health, what have the Romans ever done
> for
>> us?
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>>
>>>________________________________
>>>From: Jon K Peck &lt;
>
>> [hidden email]
>
>> &gt;
>>>To:
>
>> [hidden email]
>
>>
>>>Sent: Tuesday, November 20, 2012 3:34 PM
>>>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>>>
>>>
>>>It would be straightforward (as much as anything in R is
> straightforward)
> to write a function to simplify the dictionary specification for a new
> dataset taking a data frame as you describe.  I wouldn't call it savFile,
> though, since it could only generate a dataset, not an actual sav file. It
> would be worth, also, mapping ordered factors to ordinal level.
>>>
>>>
>>>Jon Peck (no "h") aka Kim
>>>Senior Software Engineer, IBM
>>>
>
>> [hidden email]
>
>>>new phone: 720-342-5621
>>>
>>>
>>>
>>>
>>>From:        Albert-Jan Roskam &lt;
>
>> fomcl@
>
>> &gt;
>>>To:        Jon K Peck/Chicago/IBM@IBMUS, "
>
>> [hidden email]
>
>> " &lt;
>
>> [hidden email]
>
>> &gt;,
>>>Date:        11/20/2012 06:29 AM
>>>Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
>>>
>>>>________________________________
>>>
>>>
>>>
>>>It's a little cumbersome (though flexible) to write things like
>  "wert=c("coeff2"," ",0, "F10.7", "scale")"
>>>Isn't it possible to coerce a dataframe into spss data where
> double/float
> becomes F10.7/scale, numeric/int becomes F8.0/scale, factor becomes
> F8.0/nominal and character becomes A-format/nominal?
>>>something like:
> as.savFile(as.date.frame(summary(survival.data.weibull)$coefficients[1:4]))
>>>
>>>Regards,
>>>Albert-Jan
>>>
>>>
>>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>All right, but apart from the sanitation, the medicine, education, wine,
> public order, irrigation, roads, a
>>>fresh water system, and public health, what have the Romans ever done
> for
> us?
>>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>From: Jon K Peck &lt;
>
>> [hidden email]
>
>> &gt;
>>>To:
>
>> [hidden email]
>
>>
>>>Sent: Monday, November 19, 2012 5:29 PM
>>>Subject: Re: [SPSSX-L] Data from R to SPSS Modeler
>>>
>>>Try this simple program in the Statistics Output node.  It produces a
> proper results.sav file for me.  Perhaps you need a drive letter in the
> SAVE
> command.
>>>
>>>data list list/x y.
>>>begin data
>>>1 4
>>>2 5
>>>3 6
>>>end data
>>>dataset name fred.
>>>begin program r.
>>>dta = spssdata.GetDataFromSPSS()
>>>print(summary(dta))
>>>dict = spssdictionary.CreateSPSSDictionary(c('x','',0,"F8.0", "scale"),
>>>c('y', '',0,"F8.0", "scale"))
>>>spssdictionary.SetDictionaryToSPSS("results", dict)
>>>print(dta)
>>>spssdata.SetDataToSPSS("results", dta)
>>>spssdictionary.EndDataStep()
>>>end program.
>>>dataset activate results.
>>>save outfile="c:/temp/results.sav".
>>>
>>>
>>>Jon Peck (no "h") aka Kim
>>>Senior Software Engineer, IBM
>>>
>
>> [hidden email]
>
>>>new phone: 720-342-5621
>>>
>>>
>>>
>>>
>>>From:        Lumar &lt;
>
>> Stefili@
>
>> &gt;
>>>To:
>
>> [hidden email]
>
>> ,
>>>Date:        11/19/2012 08:46 AM
>>>Subject:        Re: [SPSSX-L] Data from R to SPSS Modeler
>>>Sent by:        "SPSSX(r) Discussion" &lt;
>
>> [hidden email]
>
>> &gt;
>>>
>>>
>>>
>>>First, Thank you for your Help! :-)
>>>
>>>I added some code for activating and saving the dataset. Until the
>>>Activation it works without problem. But after the Save-Code I got an
> error.
>>>
>>>*My Code:*
>>>
>>>BEGIN PROGRAM R.
>>>spss=read.table(file="C://Users//Backup//Desktop//test.txt",
> header=TRUE,
>>>sep=",")
>>>
>>>
>>>attach(spss)
>>>library(survival)
>>>surv=Surv(IPT_HIS_REAL)
>>>
>>>survival.data.weibull=survreg(surv~LL_PER_DAY+KM_COUNT+AGE_DAYS+IPT_SDEV,data=spss,
>>>dist="weibull",scale=2.0)
>>>summary(survival.data.weibull)
>>>
>>>coeff=summary(survival.data.weibull)$coefficients[1:4]
>>>coeff
>>>coeff2=as.data.frame(coeff)
>>>coeff2
>>>
>>>wert=c("coeff2"," ",0, "F10.7", "scale")
>>>dict=spssdictionary.CreateSPSSDictionary(wert)
>>>spssdictionary.SetDictionaryToSPSS("results",dict)
>>>spssdata.SetDataToSPSS("results", coeff2)
>>>spssdictionary.EndDataStep()
>>>
>>>END PROGRAM.
>>>DATASET ACTIVATE results
>>>SAVE OUTFILE="/data/test567.sav".
>>>
>>>There must be some error but I couldn´t find it. The Error-Text means
>>>following to me:
>>>After the equal sign should stand a name but the programm could only
> find
>>>/data/test567.sav.
>>>
>>>
>>>Please could you help me to solve that problem?!
>>>
>>>Best Regards,
>>>Stefan
>>>
>>>
>>>
>>>--
>>>View this message in context:
>
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716315.html
>
>>>Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>>
>>>=====================
>>>To manage your subscription to SPSSX-L, send a message to
>>>
>
>> [hidden email]
>
>>  (not to SPSSX-L), with no body text except the
>>>command. To leave the list, send the command
>>>SIGNOFF SPSSX-L
>>>For a list of commands to manage subscriptions, send the command
>>>INFO REFCARD
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>
>
>
>
>
> -----
> Please reply to the list and not to my personal email.
> Those desiring my consulting or training services please feel free to
> email me.
> --
> View this message in context:
>
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716350.html
>
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> [hidden email]

>  (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Data-from-R-to-SPSS-Modeler-tp5716308p5716359.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

Reply | Threaded
Open this post in threaded view
|

Re: Data from R to SPSS Modeler

Lumar
In reply to this post by Jon K Peck
sorry for interrupting this discussion but I want to thank you all for your help.
My Code seems to be correct now!
Only the "." after results was missing.

Thank you very much!