Re: [R] 3d plot of earth with cut

2020-10-27 Thread Duncan Murdoch

One addition to this thread:

I've just committed contourLines3d() to rgl (in version 0.102.28).  It 
will let you display contour lines of a function on any surface in a scene.


Duncan Murdoch

On 23/10/2020 2:30 p.m., Duncan Murdoch wrote:

Good to hear you've made such progress.  Just a couple of comments:

- You should use points3d() rather than rgl.points().  The latter is a
low level function that may have unpleasant side effects, especially
mixing it with other *3d() functions like persp3d().
- There are several ways to draw a flat surface to illustrate your data.
   Which one to use really depends on the form of data.  With randomly
distributed points, yours is as good as any.  If the values are a known
function of location, there are probably better ones.

Duncan Murdoch


On 23/10/2020 12:18 p.m., Balint Radics wrote:

Dear All,

Thanks a lot for the useful help again. I manage to get it done up to a
point where I think I
just need to apply some smoothing/interpolation to get denser points, to
make it nice.
Basically, I started from Duncen's script to visualize and make the
clipping along a plane
at a slice.
Then I map my data points' values to a color palette and just plot them as
points on this
plane. Since I have already the (x,y,z) coordinates for my points in the
slice's plane
I just plot them directly. I copied the code below..

To make it nicer would be able to make a real "smooth" map on the 2D
surface, rather
than plotting all points (e.g. using polygons?).

Best,
Balint


# Construct a plane at a given longitude
r <- 6378.1 # radius of Earth in km
fixlong <- 10.0*pi/180.0 # The longitude slice

# Construct a plane in 3D:
# Let vec(P0) = (P0x, P0y, P0z) be a point given in the plane of the
longitude
# let vec(n) = (nx, ny, nz) an orthogonal vector to this plane
# then vec(P) = (Px, Py, Pz) will be in the plane if (vec(P) - vec(P0)) *
vec(n) = 0
# We pick 2 arbitrary vectors in the plane out of 3 points
p0x <- r*cos(2)*cos(fixlong)
p0y <- r*cos(2)*sin(fixlong)
p0z <- r*sin(2)
p1x <- r*cos(2.5)*cos(fixlong)
p1y <- r*cos(2.5)*sin(fixlong)
p1z <- r*sin(2.5)
p2x <- r*cos(3)*cos(fixlong)
p2y <- r*cos(3)*sin(fixlong)
p2z <- r*sin(3)
# Make the vectors pointing to P and P0
v1x <- p1x - p0x # P
v1y <- p1y - p0y
v1z <- p1z - p0z
v2x <- p2x - p0x # P0
v2y <- p2y - p0y
v2z <- p2z - p0z

# The cross product will give a vector orthogonal to the plane, (nx, ny, nz)
nx <- v1y*v2z - v1z*v2y;
ny <- v1z*v2x - v1x*v2z;
nz <- v1x*v2y - v1y*v2x;
# normalize
nMag <- sqrt(nx*nx + ny*ny + nz*nz);
nx <- nx / nMag;
ny <- ny / nMag;
nz <- nz / nMag;

# Plane equation (vec(P) - vec(P0)) * vec(n) = 0, with P=(x, y, z), P0=(x0,
y0, z0),
# giving a*(x-x0)+b*(y-y0)+c*(z-z0) = 0, where x,x0 are two points in the
plane
# a, b, c are the normal vector coordinates
a <- -nx
b <- -ny
c <- -nz
d <- -(a*v2x + b*v2y + c*v2z )

open3d()

# Plot the globe - from Duncan
# points of a sphere
lat <- matrix(seq(90, -90, len = 50)*pi/180, 50, 50, byrow = TRUE)
long <- matrix(seq(-180, 180, len = 50)*pi/180, 50, 50)
x <- r*cos(lat)*cos(long)
y <- r*cos(lat)*sin(long)
z <- r*sin(lat)
# Plot with texture
ids <- persp3d(x, y, z, col = "white",
  texture = system.file("textures/world.png", package =
"rgl"),
  specular = "black", axes = FALSE, box = FALSE, xlab = "",
  ylab = "", zlab = "", normal_x = x, normal_y = y, normal_z
= z)

# Plot the plane across the longitude slice
#planes3d(a, b, c, d, alpha = 0.6) # optionally visualize the plane
# Apply clipping to only one side of the plane using the normal vector
clipplanes3d(a, b, c, d)

# Map something onto this plane - how? Let's try with rgl.points and
mapping the colors
# The data is: data_activity and variables are $X, $Y, $Z, $Ar
library(leaflet)
# map the colors to the data values
pal <- colorNumeric(
palette = "Blues",
domain = data_activity$Ar) #
# plot the points and the mapped colors
rgl.points( data_activity$X, data_activity$Y, data_activity$Z, color =
pal(data_activity$Ar), size=3)




On Fri, Oct 23, 2020 at 1:50 AM aBBy Spurdle, ⍺XY 
wrote:


It should be a 2D slice/plane embedded into a 3D space.


I was able to come up with the plot, attached.
My intention was to plot national boundaries on the surface of a sphere.
And put the slice inside.
However, I haven't (as yet) worked out how to get the coordinates for
the boundaries.

Let me know, if of any value.
And I'll post the code.
(But needs to be polished first)



[[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] Creating unique code

2020-10-27 Thread Jim Lemon
Hi Hannah,
Without knowing how the data are organized and what each numeric
code means, it is a bit difficult. If it is assumed that each row in the
data frame(?) ipumsi_8_dta is a case (individual) and an individual may
have zero or more spouses, there would have to be more than one field for
"sploc" for those who had more than one "spouse". I would approach it by
creating a variable named "relcode" that was unique for each "union", so
that if more than one individual had the same non-zero "relcode" they would
all be in the same "relationship". That still leaves us with exclusive
relationships, so there would have to be multiple fields for "relcode" for
groups of people who were in different relationships in the same household.
I know that this is being pedantic, but it looks like a set intersection
problem of the Bob and Carol and Ted and Alice variety.

Jim

On Wed, Oct 28, 2020 at 6:39 AM Hannah Van Impe 
wrote:

> Hello,
>
> I need some help in creating a new variable. I need to create a 'couple
> identifier', which gives a unique code for every couple/triple/... in a
> household. So, I can identify couples. To do this, I should use 4 variables:
>
>   *   SERIAL = a unique numeric code for each household
>   *   PERNUM = a unique numeric code for each person
>   *   SPLOC = the numeric code of the spouse in the household, it is equal
> to the PERNUM code of the spouse
>   *   SPRULE = rules for linking a spouse, numeric code from 00 to 06
>
>
> To create the couple identifier, I need these conditions:
>
>   *   SERIAL needs to be equal for these persons in the couples
>   *   SPLOC > 0
>   *   SPLOC = PERNUM
>   *   SPRULE = 01 or 02
>
> What I already did is this:
>
> attach(ipumsi_8_dta)
> library(tinytex)
> library(dplyr)
> library(ggplot2)
> library(tidyr)
> library(knitr)
> library(forcats)
> library(mice)
> library(pander)
> library(ggcorrplot)
> library(lubridate)
> # true/false code when sploc is greater than zero
> ipumsi_8_dta <- mutate(ipumsi_8_dta, sploc_greater_than_zero =
> sploc>0)
> # true/false code when sploc is greater then zero and sprule is equal to 1
> or 2
> ipumsi_8_dta <- mutate(ipumsi_8_dta, rule_union = sploc>0 &
> sprule==1 | sprule==2)
>
> => Now I want to create a numeric code for true values of rule_union when
> serials are equal, so when they are persons of the same household.
> What method should I use to do this?
>
> Thank you very much!!
>
> [[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.
>

[[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] How to correct my error message

2020-10-27 Thread Md. Moyazzem Hossain
Dear Varin,

I think the following code will solve your problem.

n <- 60
b <- runif(n, 0, 5)
a <- runif(n, 0, 5)
z1 <- data.frame(x0=1:57,
x1=rnorm(n*0.95,2,3))
z2 <- data.frame(x0=58:60,
x1=rnorm(n*0.05,2,9))

combined=rbind(z1,z2)
z=combined[,2]
y_model <- 0.1 * b - 0.5 * z - a + 10
y_obs <- y_model +c( rnorm(n*0.95, 0, 0.1), rnorm(n*0.05, 0, 0.5) )
df<-data.frame(b,a,z,y_obs)

Thanks.

Md

On Tue, Oct 27, 2020 at 7:21 PM Sarah Goslee  wrote:

> Hi,
>
> a is of length 60.
> b is of length 60.
> z is of length 57.
>
> What do you expect to have happen when you create y_model ? What
> happens to those other 3 observations?
>
> Sarah
>
> On Tue, Oct 27, 2020 at 3:07 PM varin sacha via R-help
>  wrote:
> >
> > Dear R-experts,
> >
> > Here below my R code. The warning message is not a problem to me but
> there is an error message more problematic. I understand the error message
> but I don't know if it is possible to correct the error and if yes, how to
> correct it.
> >
> > Many thanks.
> >
> >
> > n <- 60
> > b <- runif(n, 0, 5)
> > a <- runif(n, 0, 5)
> > z <- rnorm(n*0.95,2,3) + rnorm(n*0.05,2,9)
> > y_model <- 0.1 * b - 0.5 * z - a + 10
> > y_obs <- y_model +c( rnorm(n*0.95, 0, 0.1), rnorm(n*0.05, 0, 0.5) )
> > df<-data.frame(b,a,z,y_obs)
> >
> > __
> > 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.
>
>
>
> --
> Sarah Goslee (she/her)
> http://www.numberwright.com
>
> __
> 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,
Md. Moyazzem Hossain
Associate Professor
Department of Statistics
Jahangirnagar University
Savar, Dhaka-1342
Bangladesh
Website: http://www.juniv.edu/teachers/hossainmm
Research: *Google Scholar
*;
*ResearchGate
*; *ORCID iD
*

[[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] How to correct my error message

2020-10-27 Thread Duncan Murdoch

On 27/10/2020 3:06 p.m., varin sacha via R-help wrote:

Dear R-experts,

Here below my R code. The warning message is not a problem to me but there is 
an error message more problematic. I understand the error message but I don't 
know if it is possible to correct the error and if yes, how to correct it.

Many thanks.


n <- 60
b <- runif(n, 0, 5)
a <- runif(n, 0, 5)
z <- rnorm(n*0.95,2,3) + rnorm(n*0.05,2,9)
y_model <- 0.1 * b - 0.5 * z - a + 10
y_obs <- y_model +c( rnorm(n*0.95, 0, 0.1), rnorm(n*0.05, 0, 0.5) )
df<-data.frame(b,a,z,y_obs)



I suspect you intended to concatenate the two parts of z, i.e.

 z <- c(rnorm(n*0.95,2,3), rnorm(n*0.05,2,9))

You shouldn't ignore the warning.

By the way, it's not true for every n that my expression for z will 
always give something of length n.  It would be safer to do the 
calculation as


 m <- round(n*0.95)
 z <- c(rnorm(m,2,3), rnorm(n-m,2,9)

Duncan Murdoch

__
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] Dear R experts, I have a question about meta-analysis

2020-10-27 Thread 21803005
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.


[R] Creating unique code

2020-10-27 Thread Hannah Van Impe
Hello,

I need some help in creating a new variable. I need to create a 'couple 
identifier', which gives a unique code for every couple/triple/... in a 
household. So, I can identify couples. To do this, I should use 4 variables:

  *   SERIAL = a unique numeric code for each household
  *   PERNUM = a unique numeric code for each person
  *   SPLOC = the numeric code of the spouse in the household, it is equal to 
the PERNUM code of the spouse
  *   SPRULE = rules for linking a spouse, numeric code from 00 to 06


To create the couple identifier, I need these conditions:

  *   SERIAL needs to be equal for these persons in the couples
  *   SPLOC > 0
  *   SPLOC = PERNUM
  *   SPRULE = 01 or 02

What I already did is this:

attach(ipumsi_8_dta)
library(tinytex)
library(dplyr)
library(ggplot2)
library(tidyr)
library(knitr)
library(forcats)
library(mice)
library(pander)
library(ggcorrplot)
library(lubridate)
# true/false code when sploc is greater than zero
ipumsi_8_dta <- mutate(ipumsi_8_dta, sploc_greater_than_zero = sploc>0)
# true/false code when sploc is greater then zero and sprule is equal to 1 or 2
ipumsi_8_dta <- mutate(ipumsi_8_dta, rule_union = sploc>0 & sprule==1 | 
sprule==2)

=> Now I want to create a numeric code for true values of rule_union when 
serials are equal, so when they are persons of the same household.
What method should I use to do this?

Thank you very much!!

[[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] How to correct my error message

2020-10-27 Thread Sarah Goslee
Hi,

a is of length 60.
b is of length 60.
z is of length 57.

What do you expect to have happen when you create y_model ? What
happens to those other 3 observations?

Sarah

On Tue, Oct 27, 2020 at 3:07 PM varin sacha via R-help
 wrote:
>
> Dear R-experts,
>
> Here below my R code. The warning message is not a problem to me but there is 
> an error message more problematic. I understand the error message but I don't 
> know if it is possible to correct the error and if yes, how to correct it.
>
> Many thanks.
>
>
> n <- 60
> b <- runif(n, 0, 5)
> a <- runif(n, 0, 5)
> z <- rnorm(n*0.95,2,3) + rnorm(n*0.05,2,9)
> y_model <- 0.1 * b - 0.5 * z - a + 10
> y_obs <- y_model +c( rnorm(n*0.95, 0, 0.1), rnorm(n*0.05, 0, 0.5) )
> df<-data.frame(b,a,z,y_obs)
>
> __
> 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.



-- 
Sarah Goslee (she/her)
http://www.numberwright.com

__
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] How to correct my error message

2020-10-27 Thread varin sacha via R-help
Dear R-experts,

Here below my R code. The warning message is not a problem to me but there is 
an error message more problematic. I understand the error message but I don't 
know if it is possible to correct the error and if yes, how to correct it.

Many thanks.


n <- 60
b <- runif(n, 0, 5)
a <- runif(n, 0, 5)
z <- rnorm(n*0.95,2,3) + rnorm(n*0.05,2,9)
y_model <- 0.1 * b - 0.5 * z - a + 10
y_obs <- y_model +c( rnorm(n*0.95, 0, 0.1), rnorm(n*0.05, 0, 0.5) )
df<-data.frame(b,a,z,y_obs)

__
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-27 Thread Mark Leeds
Hi: I think you're writing over the plots so only the last one exists.
Maybe try P = P + whatever but
I'm not sure if that's allowed with plots.



On Tue, Oct 27, 2020 at 8:34 AM 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?
> 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.
>

[[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] R for-loop to add layer to lattice plot

2020-10-27 Thread Luigi Marongiu
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?
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.


Re: [R] How to shade area between lines in ggplot2

2020-10-27 Thread Luigi Marongiu
Thank you!

On Mon, Oct 26, 2020 at 7:30 AM PIKAL Petr  wrote:
>
> Hi
>
> Put fill outside aes
>
> p+geom_ribbon(aes(ymin = slope_1*x + intercept_1 - 1/w[2],
> ymax = slope_1*x + intercept_1 + 1/w[2]), fill = "blue", alpha=0.1)
>
> The "hole" is because you have two levels of data (red and blue). To get rid
> of this you should put new data in ribbon call.
> Something like
>
> newdat <- trainset
> newdat$z <- factor(0)
> p+geom_ribbon(data=newdat, aes(ymin = slope_1*x + intercept_1 - 1/w[2],
> ymax = slope_1*x + intercept_1 + 1/w[2]), fill = "yellow", alpha=0.1)
>
> Cheers
> Petr
>
> > -Original Message-
> > From: Luigi Marongiu 
> > Sent: Friday, October 23, 2020 3:30 PM
> > To: PIKAL Petr 
> > Cc: r-help 
> > Subject: Re: [R] How to shade area between lines in ggplot2
> >
> > Thank you, but this split the area into two and distorts the shape of the
> > plot.
> > (compared to ``` p + geom_abline(slope = slope_1, intercept = intercept_1 -
> > 1/w[2],
> > linetype = "dashed", col = "royalblue") +
> >   geom_abline(slope = slope_1, intercept = intercept_1 + 1/w[2],
> >   linetype = "dashed", col = "royalblue") ```
> >
> > Why there is a hole in the middle of the ribbon? and the color is not
> > grey...
> >
> > On Fri, Oct 23, 2020 at 2:35 PM PIKAL Petr  wrote:
> > >
> > > Hi
> > >
> > > What about something like
> > >
> > > p+geom_ribbon(aes(ymin = slope_1*x + intercept_1 - 1/w[2],
> > > ymax = slope_1*x + intercept_1 + 1/w[2], fill = "grey70", alpha=0.1))
> > >
> > > Cheers
> > > Petr
> > >
> > > > -Original Message-
> > > > From: Luigi Marongiu 
> > > > Sent: Friday, October 23, 2020 11:11 AM
> > > > To: PIKAL Petr 
> > > > Cc: r-help 
> > > > Subject: Re: [R] How to shade area between lines in ggplot2
> > > >
> > > > also from this site: https://plotly.com/ggplot2/geom_ribbon/
> > > > I get the answer is geom_ribbon but I am still missing something ```
> > > > #! plot p = ggplot(data = trainset, aes(x=x, y=y, color=z)) +
> > > >   geom_point() + scale_color_manual(values = c("red", "blue")) #
> > > > show support vectors df_sv = trainset[svm_model$index, ] p = p +
> > > > geom_point(data = df_sv, aes(x=x, y=y),
> > > >color="purple", size=4, alpha=0.5) # show
> > > > hyperplane (decision boundaries are off set by 1/w[2]) w =
> > > > t(svm_model$coefs) %*% svm_model$SV  # %*% = matrix multiplication
> > > > slope_1 = -w[1]/w[2]
> > > > intercept_1 = svm_model$rho / w[2]
> > > > p = p + geom_abline(slope = slope_1, intercept = intercept_1, col =
> > > > "royalblue4")
> > > > p = p + geom_ribbon(aes(ymin=intercept_1 - 1/w[2],
> > > > ymax=intercept_1 + 1/w[2],
> > > > x=x, fill = "band"), alpha = 0.3) +
> > > >   scale_fill_manual("",values="grey12")
> > > > ```
> > > >
> > > > On Fri, Oct 23, 2020 at 10:26 AM PIKAL Petr 
> > wrote:
> > > > >
> > > > > Hi
> > > > >
> > > > > Did you try google? I got several answers using your question
> > > > >
> > > > > e.g.
> > > > > https://stackoverflow.com/questions/54687321/fill-area-between-lin
> > > > > es-u
> > > > > sing-g
> > > > > gplot-in-r
> > > > >
> > > > > Cheers
> > > > > Petr
> > > > >
> > > > > > -Original Message-
> > > > > > From: R-help  On Behalf Of Luigi
> > > > > > Marongiu
> > > > > > Sent: Friday, October 23, 2020 9:59 AM
> > > > > > To: r-help 
> > > > > > Subject: [R] How to shade area between lines in ggplot2
> > > > > >
> > > > > > Hello,
> > > > > > I am running SVM and showing the results with ggplot2. The
> > > > > > results include the decision boundaries, which are two dashed
> > > > > > lines parallel to a solid
> > > > > line. I
> > > > > > would like to remove the dashed lines and use a shaded area instead.
> > > > > > How can I do that?
> > > > > > Here is the code I wrote..
> > > > > > ```
> > > > > > library(e1071)
> > > > > > library(ggplot2)
> > > > > >
> > > > > > set.seed(100)
> > > > > > x1 = rnorm(100, mean = 0.2, sd = 0.1)
> > > > > > y1 = rnorm(100, mean = 0.7, sd = 0.1)
> > > > > > y2 = rnorm(100, mean = 0.2, sd = 0.1)
> > > > > > x2 = rnorm(100, mean = 0.75, sd = 0.1) 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] head(trainset); str(df)
> > > > > >
> > > > > > svm_model<- svm(z ~ .,
> > > > > > data = trainset,
> > > > > > type = "C-classification",
> > > > > > kernel = "linear",
> > > > > > scale = FALSE)
> > > > > >
> > > > > > #! 

Re: [R] securing R code....

2020-10-27 Thread PIKAL Petr
Hi

Above what you were suggested, your commands could be stored in .RHistory
and your data in .RData files if you end quit your session with save option
"yes". Either interactively or programmatically.

Cheers
Petr

> -Original Message-
> From: R-help  On Behalf Of akshay kulkarni
> Sent: Monday, October 26, 2020 3:18 PM
> To: R help Mailing list 
> Subject: [R] securing R code
> 
> dear members,
>  I am a stock trader. I am using R for my
research.
> 
> I want to service my laptop, wherein resides all my R code, which, for
> obvious reasons, has to be secured. I am using Windows 7 Ultimate.
> 
> I cannot encrypt the R data by Bitdefender, as it encrypts the entire
drive. I
> anyway need to give the key when the system, if at all, gets locked when
> servicing.
> 
> My cousin suggested backing up the data and deleting that data in the
laptop
> when giving it for servicing. How do you back up the R data? What is the
file
> name that contains all the workspace in windows 7? .RHistory only contains
> the previous commands. Can I delete only all the data/code without
deleting
> the R GUI? The extreme option would be to delete the whole of installed R
> GUI, after backing up the workspace. Some other way to secure the
> data/code?
> 
> I've tried using Backup and sync by google, but that is very cumbersome.
> 
> Please help.
> 
> thanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> [https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-
> animated-no-repeat-v1.gif] email?utm_medium=email_source=link_campaign=sig-
> email_content=webmail>  Virus-free.
> www.avast.com email?utm_medium=email_source=link_campaign=sig-
> email_content=webmail>
> 
>   [[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-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.