I have a spatial weight file in csv that I want as listw object in R. The file has the following 3 variables (left to right in the file) -- OID_, NID and WEIGHTS. NID stands for the neighbors and OID_ as the origins. There are 217 origins with 4 neighbors each. I have been able to read the csv file as a data frame (test.csv). Then I tried to check whether the OID_ variable is in the right place in the dataframe. I used "match" for that using: o <- match(OID_, OID_) I am not sure whether this is the right way to match. Please advice. Anyway, next I created a matrix object (m) using: m <- as.matrix(test.csv[, -1]) Then I created object m1, using: m1 <- m[o, o] Finally, I tried creating listw object using: mat2listw(m1) Here I get an error that x is not a square matrix. Not sure what to do now. Any helo appreciated! Thanks, Shishm
Your post is unlikely to solicit a helpful response. As you do not adhere to the posting guide, it is quite impossible for us to figure out from your description what is going wrong. Please provide a self-contained example that reproduces the problem (i.e., code with simulated or actual data that we can copy paste to the R-prompt). Moreover, chances are that when you try to reproduce the error with simulated code, you will figure out what your mistake is. Best, Daniel shish matt wrote:> > I have a spatial weight file in csv that I want as listw object in R. > The file has the following 3 variables (left to right in the file) -- > OID_, NID and WEIGHTS. NID stands for the neighbors and OID_ as the > origins. There are 217 origins with 4 neighbors each. > > > I have been able to read the csv file as a data frame (test.csv). Then I > tried to check whether the OID_ variable is in the right place in the > dataframe. I used "match" for that using: > o > <- match(OID_, OID_) > I am not sure whether this is the right way to match. Please advice. > > Anyway, next I created a matrix object (m) using: > > m > <- as.matrix(test.csv[, -1]) > > Then I created object m1, using: > > m1 > <- m[o, o] > > Finally, I tried creating listw object using: > mat2listw(m1) > Here I get an error that x is not a square matrix. > > Not sure what to do now. Any helo appreciated! > > Thanks, > Shishm > > ______________________________________________ > R-help@ mailing list > 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. >-- View this message in context: http://r.789695.n4.nabble.com/Matching-tp3950158p3951527.html Sent from the R help mailing list archive at Nabble.com.
Hi Shishm, Part of what you do seems reasonable. My guess is that: o <- match(OID_, OID_) is not doing what you want/think. What happens when you look at the results? e.g.,: print(o) You use that later on as row and column indices to extract from m, to create m1. The fact taht mat2listw complains that m1 is not square, makes me guess the indices used to create m1 are not working correctly, which points back to o <- match(). With a more detailed example we can give more detailed suggestions Hope this helps some, Josh On Fri, Oct 28, 2011 at 3:22 PM, shish matt <shishm at yahoo.com> wrote:> I have a spatial weight file in csv that I want as listw object in R. > The file has the following 3 variables (left to right in the file) -- OID_, NID and WEIGHTS. NID stands for the neighbors and OID_ as the origins. There are 217 origins with 4 neighbors each. > > > I have been able to read the csv file as a data frame (test.csv). Then I tried to check whether the OID_ variable is in the right place in the dataframe. I used "match" for that using: > o > <- match(OID_, OID_) > I am not sure whether this is the right way to match. Please advice. > > Anyway, next I created a matrix object (m) using: > > m > <- as.matrix(test.csv[, -1]) > > Then I created object m1, using: > > m1 > <- m[o, o] > > Finally, I tried creating listw object using: > mat2listw(m1) > Here I get an error that x is not a square matrix. > > Not sure what to do now. Any helo appreciated! > > Thanks, > Shishm > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, ATS Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/