what is Boolean x'.y+x.y' equal to - math

i am stuck with a boolean expression help me solve what x.y'+x'.y =?
i have exam today and i don't know how do solve this type. And in addition can someone recreate the boolean laws that involve two element instead of one for me? Thank you

There are only two inputs to the expression, so write out a truth table with the values of the inputs and for each term until you get the result.
x y x' y' x'.y x.y' x'.y+x.y'
0 0 1 1 0 ...
0 1 1 0 1 ...
1 1 0 0 0 ...
1 0 0 1 0 ...
When you have done that, look for patterns in the last column. You should then recognise the pattern as being the same as a single operator.
The pattern for the inputs is usually a Gray code so that the output column reflects changes due to only one input changing, which usually can help show up the pattern.
Alternatively, when you have your result, plot it in a grid and spot the pattern that way, e.g. for x+y you'd get
x\y 0 1
0 0 1
0 1 1

Related

Regression with several dummy variables

I am running a logistic regression and I want to control for the country of the respondents. I have 12 countries. I used the "fastDummy" package to create dummies for each country
ALL<-dummy_cols(ALL, select_columns = "country")
I get something like this:
country_Japan 1 1 0 0 0 0
country_Taiwan 0 0 1 1 0 0
country_China 0 0 0 0 1 1
and so on...
As you can see, the sum of all variables makes a perfect collinearity. For this reason, I cannot estimate the model.
I read that I need to include a variable with 0s as the last country dummy to avoid this collinearity. Is this correct? I included the intercept (a column with 1s) , but it did not help.
I would appreciate your suggestions. Thanks
Check the remove_first_dummy parameter in the dummy_cols function, i.e. set it to TRUE. This should solve your problem of multicollinearity.

Matches in binary columns-R

I am performing some prediction models. I have 2 binary columns , one with predicted values and the other one with the actual values.
Since the columns have few ones because it counts the number of people with cancer, i want to observe how many cases the model detected(how many real ones it predicted) and the percentage of sick persons correctly predicted.
Brief description of the data: the first column shows the real values and the seconde one shows the predicted values:
> predictedvsreal
real prediction
39240 0 0
39241 0 0
39242 0 0
39243 1 0
39244 0 1
39245 0 0
39246 0 0
39247 0 0
39248 1 1
39249 0 0
39250 0 0
39251 0 0
39252 0 0
Thanks!
Next time please include a reproducible example as it makes the question much better - both for letting people who answer have a concrete example to work with and to catch edge-cases, and for future readers to see a real example.
There are lots of good recommendations for how to create nice, minimal, reproducible examples at this link.
From what you describe, you want the table function, probably like this:
with(your_data, table(your_first_column_name, your_second_column_name))

torch.Tensor manipulation - Comparing two vectors

I've got two tensor objects repenting vectors:
(0110010),
(0111011)
I would like to compare between the two and create a new tensor vector:
(0110010)
Iterating over them in a loop is very slow, I know there is a solution for this in Matlab so I assume there is one for tensors as well.
To do a logical and operation for tensors containing only 1 and 0 elements you could use the :cmul() member function (element-wise multiplication).
th> torch.Tensor({0,1,1,0,0,1,0}):cmul(torch.Tensor({0,1,1,1,0,1,1}))
0
1
1
0
0
1
0
To compare two tensors element-wise you can use :eq():
th> torch.Tensor({0,1,1,0,0,1,0}):eq(torch.Tensor({0,1,1,1,0,1,1}))
1
1
1
0
1
1
0

plotting variables of procrustes analysis in r?

I have performed non-metric multidimensional scaling (NMDS) on two data frames, each containing different variables but for the same sites. I am using the vegan package:
> head (ResponsesS3)
R1_S3 R10_S3 R11_S3 R12_S3 R2_S3 R3_S3 R4_S3 R6_S3 R7_S3 R8_S3 R9_S3
4 0 0 0 0 0 1 0 0 0 0 0
5 0 0 0 0 0 1 0 0 0 0 0
7 1 0 0 1 0 0 0 0 0 0 0
12 0 0 0 0 0 1 0 0 0 0 0
14 2 2 0 0 0 0 2 0 0 0 0
16 0 0 1 0 0 0 0 0 0 1 0
> head (EnvtS3)
Dep_Mark Dep_Work Dep_Ext Use_For Use_Fish Use_Ag Div_Prod
4 0.06222836 1.0852315 0.8367309 1.1415929 1.644670 0.1006964 0.566474
5 0.25946808 1.3342266 0.0000000 1.7123894 0.822335 0.0000000 0.283237
7 2.20668862 0.0000000 0.8769881 0.4280973 0.822335 0.5244603 0.849711
12 2.26323697 0.0000000 0.8090991 1.1415929 0.000000 1.4957609 1.416185
14 1.65107675 0.5195901 0.2921132 0.5707965 0.822335 1.7873609 0.849711
16 1.82230225 0.4760163 0.1915366 2.2831858 0.000000 1.6614904 0.849711
> ResponsesS3.mds = metaMDS (ResponsesS3, k =2, trymax = 100)
> EnvtS3.mds = metaMDS (EnvtS3, k =2, trymax = 100)
I fit the results using a procrustean superimposition
> pro.ResponsesS3.EnvtS3.mds <- procrustes(ResponsesS3.mds,EnvtS3.mds)
I am most interested in understanding how the variables from each dataset fit together. I would like to use the plot() function to return a graph of the variables from ResponsesS3 and from EnvtS3, rather than the sites (which is what the plot function returns by default).
Is this possible?
No, this is not possible. The problem you'll find you have is that there will be different numbers of variables in the two datasets which causes the procrustes() method to fail if you try procrustes(..., scores = "species").
Even if you fit with procrustes(..., score = "sites") (the default), who do you propose to draw the plot if we could extract the species information? The current plot joins rows from one matric with the rows of other; this works in the default setting because the datasets are assumed to be measurements on the same locations/sites. But this is not possible with species/variables. More fundamentally, how should we pair up species with environmental variables?
Finally, you are trying to look at how the variables compare yet have used a method that essentially throws this information away once dissimilarities are computed.
I would look at the method of coinertia analysis, of which there is a crude interface in my cocorresp package and a fuller one in the ade4 package. If you find yourself wanting to compare two sets of species data, try cocorrespondence analysis, which cocorresp fits.
Like Gav said, the points must match each other one to one for Procrustes rotation. However, once you have a Procrustes rotation, you can naturally apply it to other matrices with the same number of columns. The number of columns is crucial: If you have 2-dim NMDS, your variables also must be mapped into these 2 dim. Function metaMDS() will get you such column scores corresponding to your ordination of row scores, but I don't know how adequate these are in your case. The easiest way to rotate those scores in vegan is to use predict method with newdata. Continuing with your example:
predict(pro.ResponsesS3.EnvtS3.mds, newdata=scores(EnvtS3.mds, "species"))
This will only rotate your column scores ("species") similarly as is rotated your row scores.
We do not know what you try to achieve, and indeed there may be better ways to achieve your goal (check Gavin's answer for a starter). However, this will do the rotation.

do until loop in R

at the end of my wits, so sorry this is the wrong place, or done incorrectly. first time asking here. i am new to R, with very little programming experience (a pascal class in college, and was very good at macromedia lingo way back - so, not that afraid of code).
to keep things short and simple, i think best to just show you what i have, and what i would like. i have spent hours upon hours searching and trying for a solution.
an example of what i have (it is an xts object called "signals", and indexed by days (left out here to make the example simple):
open close position
0 0 0
1 0 0
0 0 0
0 0 0
0 1 0
0 0 0
and what i would like to happen:
open close position
0 0 0
1 0 1
0 0 1
0 0 1
0 1 1
0 0 0
basically, when "open" is true, repeat 1s in "position" until "close" is true. amazingly simple, i think, but somehow i can't make it work. here one example of where i got that i thought was maybe close, but it gets stuck in an endless loop:
for (i in 1:nrow(signals)) {
if (signals[i,"open"]==1) next
while (signals[i,"close"] == 0) {
signals[i,"position"] <- 1 }
}
thank you!
EDIT - i left out an important qualifier. there are times where the first true statement in "close" will come before the first true statement in "open." however, now that i wrote that out here, i suppose it is easier to just 'clean' the close column somehow, so there are no 1s in it prior to the point of the first 1 in the open column.
however, if someone has an idea how to do it all, feel free to add additional information. thanks!
You don't have to use loops for this:
open <- c(0,1,0,0,0,0)
close <- c(0,0,0,0,1,0)
position <- cumsum(open-close)
position
[1] 0 1 1 1 0 0
Note this closes immediately, if you want to on the line after you get a close signal, use:
cumsum(open-c(0,close[-length(close)]))
[1] 0 1 1 1 1 0
The reason your while statment never ends is that you have nothing to modify what is being tested, that is i doesn't get incremented.

Resources