What are these Bytes in the OID returned by X509Certificte.getExtensionValue("2.5.29.32")? - x509certificate

I am pulling the 2.5.29.32 Certificate Policy OID bytes from an X.509 certificate. I am not using BouncyCastle.
bytes = _cert.getExtensionValue("2.5.29.32");
The raw bytes I get are
4, 15, 48, 13, 48, 11, 6, 9, 96, -122, 72, 1, 101, 2, 1, 11, 42
After DER decode (type = 4, size = 15), I have:
48, 13, 48, 11, 6, 9, 96, -122, 72, 1, 101, 2, 1, 11, 42
What I am after is the:
96, -122, 72, 1, 101, 2, 1, 11, 42 (2.16.840.1.101.2.1.11.42)
What are the bytes: 48, 13, 48, 11, 6, 9?

48 (aka 0x30) is the tag for CONSTRUCTED SEQUENCE (all SEQUENCE values are CONSTRUCTED, but still).
The 13 is the length value, as is the 11 two bytes later.
6 (aka 0x06) is the tag for OBJECT IDENTIFIER (aka OID), and 9 is its length.
So 30 0D 30 0B 06 09 ... is SEQUENCE(SEQUENCE(OID(... [ 9 bytes ]))).
ITU-T X.680-201508, the ASN.1 language.
ITU-T X.690-201508, the BER (and CER) and DER encodings for ASN.1 data.

In case anyone is interested in the code I wrote to do this...
private static byte CONSTRUCTED_SEQUENCE_TAG = 48;
private static byte OID_CODE = 6;
private static String extractPolicyOid(X509Certificate pCert) throws IOException {
byte[] _bytes = pCert.getExtensionValue("2.5.29.32");
if (_bytes == null) {
return null;
}
DerValue _der = new DerValue(new ByteArrayInputStream(_bytes));
_bytes = _der.getOctetString();
/*
* Skip header goo
*/
int _idx = 0;
while (_idx < _bytes.length) {
if (_bytes[_idx] == CONSTRUCTED_SEQUENCE_TAG) {
_idx += 2;
} else if (_bytes[_idx] == OID_CODE) {
_idx += 2;
} else {
break;
}
}
/*
* Create string version of OID
*/
StringBuffer _sb = new StringBuffer();
byte _byte = _bytes[_idx++];
_sb.append(_byte / 40);
_sb.append(".");
_sb.append(_byte % 40);
int _accum = 0;
for (; _idx < _bytes.length; _idx++) {
_byte = _bytes[_idx];
if (_byte << ~7 < 0) {
_accum = (_byte & 0x7f) << 7;
} else {
_accum += _byte;
_sb.append(".");
_sb.append(_accum);
_accum = 0;
}
}
return _sb.toString();
}

Related

SVD function in R. I want to get the singular values $d from a list of datasets. I want to put it in a table form

I want to use the svd function to get the singular values of a large datasets in a list.
When I use the svd function in a single matrix, I am able to use $d and get the values, but for the list I cannot get the output.
Here is the code for a matrix and the output.
tb = matrix(c(64, 112, 59, 174, 111, 37,
39, 135, 115, 92, 161, 70,
93, 119, 50, 142, 20, 114,
149, 191, 62, 17, 145, 21,
60, 37, 29, 74, 42, 242), nrow = 5, ncol = 6, byrow = TRUE)
## Compute SVD of tb
#
my_svd = svd(tb)
## Retrieve (save) singular values
#
sv = my_svd$d
## Compute ratio between "1st/2nd" & "2nd/3rd" singular values
#
ratios = matrix(c(sv[1]/sv[2], sv[2]/sv[3]), nrow = 1)
colnames(ratios) = c("sv1/sv2", "sv2/sv3")
## Print ratios
ratios
How do I apply this to the list of dataset?
my current code
svdresult <- lapply(d1,svd)
svdresult
d1 is my list of dataset
How do I get svdresult$d on the list of datasets.
Thanks in advance
Maybe something like the following?
get_svd_ratios <- function(data) {
sv = svd(data)$d
n = length(sv)
ratios = matrix(sv[1:(n - 1)] / sv[2:n] , nrow = 1)
names = paste(
paste0("sv", 1:(n - 1)),
paste0("sv", 2:n),
sep = "/"
)
colnames(ratios) = names
return(ratios)
}
lapply(list(tb), get_svd_ratios)
# [[1]]
# sv1/sv2 sv2/sv3 sv3/sv4 sv4/sv5
# [1,] 2.261771 1.680403 1.29854 2.682195

geeglm giving error "incompatible types (from language to character) in subassignment type fix"

I have a problem using geeglm form the geepack library. I don't understand what is wrong:
df <- structure(list(ID = c(3, 3, 10, 11, 11, 13),
VO2max = c(3584.2, 2235.7, 4191.6, 2025.1, 3184.6, 2134.9),
HRR120 = c(56, 34, 60, 57, 30, 67),
Age = c(16, 24, 29, 11, 27, 34),
Weight = c(63.8, 72, 81, 41, 108, 55),
Height = c(176, 167, 178, 150, 176, 156 )),
class = c("data.frame"))
library(geepack)
geeglm(formula = "VO2max ~ HRR120 ",family = "gaussian",corstr = "independence",id = ID,data = df)
Error in mcall$formula[3] <- switch(match(length(sformula), c(0, 2, 3)), :
incompatible types (from language to character) in subassignment type fix
I tried
df$ID <- as.character(df$ID)
df$ID <- as.factor(df$ID)
without any luck. Does anyone have an explanation ?
Just unquote formula and everything works:
geeglm(formula = VO2max ~ HRR120, family = "gaussian",corstr = "independence",id = ID,data = df)
Call:
geeglm(formula = VO2max ~ HRR120, family = "gaussian",
data = df, id = ID, corstr = "independence")
Coefficients:
(Intercept) HRR120
2764.311798 2.533649
Degrees of Freedom: 6 Total (i.e. Null); 4 Residual
Scale Link: identity
Estimated Scale Parameters: [1] 666987
Correlation: Structure = independence
Number of clusters: 4 Maximum cluster size: 2

How to edit the score ranges in highcharter-histogram tooltip

I am using the highcharter package to make a histogram. I want to get rid of the default score range display -(x, y]- and replace it with something that reads: score ranges: x to y
library(highcharter)
apple <- c(0, 22, 5, 32, 34, 35, 56, 67, 42, 67, 12, 99, 46, 78, 43, 67, 33, 11)
hchart(apple, color = "#a40c19", breaks = 20) %>%
hc_yAxis(title = list(text = "Number of Apples")) %>%
hc_xAxis(title = list(text = "Score (0-100)")) %>%
hc_tooltip(borderWidth = 1, sort = TRUE, crosshairs = TRUE,
pointFormat = "Score Range: {point.x} to {point.x} <br> Number of Apples: {point.y}") %>%
hc_legend(enabled = FALSE)
For example, in the picture below, I want to get rid of the heading (30, 35] and replace it with Score Range: 30 to 35.
First of all you need to know what is the interval length of your histogram:
library(highcharter)
apple <- c(0, 22, 5, 32, 34, 35, 56, 67, 42, 67, 12, 99, 46, 78, 43, 67, 33, 11)
h <- hist(apple, breaks = 20)
d <- diff(h$breaks)[1]
d
> d
[1] 5
Now, you need to use pointFormatter instead of pointFormat because allow you to have more control in the output of the tooltip. pointFormat need a string template and pointFormatter need a javascript function.
You put the delta in that function to get the right limits in for every interval. Obviously you can do it more elegant but that's the idea.
hchart(h, color = "#a40c19", breaks = 20) %>%
hc_yAxis(title = list(text = "Number of Apples")) %>%
hc_xAxis(title = list(text = "Score (0-100)")) %>%
hc_tooltip(borderWidth = 1, sort = TRUE, crosshairs = TRUE,
headerFormat = "",
pointFormatter = JS("function() {
return 'Score Range:' + (this.x - 5/2) + ' to ' + (this.x + 5/2) + '<br> Number of Apples:' + this.y;
}")) %>%
hc_legend(enabled = FALSE)
Finally you use headerFormat = "" to remove the header.

Grouping iGraph Vertices in a weighted network by color/subgroup in R

I am struggling to group my network by the subgroups. I currently have the following network:
Current Network
Which I have assigned the subgroups. I would like to plot all of the subgroups clustered together. To get a graph that looks like this:
Goal
Most algorithms seems to cluster based on weights in the graph. But I want to tell it to cluster based on the node colors/labelled subgroups. This is what I have now to code this network:
#Graph with Weighted matrix
g_weighted<-graph.adjacency(WeightedMatrix, mode="undirected", weighted = TRUE)
#Make nodes different colors based on different classes
numberofclasses<-length(table(ConnectedVertexColor))
V(g_weighted)$color=ConnectedVertexColor
Node_Colors <- rainbow(numberofclasses, alpha=0.5)
for(i in 1:numberofclasses){
V(g_weighted)$color=gsub(unique(ConnectedVertexColor[i],Node_Colors[i],V(g_weighted)$color)
}
#Plot with iGraph
plot.igraph(g_weighted,
edge.width=500*E(g_weighted)$weight,
vertex.size=15,
layout=layout.fruchterman.reingold, ##LAYOUT BY CLASS
title="Weighted Network",
edge.color=ifelse(WeightedMatrix > 0, "palegreen4","red4")
)
legend(x=-1.5, y=-1.1, c(unique(ConnectedVertexColor)), pch = 19, col=Node_Colors, bty="n")
The ConnectedVertexColor is a vector the contains information about if the node is a lipid, Nucleotide, Carb or AA. I have tried the command V(g_weighted)$community<-ConnectedVertexColor
but I cannot get this to transfer into useful information for iGraph.
Thanks for advice in advance.
Since you do not provide data, I am making a guess based on your "Current Network" picture. Of course, what you need is a layout for the graph. Below I provide two functions to create layouts that might meet your needs.
First, some data that looks a bit like yours.
EL = structure(c(1, 5, 4, 2, 7, 4, 7, 6, 6, 2, 9, 6, 3, 10,
7, 8, 3, 9, 8, 5, 3, 4, 10, 13, 12, 12, 13, 12, 13, 15, 15,
11, 11, 14, 14, 11, 11, 11, 15, 15, 11, 11, 13, 13, 11, 13),
.Dim = c(23L, 2L))
g2 = graph_from_edgelist(EL, directed = FALSE)
Groups = c(rep(1, 10), 2,2,3,3,3)
plot(g2, vertex.color=rainbow(3)[Groups])
First Layout
GroupByVertex01 = function(Groups, spacing = 5) {
Position = (order(Groups) + spacing*Groups)
Angle = Position * 2 * pi / max(Position)
matrix(c(cos(Angle), sin(Angle)), ncol=2)
}
GBV1 = GroupByVertex01(Groups)
plot(g2, vertex.color=rainbow(3)[Groups], layout=GBV1)
Second Layout
GroupByVertex02 = function(Groups) {
numGroups = length(unique(Groups))
GAngle = (1:numGroups) * 2 * pi / numGroups
Centers = matrix(c(cos(GAngle), sin(GAngle)), ncol=2)
x = y = c()
for(i in 1:numGroups) {
curGroup = which(Groups == unique(Groups)[i])
VAngle = (1:length(curGroup)) * 2 * pi / length(curGroup)
x = c(x, Centers[i,1] + cos(VAngle) / numGroups )
y = c(y, Centers[i,2] + sin(VAngle) / numGroups)
}
matrix(c(x, y), ncol=2)
}
GBV2 = GroupByVertex02(Groups)
plot(g2, vertex.color=rainbow(3)[Groups], layout=GBV2)

Variable lengths differ error when using lm in R

I am trying to run a linear model on time and a monthly factor, however I am getting the error:
Error in model.frame.default(formula = ts.data ~ time2 + factor(month2), :
variable lengths differ (found for 'time2')
This is how I created the variables:
time2<-seq(along=ts.data)
month2<-rep(1:12,length=length(ts.data))
However running length(time2), length(month2) and length(ts.data) gives the same number, does anyone know how I fix the error?
Trying to run lm :
lm(ts.data~time2+factor(month2))
The data I am using:
structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 78238, 73928, 70708, 75175, 70744, 65604, 61227, 62635,
47652, 51507, 81874, 98236, 99401, 94756, 94697, 93732, 100334,
139355, 88575, 94169, 86084, 98249, 95321, 87822, 80256, 81875,
86293, 80712, 79533, 82847, 84498, 84185, 78382, 82701, 80491,
91140, 86847, 96727, 101295, 99450, 87783, 101246, 97913, 100081,
96346, 93608, 90648, 99105, 90920, 84960, 82591, 88090, 89980,
87778, 87429, 81898, 77285, 80369, 73193, 65139, 60126, 57219,
94204, 112472, 157199, 154791.5, 154294.88, 161920.63, 147408.75,
134418, 132158.5, 104572.5, 96831, 91045.88, 141182.63, 214759.25,
216647, 184598.38, 210794.38, 182403.75, 193001.63, 176807.38,
186552.63, 201375.88, 181861.25, 193234.88, 187240.25, 168242,
172475.13, 188996.25, 179663.88, 192861.63, 187461.25, 188670.5,
198826.25, 208696.5, 180490.75, 202265.88, 187966.13, 203342.13,
194850.38, 230582.63, 212517, 223432.5, 196511.63, 229582.25,
206120.63, 225629.88, 209769.63, 210797.63, 213215.75, 215144.88,
223266, 230747.63, 228573, 223828.88, 202102.88, 192863.63, 206675.13,
195647.5, 173897.25, 183788.88, 158511.38, 138559, 114163.25,
110399.13, 164751.13, 270772, 90430, 81719, 79183, 85428, 79372,
72361, 66207, 55403, 51693, 60280, 98698, 123059, 121550, 107662,
107863, 107630, 114685, 169659, 100104, 107598, 97728, 112850,
107784, 97580, 92709, 99098, 99482, 100543, 98856, 106081, 108248,
104769, 96966, 100093, 103107, 114944, 108001, 126289, 135213,
129717, 121688, 134421, 127318, 127412, 121922, 119045, 116989,
126286, 116707, 106627, 98219, 111225, 117279, 113725, 114633,
100633, 95478, 98394, 87616, 75329, 68274, 70658, 122995, 145224,
155833, 131896.5, 138340.63, 145610, 130653.13, 122562.75, 115850.5,
91749.88, 81787.13, 85457.5, 142931.63, 214970, 216836.63, 175902,
180757.88, 175233.63, 168982.13, 168727.25, 173501, 182731.38,
152260.63, 182607, 179326.5, 157693.13, 161004.75, 172990.5,
166204.38, 175172.63, 186446.13, 202645.38, 202500.25, 204148.38,
187763.5, 207269.75, 183334.88, 206552.5, 207270.13, 226123.88,
239037.88, 214656.38, 216552.75, 231406.75, 207365.63, 217873.5,
200308.88, 201696.5, 208984.75, 227723.38, 212083.25, 206262.38,
186596.25, 215496.63, 199399, 184933.25, 195925.63, 190318.63,
170375.38, 171624.13, 154537.13, 133532.25, 119179.13, 113297.88,
174946.5, 304690, 108567, 99358, 97299, 103628, 96936, 89254,
83761, 72058, 66685, 74491, 117292, 139878, 139585, 130180, 130079,
127562, 136152, 197149, 118619, 127875, 118094, 134989, 130688,
121475, 112367, 114805, 117087, 118526, 118038, 123988, 127511,
125790, 116702, 123049, 124260, 141232, 133809, 156349, 162637,
158367, 144491, 164389, 155305, 161401, 151829, 144188, 142702,
156405, 141937, 129857, 120318, 132823, 138201, 135058, 129275,
119897, 112924, 120385, 108134, 94062, 86695, 88434, 145426,
167100, 184196.63, 166628.38, 168193.13, 190280.88, 154984.75,
153784.38, 148033.75, 121304.25, 107303.25, 108003.13, 168770,
240983.75, 242817.13, 220229.38, 222805.75, 205068.75, 205204.25,
192598.25, 206565.38, 227284.88, 199258.25, 227122.88, 209076.13,
194855.5, 196357.25, 206865.88, 209580.13, 222190.88, 234610.88,
229339.13, 219321.63, 232571.75, 218584.75, 246116.38, 229563,
256776.75, 257335.25, 271507, 272014, 265850.5, 253426.63, 291759.63,
262608.88, 279417.25, 264583.25, 256634.88, 271024.88, 283927.13,
270597.38, 264222.5, 235009.13, 258379.25, 246485.5, 240163.25,
238369.88, 240961.5, 219826.75, 212077.5, 194937, 166299, 141284.88,
130153.38, 206775, 342062.88), .Dim = c(64L, 8L), .Dimnames = list(
NULL, c("Week_Number", "Campaign_Period", "Control_Traffic",
"Control_Revenue", "VOD_Test_Traffic", "VOD_Test_Revenue",
"TV_Test_Traffic", "TV_Test_revenue")), .Tsp = c(1, 2.21153846153846,
52), class = c("mts", "ts", "matrix"))
If we are creating grouping variables based on the number of rows, we need to change the 'month2' and 'time2'.
month2<-rep(1:12,length=nrow(ts.data))
time2<-seq_len(nrow(ts.data))
res <- lm(ts.data~time2 + factor(month2))
coef(res)
# Week_Number Campaign_Period Control_Traffic Control_Revenue VOD_Test_Traffic VOD_Test_Revenue TV_Test_Traffic TV_Test_revenue
#(Intercept) 0.0000000000000213162821 3.384444444 78799.578 157220.4207 87712.9656 148735.2930 106055.5914 177326.337
#time2 0.9999999999999994448885 -0.017777778 123.605 727.3023 383.8344 966.6405 465.7336 1229.196
#factor(month2)2 0.0000000000000152835379 0.017777778 -1177.438 2858.4910 -1320.1678 -8588.9322 -1128.5669 -6726.196
#factor(month2)3 0.0000000000000008207055 0.035555556 5419.457 10544.3136 6779.6644 346.1573 7382.8661 1173.044
#factor(month2)4 0.0000000000000016587917 0.053333333 8603.018 27254.5313 12531.1633 23542.1418 12777.1325 28186.515
#factor(month2)5 0.0000000000000012801265 -0.268888889 3290.677 6985.0394 7531.8356 3638.7607 7201.7339 5969.374
#factor(month2)6 0.0000000000000020182926 -0.251111111 12858.272 5428.8610 20021.4011 4320.5682 21940.0003 7326.704
#factor(month2)7 0.0000000000000016906446 -0.233333333 1297.067 6299.8347 3690.1667 327.4537 2400.8667 -451.516
#factor(month2)8 0.0000000000000016516546 -0.015555556 1838.662 6690.3563 -832.6678 -2303.4348 445.1331 3647.310
#factor(month2)9 0.0000000000000015682557 0.002222222 -5728.743 -12651.4220 -7622.1022 -22135.3253 -8178.0006 -15978.562
#factor(month2)10 0.0000000000000003302248 0.020000000 -1715.348 -5722.0704 -2630.9367 -11870.0938 -2470.1342 -9128.055
#factor(month2)11 0.0000000000000022008184 0.037777778 1179.647 -5052.7747 1691.6289 -8744.7323 2258.9322 -5674.003
#factor(month2)12 0.0000000000000033608693 0.055555556 5039.042 4606.1469 5908.5944 4559.9012 7788.3986 6991.025

Resources