3D rotation matrix (rotate to another reference system) - math

Is there a direct approach to construct a rotation matrix from the following input?
Say there is a standard perpendicular reference: X[1,0,0], Y[0,1,0], Z[0,0,1] and I want rotation matrix to rotate it to match another perpendicular reference X'[a1,b1,c1], Y'[a2,b2,c2], Z'[a3,b3,c3]. Vectors are unit vectors.
Is it possible that the matrix would be like below?
a1, a2, a3
b1, b2, b3
c1, c2, c3

Given points defined in a XYZ coordinate system, you transform them to a X'Y'Z coordinate system with a 3x3 rotation matrix. In general, the components of the local a, b, and c axes arranged in columns in the world coordinates represent the local->world transformation for that system such that
| x_world | | a1 b1 c1 | | x_local |
| y_world | = | a2 b2 c2 | | y_local |
| z_world | | a3 b3 c3 | | z_local |
and the reverse transformation (with a matrix transpose)
| x_local | | a1 a2 a3 | | x_world |
| y_local | = | b1 b2 b3 | | y_world |
| z_local | | c1 c2 c3 | | z_world |
Now to transform between any two coordinate systems with local->world rotation matrices XYZ and X'Y'Z' (note in your case XYZ is the 3x3 identity matrix) you chain the above for
point_x'y'z' = transpose(X'Y'Z') * (XYZ) * point_xyz
| x' | | a1 b1 c1 | | x | | a1 x + b1 y + c1 z |
| y' | = | a2 b2 c2 | | y | = | a2 x + b2 y + c2 z |
| z' | | a3 b3 c3 | | z | | a3 x + b3 y + c3 z |

Yes, the transformation from identity to any other transform is the other transform. Usually you do not call these things rotation matrices because they represent any arbitrary transformation. It may be that the transformation may not be achievable by rotation alone, as the transformation may be mirrored about one or two planes.
Please note while your using column vector's it is perfectly possible to also use row vectors in which case your results are just transposed, and multiplication order the reverse. Mathematically its the same thing. So check your system

Related

Can weighted data be used with the CrossTable function in R?

I have attached sample weights to my data using the code below;
s_w <- couple_dta$h_sw /1000000
design <-svydesign(ids =~s_unit + hh, strata =~res , weights = s_w ,data =c_dta)
I had earlier created crosstables with unweighted data using the code;
CrossTable(c_dta$varA, c_dta$varB, prop.c = FALSE ,prop.r = FALSE , prop.chisq = FALSE , format = "SPSS")
The result of CrossTable with unweighted data is something below.
Variable B
Variable A | f | mf | m | Row Total |
---------------------|-----------|-----------|-----------|-----------|
m | n1 | n2 | n3 |n1 +n2+n3 |
|n1/N x 100 | n2/N x 100|n3/N x 100 | |
---------------------|-----------|-----------|-----------|-----------|
mf | n5 | n6 | n7 | n5+n6+n7 |
|n5/N x 100 |n6/N x 100 | n7/N x 100| |
---------------------|-----------|-----------|-----------|-----------|
f | n8 | n9 | n10 |n8+n9+n10 |
|n8/N x 100 |n9/N x 100 |n10/N x 100| |
---------------------|-----------|-----------|-----------|-----------|
Column Total | n1+n5+n8 | n2+n6+n9 |n3+n7+n10 | N |
---------------------|-----------|-----------|-----------|-----------|
Is there a way of incorporating weights to the second data. I have looked at 'prop.table(svytable)' but not sure how to proceed, given that I would also like to display the number of observations in each cell and the corresponding percentage.
Thank you in advance
Edit : I have used the svytable and Crosstable functions to achieve my goal.
table2 <- svytable(~c_dta$VarA + c_dta$wrd_VarB, design=design)
CrossTable(table2 ,prop.c = FALSE ,prop.r = FALSE , prop.chisq = FALSE , format = "SPSS")

Add a value from a column in one table based off finding a result in another in R

I have a data table in R:
|gene | prom_65| prom_66| amast_69| amast_70| markerID|
|:--------------|---------:|---------:|---------:|---------:|---------:|
|ABC | 24.7361| 25.2550| 31.2974|45.4209 |16:123234_T/C; 16:54352342_A/T; 16:747564_T/G|
|DFG | 107.3580| 112.9870| 77.4182| 86.3211| 16:3453453_G/A; 16:765753453_A/T; 16:65345345_T/G|
|LKP | 72.0639| 86.1486| 68.5747| 77.8383| 16:25234453_G/C; 16:876456546_A/T; 16:4535_T/G|
|KLF | 43.8766| 53.4004| 34.0255| 38.4038| 16:87484_G/A; 16:5435_A/T; 16:747564_T/G|
|PPO | 2382.8700| 1871.9300| 2013.4200| 2482.0600| 16:785_T/C; 16:5435_A/T; 16:747564_T/G|
|LWPV | 49.6488| 53.7134| 59.1175| 66.0931| 16:123_T/C; 16:54564_A/T; 16:54646_T/G|
I have another data table:
|markerid | prom_65| prom_66| amast_69| amast_70| pvalue|
|:--------------|---------:|---------:|---------:|---------:|---------:|
|16:123234_T/C |x | x | x | x | x |
|16:3453453_G/A| x | x | x x | x |
I would like to add the gene column to table two for the markerid that matches the relevant gene in table one. In table one the markerIDs are all separated by a semi-colon and a markerID will only ever appear within one gene row in table1. In this example the output should look like this:
|markerid | prom_65| prom_66| amast_69| amast_70| pvalue |gene|
|:--------------|---------:|---------:|---------:|---------:|---------:|
|16:123234_T/C |x | x | x | x | x |ABC
|16:3453453_G/A | x | x | x | x | x |DFG
Not sure how to approach this in R.
Many thanks
Without a reproducible example of your table, it is hard to be sure what looks like the last column (it seems to be a list but not sure).
You can try on the second table:
Table2$gene <- sapply(Table2$markerid, function(x) Table1$Gene[grep(x,Table1$marker_id)])
Here an example with dummy dataframes:
dataA <- data.frame(Gene = LETTERS[1:5],
marker = paste(letters[6:10],"_A"))
Gene marker
1 A f _A
2 B g _A
3 C h _A
4 D i _A
5 E j _A
dataB <- data.frame(marker = letters[6:8])
marker
1 f
2 g
3 h
And now, if you use the sapply function:
dataB$Gene <- sapply(dataB$marker, function(x) dataA$Gene[grep(x,dataA$marker)])
1 f A
2 g B
3 h C
Does it look what you are trying to get ?
If it is not working, can you provide the output of str(Table1) ?

Finding Faces in a Geometric Directed Graph

I'm confronted with a rather unusual problem dealing with an directed geometric graph. Imagine the graph being borders of countries. I'm looking for a way to find the faces. My graph consists of directed edges which may form cycles (but not necessarily).
What I am looking for are the left and right faces as well as the predecessors and successors of the left and right faces for each edge.
Each face should be constructed anti-clockwise, meaning that the left face of an edge is always inside and the right face is outside of the specific face.
At the end of the day the nodes of the faces are geographic coordinates (lat and lon).
This is the information I am looking for (beginning from LeftFace..)
+------+-------+-------+----------+-----------+---------------------+--------------------+
| Edge | NodeA | NodeB | LeftFace | RightFace | PredecessorLeftFace | SuccessorRightFace |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E1 | P1 | P2 | A | C | E5 | E2 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E2 | P2 | P3 | A | C | E1 | E6 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E3 | P3 | P4 | A | B | E2 | E8 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E4 | P4 | P5 | A | C | E3 | E5 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E5 | P5 | P1 | A | C | E4 | E1 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E6 | P3 | P6 | B | C | E3 | E7 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E7 | P6 | P7 | B | C | E6 | E8 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
| E8 | P7 | P4 | B | C | E7 | E4 |
+------+-------+-------+----------+-----------+---------------------+--------------------+
For each directed edge add also opposite edge in a graph. Than, for each (directed) edge find face in that direction. That means, traverse face edges so that in every vertex choose leftmost neighboring edge, until path returns to starting vertex. To choose leftmost edge, 2D positions of vertices are needed.
Example of choosing leftmost edge: going from P3 to P4 (opposite of E3). In P4 there are two possibilities to continue path, P5 and P7. Now check angles on the left side of edges. P3-P4-P5 is ~90deg, and P3-P4-P7 is ~270deg. Angle P3-P4-P5 is smaller than P3-P4-P7, so next edge in path is E4, and next point in path is P5.
Algorithm:
For each directed edge add opposite edge
While there are edges in graph
Choose one directed edge
Find edges that enclose face (on left side) starting from that edge
Add face to list of faces
Remove face edges from graph

Removing Left Recursion from CFG

The following grammar has left recursion:
T -> Tx | TYx | YX | x
X -> xx
Y -> Yy | Yx | y
How do you go about removing left recursion. I read the wikipedia explanation, but I'm fairly new to CFGs so it did not make a lot of sense. Any help is appreciated? A plain english explanation would be even more appreciated.
In this example, you can follow Robert C. Moore's general algorithm to convert a rule with left recursion to a rule with right recursion:
A -> A a1 | A a2 | ... | b1 | b2 | ...
# converts to
A -> b1 A' | b2 A' | ...
A' -> e | a1 A' | a2 A' | ... # where e = epsilon
In our first case: A=T, a1=x, a2=Yx, b1=y, b2=x... (similarly for Y)
T -> YXT' | xT'
T' -> e | xT' | YxT'
X -> xx
Y -> yY'
Y' -> e | yY' | xY'

R: graphing upper and lower bounds with ggplot2

I have a dataset with three variables. One continous independent variable, one continous dependent variable, and a binary variable that catagorizes how the measurements were taken. Using ggplot, I know that I can make a scatter plot with the points colored by the catagory:
g <- ggplot(dataset, aes(independent, dependent))
g + geom_point(aes(color=catagory))
However, I want to know if there is a way to make a graph where there is a vertical line comming up from points of catagory 0 and a vertical line going down from points of catagory 1. It would look something like this:
- | | |
| | | |
| | | |
| | | |
- | | o |
| | | | |
| | o | | |
| | o | | | |
- | | | o | o
| | | | |
| o | | |
| | | |
+----|-----|-----|-----|-----|
The reason for wanting a plot like this is that one category represents an upper bound (the points with lines going downwards) and one represents a lower bound (the points with lines going upwards). Having these lines would make it easy to visualize the area which is between these bounds, and whether a function plotted on top could accurately represent the data:
- | | |
| | | |
| | | |
| | | |
- | | o | _____
| | | |_|__/
| | o |_/| |
| | o |__/| | |
- | | /| o | o
| _|_|/ | |
| / o | | |
|/ | | |
+----|-----|-----|-----|-----|
If there is any way to do this using ggplot or any other graphing library for R, I would love to know how. However, if it isn't possible, I'd be open to hearing other ways to represent this data. Simply distinguishing the catagories based on color doesn't do enough to emphasize the upper/lower bound nature of the catagories for my purposes.
The following could work for you, I hope I understood the problem well.
First, generating some random data for the dataframe, as no sample data was provided. The random numbers will make the plot ugly, I hope it will look better with real data:
dataset <- data.frame (
independent = runif(100),
dependent = runif(100),
catagory = floor(runif(100)*2))
Next, find the upper or lower part of the plot (=min/max of values) based on "catagory" for every case:
dataset$end[which(dataset$catagory == 0)] <- max(dataset$dependent)
dataset$end[which(dataset$catagory == 1)] <- min(dataset$dependent)
Now, we can plot data with geom_segment().
g <- ggplot(dataset, aes(independent, dependent, min, max))
g + geom_segment(aes(x=independent, y=dependent, xend=independent, yend=end, color=catagory))
Note, that I also added + theme_bw() + opts(legend.position = "none") parameters to the plot as it looked very strange with random datas.

Resources