Fix a seed in R using NLMR package - r

I'm using the NLMR package with
# simulate polygonal landscapes
mosaicgibbs <- NLMR::nlm_mosaicgibbs(ncol = 100,
nrow = 100,
germs = 20,
R = 2,
patch_classes = 3)
# visualize the NLM
rasterVis::levelplot(mosaicgibbs, margin = FALSE, par.settings = rasterVis::viridisTheme())
But I would like to fix the seed number in order to reproduce the spatial pattern .
I have tried with set.seed(123) but it doesn't work.
E.

I'm not sure whether this helps in your case, but I couldn't just make a comment. In nlm_gaussianfield() the syntax for setting the seed is:
nlm_gaussianfield(nrow=10, ncol= 10, user_seed = 123)
The default is NULL. Maybe this works in nlm_mosaicgibbs() as well. Good luck!

Related

barplot graph is not working (beside = T)

I want to draw a graph of the mean by a value.
It's a statistic graph, and for this I made a list of each grade.
It used the "list" function to bring in each score and the average of each value using the "lapply" function.
But I tried to use it as a 'barlpot','beside =T' but the graph was not made properly.
I don't know if the graph format is wrong or what kind of mistake I made.
Code
list01 <-list(Sci=df_01$Sci,Eng = df_01$Eng,Math = df_01$Math)
C01_gd = lapply(list01,mean)
as.matrix(C01_gd)
barplot(as.matrix(C01_gd) ,border="white",beside = T)
Here's a tentative solution (tentative, as you have not provided a sample of your data).
Some reproducible data for illustration:
set.seed(12)
df <- data.frame(
Sci = sample(1:6, 100, replace = T),
Eng = sample(1:6, 100, replace = T),
Math = sample(1:6, 100, replace = T)
)
The calculation of the means could not be simpler using apply:
means <- apply(df, 2, mean)
And drawing the barplot is not rocket science either:
barplot(means)

Most efficient way of drawing a heatmap from matrix with OpenGL?

Assume a matrix m of integer values:
m <- matrix(sample(1:10, 100, replace = TRUE), nrow = 10)
Given a colour palette that maps those values from 1 to 10 to some colours, how to show matrix m as a heatmap in R with OpenGL graphics, e.g. using the rgl package? (Preferably in the most efficient way.)
The very thorough answer here suggests this may not be what you want; you might want to try the solution below against the other solutions benchmarked there. Nonetheless:
Set up data and colour map
set.seed(101)
library(viridisLite)
vv <- viridis(10)
m <- matrix(sample(1:10, 100, replace = TRUE), nrow = 10)
Draw the picture:
library(rgl)
view3d(theta=0, phi=0) ## head-on view
par3d(zoom=0.7) ## (almost) fill window
surface3d(x = 1:10, y = 1:10, z = matrix(0, 10,10),
color = vv[m],
smooth=FALSE, lit=FALSE ## turn off smoothing/lights
)
You may need to use pop3d() between surfaces to clear the previous surface ...

Random Graph Function in R

I have an assignment in which I have to generate my own random graph function in R, with an igraph output. I've figured out that the easiest way to do this is to simply generate a square matrix and then build a function which creates edges between the nodes in the matrix. However I'd like to do something special, where the probability of the edges are based on forming a higher likelihood of sybil networks. Would look like this:
My matrix is generated and visualised quite simply like this:
library(ggraph)
library(igraph)
NCols <- 20
NRows <- 20
myMat <-matrix(runif(NCols*NRows), ncol = NCols)
myMat
randomgraph <- graph_from_adjacency_matrix(myMatG, mode = "undirected", weighted = NULL, diag = TRUE, add.colnames = NULL, add.rownames = NA)
randomgraph %>%
ggraph() +
geom_node_point(colour = "firebrick4", size = 0.5, show.legend = F)
I know there are functions like Erdos-Renyi Random- (for a true random graph), Barabási-Albert Scale-Free- and Watts-Strogatz Small-World graphs. I'm trying to write my own with a unique twist.
Any advice or code snippets on how to write my own preferential attachment function for the random matrix would be greatly appreciated! Thank you!

Pretty dendrograms in R?

My dendrograms are horribly ugly, on the verge of unreadable, and usually look like this:
library(TraMineR)
library(cluster)
data(biofam)
lab <- c("P","L","M","LM","C","LC","LMC","D")
biofam.seq <- seqdef(biofam[1:500,10:25], states=lab)
ccost <- seqsubm(biofam.seq, method = "CONSTANT", cval = 2, with.missing=TRUE)
sequences.OM <- seqdist(biofam.seq, method = "OM", norm= TRUE, sm = ccost,
with.missing=TRUE)
clusterward <- agnes(sequences.OM, diss = TRUE, method = "ward")
plot(clusterward, which.plots = 2)
What I would like to create is something like the following, meaning a round dendrogram, where the size of the labels can be carefully controlled so that they are actually visible:
How can I accomplish this in R?
The following solution may not be optimal but worth a try:
library(ape)
CL1 <- as.hclust(clusterward)
CL2 <- as.phylo(CL1)
plot(CL2, type="fan", cex=0.5)
The main issue obviously being the fact that there is still too many objects, hence too many labels. To turn the labels off, use argument show.tip.label=FALSE. You can also get rid of the margins to occupy the complete device with no.margin=TRUE:
plot(CL2, type="fan", show.tip.label=FALSE, no.margin=TRUE)

Making a 3D surface from time series data in R

I have a large data set which I would like to make a 3D surface from. I would like the x-axis to be the date, the y-axis to be the time (24h) and the z-axis (height) to be a value I have ($). I am a beginner with R, so the simpler the better!
http://www.quantmod.com/examples/chartSeries3d/ has a nice example, but the code is way to complicated for my skill level!
Any help would be much appreciated - anything I have researched so far needs to have the data sorted, which is not suitable I think.
Several options present themselves, persp() and wireframe(), the latter in package lattice.
First some dummy data:
set.seed(3)
dat <- data.frame(Dates = rep(seq(Sys.Date(), Sys.Date() + 9, by = 1),
each = 24),
Times = rep(0:23, times = 10),
Value = rep(c(0:12,11:1), times = 10) + rnorm(240))
persp() needs the data as the x and y grid locations and a matrix z of observations.
new.dates <- with(dat, sort(unique(Dates)))
new.times <- with(dat, sort(unique(Times)))
new.values <- with(dat, matrix(Value, nrow = 10, ncol = 24, byrow = TRUE))
and can be plotted using:
persp(new.dates, new.times, new.values, ticktype = "detailed", r = 10,
theta = 35, scale = FALSE)
The facets can be coloured using the col argument. You could do a lot worse than study the code for chartSeries3d0() at the page you linked to. Most of the code is just drawing proper axes as neither persp() nor wireframe() handle Date objects easily.
As for wireframe(), we
require(lattice)
wireframe(Value ~ as.numeric(Dates) + Times, data = dat, drape = TRUE)
You'll need to do a bit or work to sort out the axis labelling as wireframe() doesn't work with objects of class "Date" at the moment (hence the cast as numeric).

Resources