I have three reference vectors
a ( 0, 0, 1 )
b ( 0, 1, 0 )
c ( 1, 0, 0 )
and will have measurements such as
x( 0, 0.5, 0.3 )
which I want to plot in a 2D figure as a triangle, who edges would correspond to a, b and c.
In Matlab there is a straighforward function to do that
http://fr.mathworks.com/help/matlab/ref/triangulation.cartesiantobarycentric.html?s_tid=gn_loc_drop
does anyone know an equivalent in R or should I implement the maths?
Sure, you can go back and forth between cartesian and barycentric.
Bary to Cart:
library(geometry)
## Define simplex in 2D (i.e. a triangle)
X <- rbind(
c( 0, 0, 1 ),
c( 0, 1, 0 ),
c( 1, 0, 0 ))
## Cartesian cooridinates of points
beta <- rbind(c( 0, 0.5, 0.3 ),
c(0.1, 0.8, 0.1),
c(0.1, 0.8, 0.1))
## Plot triangle and points
trimesh(rbind(1:3), X)
text(X[,1], X[,2], 1:3) # Label vertices
P <- bary2cart(X, beta)
Cart to Bary:
## Define simplex in 2D (i.e. a triangle)
X <- rbind(c(0, 0),
c(0, 1),
c(1, 0))
## Cartesian cooridinates of points
P <- rbind(c(0.5, 0.5),
c(0.1, 0.8))
## Plot triangle and points
trimesh(rbind(1:3), X)
text(X[,1], X[,2], 1:3) # Label vertices
points(P)
cart2bary(X, P)
Related
my problem is summarized in finding a vector X with the best solution to the problem:
L is the profits,
R is the restrictions,
P is a constraint parameters matrix,
max SUM_i (x_i * l_i)
or max(t(L)%*%X)
restriction
SUM_i(x_i*p_ij)<=r_j
or P%*%X <= R.
I find a solution for X, but not the best, which would be
fb = c(.217,0,0,23,2865,0,13,427).
How do I find the best solution?
code:
X<-matrix(rep(1,6),6,1)
P<-matrix(c(
1, 1, 1, 2, 0, 0,
0, 1, 1, 2, 1, 1,
99.4, 37.75, 19.75, 54.40, 74.75, 53,
2.400, 1.540, 0, 0, 0, 0,
2.400, 1.960, 0, 0, 0, 0,
1.800, 3.300, 5.330, 0, 0, 0,
0, 0, 2.070, 0, 8.700, 0,
0, 0, .436, 0, 19.100, 12.363,
0, 3.000, .364, 0, 9.100, 26.737 ),
9,6,1)
L <- matrix(c(83.4, 72.35, 27.3, 72.05, 217.25, 455), 6,1)
R <- matrix(c(60,60,2000,351,448,479,338,424,359),9,1)
farm<- function(par, P,R, L){
trues<- P%*%par<=R
if (min(trues)==1 && min(par)>=0) {
return(-t(L)%*%par)
}
else{
return(0)
}
}
mtds = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN","Brent")
out <- optim(par = X, # initial guess
fn = farm,
P = P,
R = R ,
L = L,
method = mtds[5])
# my result
t(L)%*%out$par
#A matrix: 1 × 1 of type dbl
#7419.596
# the first best
fb<- matrix(c(.217,0,0,23.2865,0,13.427),6,1)
t(L)%*%fb
#A matrix: 1 × 1 of type dbl
#7805.175
I think you can try fmincon from package pracma
library(pracma)
objfun <- function(x) -t(L)%*%x
res <- fmincon(x0 = X,fn = objfun,A = P,b = R,lb = rep(0,length(X)))
and you will see that
> res$par
[1] 4.201711e-16 -1.239088e-15 1.863081e-17 2.310286e+01
[5] 5.566620e-01 1.323762e+01
> -res$value
[,1]
[1,] 7808.615
That looks very much like a model that could be solved by a linear programme.
library("Rglpk")
Rglpk_solve_LP(obj = L,
mat = P,
dir = rep("<=", 9),
rhs = R,
max = TRUE)
is there a way to generate a random sample from a higher order markov chain? I used the package clickstream to estimate a 2nd order markov chain and i'm now trying to generate a sample from it. I understand how to do this from a transition matrix with the randomClickstreams function but that would only work for a 1st order markov chain.
Here's a reproducible example where we generate a sample from a transition matrix and then fit a 2nd order markov chain on the sample:
trans_mat <- matrix(c(0, 0.2, 0.7, 0, 0.1,
0.2, 0, 0.5, 0, 0.3,
0.1, 0.1, 0.1, 0.7, 0,
0, 0.4, 0.2, 0.1, 0.3,
0, 0 , 0 , 0, 1), nrow = 5)
cls <- randomClickstreams(states = c("P1", "P2", "P3", "P4", "end"),
startProbabilities = c(0.5, 0.5, 0, 0, 0),
transitionMatrix = trans_mat,
meanLength = 20, n = 1000)
# fit 2nd order markov chain:
mc <- fitMarkovChain(clickstreamList = cls, order = 2,
control = list(optimizer = "quadratic"))
This is made of 2 transition matrices and 2 lambda parameters:
How can i then use these elements to create a random sample of say 10000 journeys?
I have a "logical" vector of N components like this:
0 0 0 0 0 1 1 1 1 0 1 0 1 0 ...
I want to show a vector/matrix where the elements are colors. The element i is one color if the element i of my logical value is a 0 and another color otherwise. It's for representing a logical vector in a visual way.
You can use your logical vector in a col= argument for plotting, and the logical will be coerced into numeric. So you could do
logi_vec <- sample(c(T,F), 20, replace=T)
# [1] TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE FALSE
x <- rnorm(20)
# simple plotting, the pch=16 produces a solid dot
plot(x, col=logi_vec+1, pch=16) # black vs. red
plot(x, col=logi_vec+2, pch=16) # red vs. green
plot(x, col=2*(logi_vec+1), pch=16) # red vs. blue
etc.
Note that this will work exactly the same way with a vector of 0/1 as with FALSE/TRUE.
If you want to see which colors correspond with which numbers on your machine, check out
palette()
# [1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow" "gray"
So on my machine, a color of value 1 is black, 2 is red, etc. Check out ?palette to see how to change the default values.
Not exactly sure what your expected output is, but maybe something like:
x <- structure(c(0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0), .Dim = c(14L,
1L))
image(x, col = grey.colors(start = 1, end = 0, n = 2))
To give:
Edit: A nicer version:
z <- structure(c(0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0), .Dim = c(14L,
1L))
x <- 1:nrow(z)
y <- 1:ncol(z)
image(x, y, z, col = grey.colors(start = 1, end = 0, n = 2), yaxt = "n", xaxs = "r")
I have used corrplot package to plot my data-pairs. But all the relationships in my data are positive.
Mydata<-read.csv("./xxxx.csv")
M <-cor(Mydata)
corrplot(M,,col=rev(brewer.pal(n=8, name="RdYlBu")))
Using ggcorr, I also can't find any solution to deal with the issue.
How to generate a user-defined colormap with the corresponding range from 0 to 1?
If you are trying to map the entire range of the colormap to only the positive correlations, you could use col = rep(rev(brewer.pal(n=8, name="RdYlBu")), 2). This repeats the color sequence, and then cl.lim = c(0,1) forces corrplot to use only the 2nd half of the sequence, mapped to the range 0 to 1.
par(xpd=T)
corrplot(M,,'upper',
col = rep(rev(brewer.pal(n=8, name="RdYlBu")), 2),
cl.lim = c(0,1),
mar = c(1, 0, 1, 0))
Some reproducible data
set.seed(12)
x = (1:100)/100
Mydata = data.frame(a=x^runif(1, 0, 50),
b=x^runif(1, 0, 50),
c=x^runif(1, 0, 50),
d=x^runif(1, 0, 50),
e=x^runif(1, 0, 50),
f=x^runif(1, 0, 50),
g=x^runif(1, 0, 50),
h=x^runif(1, 0, 50),
i=x^runif(1, 0, 50))
M = cor(Mydata)
we met with interesting problem of R. We wanted to find 100 degree of any given markov chain matrix.
The problem is that after some time matrix suddenly goes to zero. We think that is causation of approximately of matrix multiplication.
Do you have any ideas how to fix it?
a <- c(0.2, 0, 0.7, 0.1)
b <- c(0.5, 0.2, 0.2, 0.1)
c <- c(0, 0.3, 0.7, 0)
d <- c(0.1, 0.8, 0, 0.1)
mat <- matrix(c(a,b,c,d), ncol=4, byrow=TRUE)
z <- mat
for(i in 1:100)
{
print(i)
z <- z%*%z
print(z)
}