Re: [R] Creating unique code

2020-10-28 Thread Jim Lemon
Hi Hannah,
Yes, that does give me more insight. The polygyny only doesn't matter, for
even if there was polyandry it could be coded in the same way. If I
understand this correctly you have one variable (SPRULE) that must contain
information about the household of the individual and the identities of his
(or her) relationship partner(s). The household number can accommodate up
to three relationship partners as long as there are a maximum of 9 people
per household. Given these constraints, I think this may do what you want:

ipumsi_8_dta<-
 read.table(
 text="country year sample serial hhwt pernum perwt resident sploc sprule
204 2013 204201301 4000 10 1 10 1 5 2
204 2013 204201301 4000 10 2 10 1 0 2
204 2013 204201301 4000 10 3 10 1 0 2
204 2013 204201301 4000 10 4 10 1 0 2
204 2013 204201301 4000 10 5 10 1 1 2
204 2013 204201301 4000 10 6 10 1 1 2
204 2013 204201301 4000 10 7 10 1 0 2
204 2013 204201301 4000 10 8 10 1 0 2
204 2013 204201301 4000 10 9 10 1 0 2
204 2013 204201301 7000 10 1 10 1 2 1
204 2013 204201301 7000 10 2 10 1 1 1
204 2013 204201301 7000 10 3 10 1 0 0
204 2013 204201301 7000 10 4 10 1 5 1
204 2013 204201301 7000 10 5 10 1 4 1",
header=TRUE,stringsAsFactors=FALSE)
for(hh in unique(ipumsi_8_dta$serial)) {
 cat("hh",hh," ")
 for(ind in ipumsi_8_dta$pernum[ipumsi_8_dta$serial == hh]) {
  cat("ind",ind,"\n")
  if(ipumsi_8_dta$sploc[ipumsi_8_dta$serial == hh &
ipumsi_8_dta$pernum == ind] > 0) {
   cat("sploc > 0\n")
   relationships<-
ipumsi_8_dta$pernum[ipumsi_8_dta$serial == hh &
 ipumsi_8_dta$sploc == ind]
   cat(relationships,"\n")
   if(length(relationships > 1)) {
ipumsi_8_dta$sprule[ipumsi_8_dta$serial == hh &
 ipumsi_8_dta$pernum == ind]<-
  hh+as.numeric(paste0(relationships,collapse=""))
   } else {
ipumsi_8_dta$sprule[ipumsi_8_dta$serial == hh &
 ipumsi_8_dta$pernum == ind]<-hh+
ipumsi_8_dta$sploc[ipumsi_8_dta$serial == hh &
 ipumsi_8_dta$pernum == ind]
   }
  } else {
   ipumsi_8_dta$sprule[ipumsi_8_dta$serial == hh &
ipumsi_8_dta$pernum == ind]<-hh
  }
 }
}
ipumsi_8_dta

Note that this is a bad way to create a field in a database and it would be
better to create the information on the fly with a query.
It won't work with between household relationships

Jim

On Wed, Oct 28, 2020 at 10:08 PM Hannah Van Impe 
wrote:

> Hello
> Again, thank you very much for the help!!
>
...

> In this foto, you can see that there is a polygamous union in household 6.
> The men always only shows 1 sploc variable, even when they have multiple
> women. So, only men have multiple women and women don’t have multiple men.
> So here, The man is observation 49, and he is linked with woman 53, but he
> can also be linked with other women. We see that woman 53 is linked with
> this man, but also woman 54 is linked with this man. So the man in
> observation 49 is linked with two women, 53 and 54, and I would like to
> give these 3 observations the same numerical code, so I can identity this
> union.
> Is it possible to give more explanation when you have this information?
> Hannah
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Exporting SPSS Dataset to STATA format

2020-10-28 Thread John Kane
What have you tried so far?

The method shown here https://www.statmethods.net/input/exportingdata.html
should work.

On Wed, 28 Oct 2020 at 11:06, nelgyn tennyson  wrote:

> *Dear Sir/Madam,*
>
> *   Subject : Exporting SPSS Dataset to STATA format*
>
> I have a SPSS file which i am able to import into R via Rcmdr or Rstudio.
> But i am unable to export the same file into STATA format.
> It would be kind of you to kindly help me achieve the same and send me  a
> sample R script whcih could be used for the same purpose.
>
> *Thanks and Regards*
> *Nelgyn Tennyson *
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
John Kane
Kingston ON Canada

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Reference to cite stats package

2020-10-28 Thread John Kane
citation("packagename")


citation("lubridate")

==
To cite lubridate in publications use:

  Garrett Grolemund, Hadley Wickham (2011). Dates and Times Made
  Easy with lubridate. Journal of Statistical Software, 40(3),
  1-25. URL http://www.jstatsoft.org/v40/i03/.

A BibTeX entry for LaTeX users is

  @Article{,
title = {Dates and Times Made Easy with {lubridate}},
author = {Garrett Grolemund and Hadley Wickham},
journal = {Journal of Statistical Software},
year = {2011},
volume = {40},
number = {3},
pages = {1--25},
url = {http://www.jstatsoft.org/v40/i03/},
  }

==





On Wed, 28 Oct 2020 at 11:07, Iriel Joerin  wrote:

> Hi,
> I´m using the R package stats for some analysis, I would like to know if
> there is any special reference paper to cite this package or just the R
> paper itself.
> Thank you very much for your answer,
> Iriel Joerin
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
John Kane
Kingston ON Canada

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Reference to cite stats package

2020-10-28 Thread Iriel Joerin
Hi,
I´m using the R package stats for some analysis, I would like to know if
there is any special reference paper to cite this package or just the R
paper itself.
Thank you very much for your answer,
Iriel Joerin

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Exporting SPSS Dataset to STATA format

2020-10-28 Thread nelgyn tennyson
*Dear Sir/Madam,*

*   Subject : Exporting SPSS Dataset to STATA format*

I have a SPSS file which i am able to import into R via Rcmdr or Rstudio.
But i am unable to export the same file into STATA format.
It would be kind of you to kindly help me achieve the same and send me  a
sample R script whcih could be used for the same purpose.

*Thanks and Regards*
*Nelgyn Tennyson *

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Dear R experts, I have a question about meta-analysis

2020-10-28 Thread Michael Dewey

Dear Zhang

There is a mailing list dedicated to meta-analysis in R where your 
question may get more attention. Before posting it would be a good idea 
to look at the archives as this issue comes up there repeatedly.


https://stat.ethz.ch/mailman/listinfo/r-sig-meta-analysis//

For a link to the archive and for instructions on registering for the list.

Michael


On 27/10/2020 13:32, 21803...@zju.edu.cn wrote:

Dear R experts,

Greetings from China! I'm Zhang in the College of Education, Zhejiang 
University, and I am recently running a meta-analysis. Since research using the 
randomized controlled trial (RCT) often dismissed reporting the correlation (r) 
between multivariate outcomes, for instance, a study measuring students' gains 
on problem solving skills with three aspects and reported the pre-post scores 
respectively, but obviously these three aspects were correlated. I wonder if 
and how I could integrate the effect sizes among these three aspects into an 
overall effect size without getting the concrete 'r'? Could R project and (or) 
function help me solve this problem?




Best,

Zhang





--

Zhang Enming (张恩铭)

PhD Student

Department of Curriculum and Instruction

College of Education, Zhejiang University

Tel: +86 17649850218

Email: 21803...@zju.edu.cn
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




--
Michael
http://www.dewey.myzen.co.uk/home.html

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R for-loop to add layer to lattice plot

2020-10-28 Thread Luigi Marongiu
Awesome, thanks!

On Wed, Oct 28, 2020 at 7:00 AM Deepayan Sarkar
 wrote:
>
> On Tue, Oct 27, 2020 at 6:04 PM Luigi Marongiu  
> wrote:
> >
> > Hello,
> > I am using e1071 to run support vector machine. I would like to plot
> > the data with lattice and specifically show the hyperplanes created by
> > the system.
> > I can store the hyperplane as a contour in an object, and I can plot
> > one object at a time. Since there will be thousands of elements to
> > plot, I can't manually add them one by one to the plot, so I tried to
> > loop into them, but only the last is added.
> > Here it the working example for more clarity:
> >
> > ```
> > library(e1071)
> > library(lattice)
> > library(latticeExtra)
> >
> > make.grid <- function(x, n = 1000) {
> >   grange = apply(x, 2, range)
> >   x1 = seq(from = grange[1,1], to = grange[2,1], length = n)
> >   x2 = seq(from = grange[1,2], to = grange[2,2], length = n)
> >   expand.grid(X1 = x1, X2 = x2)
> > }
> >
> > plot_list <- list()
> > for (i in 1:10) {
> >   x1 = rnorm(100, mean = 0.2, sd = 0.15)
> >   y1 = rnorm(100, mean = 0.7, sd = 0.15)
> >   y2 = rnorm(100, mean = 0.2, sd = 0.15)
> >   x2 = rnorm(100, mean = 0.75, sd = 0.15)
> >   df = data.frame(x = c(x1,x2), y=c(y1,y2),
> >   z=c(rep(0, length(x1)), rep(1, length(x2
> >   df$z = factor(c(rep(0, length(x1)), rep(1, length(x2
> >   df[, "train"] <- ifelse(runif(nrow(df)) < 0.8, 1, 0)
> >   trainset <- df[df$train == 1, ]
> >   testset <- df[df$train == 0, ]
> >   trainColNum <- grep("train", names(df))
> >   trainset <- trainset[, -trainColNum]
> >   testset <- testset[, -trainColNum]
> >   svm_model <- svm(z ~ .,
> >   data = trainset,
> >   type = "C-classification",
> >   kernel = "linear",
> >   scale = FALSE)
> >   # generate contour
> >   xmat = make.grid(matrix(c(testset$x, testset$y),
> >   ncol = 2, byrow=FALSE))
> >   xgrid = as.data.frame(xmat)
> >   names(xgrid) = c("x", "y")
> >   z = predict(svm_model, xgrid)
> >   xyz_dat = as.data.frame(cbind(xgrid, z))
> >   plot_list[[i]] = contourplot(z ~ y+x, data=xyz_dat, pretty = TRUE,
> >xlim=c(-1,50), ylim=c(-0.001, 0.05),
> >labels = FALSE, col = "blue", lwd = 0.5)
> >
> > }
> > # the contour is stored in the object plot_list
> > str(plot_list) # confirm that there is data here
> >
> > # I can add one element at the time to lattice's xyplot and store it
> > in an object P
> > P = xyplot(y ~ x, group = z, data = df,
> >pch = 16, cex = 1.5, alpha = 0.25) + as.layer(plot_list[[1]]) +
> >   as.layer(plot_list[[2]])
> > plot(P)  # this demonstrates that the lines are not the same
> >
> > # but if I add the elements via loop, it does not work
> > for (i in 1:length(plot_list)) {
> >   print(i)
> >   P = xyplot(y ~ x, group = z, data = df,
> >  pch = 16, cex = 1.5, alpha = 0.25) + as.layer(plot_list[[i]])
> > }
> > plot(P)
> > ```
> >
> > Am I missing something?
>
> Yes, as Mark says, you need to change the last part to something like
>
> P = xyplot(y ~ x, group = z, data = df, pch = 16, cex = 1.5, alpha = 0.25)
> for (i in 1:length(plot_list)) {
>   print(i)
>   P = P + as.layer(plot_list[[i]])
> }
> plot(P)
>
> -Deepayan
>
> > Thank you
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.



-- 
Best regards,
Luigi

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R for-loop to add layer to lattice plot

2020-10-28 Thread Deepayan Sarkar
On Tue, Oct 27, 2020 at 6:04 PM Luigi Marongiu  wrote:
>
> Hello,
> I am using e1071 to run support vector machine. I would like to plot
> the data with lattice and specifically show the hyperplanes created by
> the system.
> I can store the hyperplane as a contour in an object, and I can plot
> one object at a time. Since there will be thousands of elements to
> plot, I can't manually add them one by one to the plot, so I tried to
> loop into them, but only the last is added.
> Here it the working example for more clarity:
>
> ```
> library(e1071)
> library(lattice)
> library(latticeExtra)
>
> make.grid <- function(x, n = 1000) {
>   grange = apply(x, 2, range)
>   x1 = seq(from = grange[1,1], to = grange[2,1], length = n)
>   x2 = seq(from = grange[1,2], to = grange[2,2], length = n)
>   expand.grid(X1 = x1, X2 = x2)
> }
>
> plot_list <- list()
> for (i in 1:10) {
>   x1 = rnorm(100, mean = 0.2, sd = 0.15)
>   y1 = rnorm(100, mean = 0.7, sd = 0.15)
>   y2 = rnorm(100, mean = 0.2, sd = 0.15)
>   x2 = rnorm(100, mean = 0.75, sd = 0.15)
>   df = data.frame(x = c(x1,x2), y=c(y1,y2),
>   z=c(rep(0, length(x1)), rep(1, length(x2
>   df$z = factor(c(rep(0, length(x1)), rep(1, length(x2
>   df[, "train"] <- ifelse(runif(nrow(df)) < 0.8, 1, 0)
>   trainset <- df[df$train == 1, ]
>   testset <- df[df$train == 0, ]
>   trainColNum <- grep("train", names(df))
>   trainset <- trainset[, -trainColNum]
>   testset <- testset[, -trainColNum]
>   svm_model <- svm(z ~ .,
>   data = trainset,
>   type = "C-classification",
>   kernel = "linear",
>   scale = FALSE)
>   # generate contour
>   xmat = make.grid(matrix(c(testset$x, testset$y),
>   ncol = 2, byrow=FALSE))
>   xgrid = as.data.frame(xmat)
>   names(xgrid) = c("x", "y")
>   z = predict(svm_model, xgrid)
>   xyz_dat = as.data.frame(cbind(xgrid, z))
>   plot_list[[i]] = contourplot(z ~ y+x, data=xyz_dat, pretty = TRUE,
>xlim=c(-1,50), ylim=c(-0.001, 0.05),
>labels = FALSE, col = "blue", lwd = 0.5)
>
> }
> # the contour is stored in the object plot_list
> str(plot_list) # confirm that there is data here
>
> # I can add one element at the time to lattice's xyplot and store it
> in an object P
> P = xyplot(y ~ x, group = z, data = df,
>pch = 16, cex = 1.5, alpha = 0.25) + as.layer(plot_list[[1]]) +
>   as.layer(plot_list[[2]])
> plot(P)  # this demonstrates that the lines are not the same
>
> # but if I add the elements via loop, it does not work
> for (i in 1:length(plot_list)) {
>   print(i)
>   P = xyplot(y ~ x, group = z, data = df,
>  pch = 16, cex = 1.5, alpha = 0.25) + as.layer(plot_list[[i]])
> }
> plot(P)
> ```
>
> Am I missing something?

Yes, as Mark says, you need to change the last part to something like

P = xyplot(y ~ x, group = z, data = df, pch = 16, cex = 1.5, alpha = 0.25)
for (i in 1:length(plot_list)) {
  print(i)
  P = P + as.layer(plot_list[[i]])
}
plot(P)

-Deepayan

> Thank you
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.