Displaying 20 results from an estimated 50000 matches similar to: "Using a Data Frame/Matrix outside a function"
2024 Dec 04
3
Undocumented behaviour of diag when replacing the diagonal of a matrix?
Dear list,
is anyone aware of the following behavious of diag when used to replace
diagonals (plural!) of a matrix?
Small example: The following is documented and clearly to be expected:
A <- matrix(0, nrow = 5, ncol = 5)
diag(A) <- 1; A
BUT, what about the following? When executing the code of `diag<-` line
by line, it throws errors. So why does it work?
diag(A[-1, ]) <- 2; A
2006 Sep 21
2
Exponentiate a matrix
Suppose I have a square matrix P
P <- matrix(c(.3,.7, .7, .3), ncol=2)
I know that
> P * P
Returns the element by element product, whereas
> P%*%P
Returns the matrix product.
Now, P^2 also returns the element by element product. But, is there a
slick way to write
P %*% P %*% P
Obviously, P^3 does not return the result I expect.
Thanks,
Harold
[[alternative HTML version
2012 Jun 07
2
How to build a large identity matrix faster?
Hello, I am trying to build a large size identity matrix using diag(). The
size is around 23000 and I've tried diag(23000), that took a long time.
Since I have to use this operation several times in my program, the running
time is too long to be tolerable. Are there any alternative for diag(N)?
Thanks
Cheers,
yct
[[alternative HTML version deleted]]
2010 Sep 22
1
Newey West and Singular Matrix
dear R experts: ?I am writing my own little newey-west standard error
function, with heteroskedasticity and arbitrary x period
autocorrelation corrections. ?including my function in this post here
may help others searching for something similar. it is working quite
well, except on occasion, it complains that
Error in solve.default(crossprod(x.na.omitted, x.na.omitted)) :
system is
2004 Oct 13
2
diagonal matrix construction
Hi,
I have worked long and hard and looked in the manuals and am having a hard time constructing a diagonal matrix. I can get the diagonals out of a matrix but can't construct the matrix with just the diagonals. I have been on the web site and manuals and I think that it says to use:
dsj <- diag (three = 1, nrow, ncol = 7) three is the name of my matrix and
2010 Sep 23
1
Newey West and Singular Matrix + library(sandwich)
thank you, achim. I will try chol2inv.
sandwich is a very nice package, but let me make some short
suggestions. I am not a good econometrician, so I do not know what
prewhitening is, and the vignette did not explain it. "?coeftest" did
not work after I loaded the library. automatic bandwidth selection
can be a good thing, but is not always.
as to my own little function, I like the
2011 Dec 15
2
Data Manipulation - make diagonal matrix of each element of a matrix
Dear R list,
I have the following data:
set.seed(1)
n <- 5 # number of subjects
tt <- 3 # number of repeated observation per subject
numco <- 2 # number of covariates
x <- matrix(round(rnorm(n*numco),2), ncol=numco) # the actual covariates
x
> x
[,1] [,2]
[1,] -0.63 -0.82
[2,] 0.18 0.49
[3,] -0.84 0.74
[4,] 1.60 0.58
[5,] 0.33 -0.31
I need to form a matrix
2011 Mar 26
2
Building a matrix so that matrix(r, c)<-matrix(c, r) with No For Loops
Hello,
I would like to take advantage of the upper.tri() function here but I don't
know exactly. Here is some working code...
i<-5
fi<-matrix(0,nrow=i,ncol=i)
for(r in 1:i){
for(c in 1:i){
if(r==c){
fi[r,c]<-1
}else if(r<c){
fi[r,c]<-1-runif(1)^.5
}else{
fi[r,c]<-fi[c,r]
}
}
}
So far I know I can simplify this code to 5 lines (no for loops):
i<-5
2005 Jan 03
2
Memory Efficient Methods for Building Matrix
Dear List:
I am having to build a block-diagonal matrix (vl) and am currently using
the following code.
I<-diag(sample.size)
vl<-kronecker(I,vl.mat)
This code works fine, but for large N, it is a huge memory hog. Is there
a more efficient method for constructing vl?
Thanks,
Harold
[[alternative HTML version deleted]]
2009 Sep 04
5
< 0 x 0 matrix >
Hi,
Does anybody know, what is going on here?
> diag(sqrt(1))
[,1]
[1,] 1
> diag(sqrt(0.3333))
<0 x 0 matrix>
> sqrt(1)
[1] 1
> sqrt(0.3333)
[1] 0.5773214
BR, Markku Karhunen
researcher
University of Helsinki
2010 Jun 15
2
Integration problem: error in invoking an outside function
Dear all,
Currently I am trying to integrate a function which depends on four
variables, two of which are given, one is given in the integrate function,
so there is one variable to integrate on.
The code is as follows:
Pmatrix =
function(th) {
P = matrix(nrow=6, ncol=6, data=0)
P[1,1] = P[2,1]=P[3,2]=P[4,3]=P[5,4]=P[6,5]= exp(-th)
P[,6] = 1-exp(-th)
return(P)}
lim.verd =
2003 Apr 01
1
setGeneric
I'm still having difficulties with methods...is the following behavior expected?
(This is without loading any libraries)
R : Copyright 2003, The R Development Core Team
Version 1.7.0 Under development (unstable) (2003-03-31)
> ls()
character(0)
>
> diag(2)
[,1] [,2]
[1,] 1 0
[2,] 0 1
> setGeneric("diag")
[1] "diag"
> diag(2)
Error in
1998 Sep 09
2
diag() losing dimnames
Using diag() to extract the diagonal of a matrix loses all but the first dimname
(R 0.62.3). The problem seems to be in [ ]:
>
> x <- matrix(1:9,3,3)
> dimnames(x) <- list(c("a", "b", "c"), c("a", "b", "c"))
> x
a b c
a 1 4 7
b 2 5 8
c 3 6 9
> diag(x)
a NA NA
1 5 9
> x[c(1,5,9)]
a NA NA
1 5 9
>
Paul
2011 Apr 04
1
simulating a VARXls model using dse
Hello,
Using the dse package I have estimated a VAR model using estVARXls().
I can perform forecasts using forecast() with no problems, but when I
try to use simulate() with the same model, I get the following error:
Error in diag(Cov, p) :
'nrow' or 'ncol' cannot be specified when 'x' is a matrix
Can anyone shed some light on the meaning of this error? How can I
2012 Dec 11
1
converting manual command to loop command
Dear useRs,
i have certain commands for some operations in R. They are good if you have a small dataset but my dataset, apart from what i used in the recent past, is prety large. I want to convert these massive sets of commands into a simple loop.
Your help is required on it
thanks in advance
eliza
kindly note:
"e" is matrix whose each column has to be executed into a distance vector
2009 Jul 09
1
Converting indices of a matrix subset
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I have two matrices:
> m1 <- matrix(1,4,4)
> m1
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 1 1 1
[3,] 1 1 1 1
[4,] 1 1 1 1
> m2 <- matrix(0,3,3)
> diag(m2) <- 1
> m2
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
I want to get indicies from m2
2013 Apr 12
2
processing matrix equation derived from rows of two matrices
Hi,
May be this helps:
?tb[1,]%*%(((val-rep(meansb79[1,],5))^2)/6)
#??????? [,1]
#[1,] 1.47619
tryvarb<-c(1,2,3,4,4,4,4)
?var(tryvarb)
#[1] 1.47619
tb[2,]%*%(((val-rep(meansb79[2,],5))^2)/6)
#???????? [,1]
#[1,] 1.904762
sapply(seq_len(nrow(tb)),function(i) tb[i,]%*%(((val-rep(meansb79[i,],5))^2/6)))
# [1] 1.4761905 1.9047619 1.9047619 1.9047619 1.9047619 2.2857143 1.9047619
# [8] 1.9047619
1997 Oct 29
4
R-beta: new executable
I have just put up a new executable as a replacement for the one in
rseptbeta.zip
there have only been a few changes; mostly to the menu's. I am about
to start on a major overhaul including getting survival to work and
grabbing the 0.60 version once it's stable.
Please let me know about other enhancements you want....
robert
1997 Oct 29
4
R-beta: new executable
I have just put up a new executable as a replacement for the one in
rseptbeta.zip
there have only been a few changes; mostly to the menu's. I am about
to start on a major overhaul including getting survival to work and
grabbing the 0.60 version once it's stable.
Please let me know about other enhancements you want....
robert
2011 Sep 26
2
Triangular matrix upper to down
Hi,
suppose that we have a triangular upper matrix A
test <- matrix(ncol = 4, nrow = 4)
test[1, ] <- c(NA,1,1,1)
test[2, ] <- c(NA,NA,1,1)
test[3, ] <- c(NA,NA,NA,1)
test[4, ] <- c(NA,NA,NA,NA)
I know how quickly set diagonal value diag(test) <- 1. But how quickly set
down value i.e. matrix is symmetrical? Is there in r project any quickly
function?
Thanks,
Best
Marcin