How to swap arrays in Maxima - swap

I'm having problems in swapping arrays
array(a,1,4);
array(c,1,4);
I tried this:
[a,c]: [c,a]
this:
[a[1,1],c[1,1]]: [c[1,1],a[1,1]],
[a[1,2],c[1,2]]: [c[1,2],a[1,2]],
[a[1,3],c[1,3]]: [c[1,3],a[1,3]]
and this:
f(t1,t2):= block(for i:1 while i<=3 do(
t[1,i]: t1[1,i],
t1[1,i]: t2[1,i],
t2[1,i]: t[1,i]
));
but it won't work.
I appreciate any help bros.

If you don't mind copying data
(%i1) array(a, 4, 1)$
(%i2) fillarray(a, makelist(i, i, 10))$
(%i3) listarray(a);
(%o3) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
(%i4) array(b, 4, 1)$
(%i5) fillarray(b, makelist(10*i, i, 10))$
(%i6) listarray(b);
(%o6) [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
(%i7) array(c, 4, 1)$
(%i8) fillarray(c, a)$
(%i9) fillarray(a, b)$
(%i10) fillarray(b, c)$
(%i11) remarray(c)$
(%i12) listarray(a);
(%o12) [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
(%i13) listarray(b);
(%o13) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Related

Zelen Exact Test - Trying to use a k 2x2 in the function zelen.test()

I am trying to use the zelen.test function on the package NSM3. I am having difficulty reading the data into the function.
You can recreate my data using
data <- c(4, 2, 3, 3, 8, 3, 4, 7, 0, 7, 1, 1, 12, 13,
74, 74, 77, 85, 31, 37, 11, 7, 18, 18, 96, 97, 48, 40)
events <- matrix(data, ncol = 2)
The documentation on CRAN states that zelen.test(z, example = F, r = 3) where z is an array of k 2 x 2 matrix, example is set to FALSE because it returns a p-value for an example I cannot access, and r is the number of decimals the users wants returned in the p-value.
I've tried:
zelen.test(events, r = 4)
I thought it may want the study number and the trial data, so I tried this:
studies <- c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7)
data <- c(4, 2, 3, 3, 8, 3, 4, 7, 0, 7, 1, 1, 12, 13,
74, 74, 77, 85, 31, 37, 11, 7, 18, 18, 96, 97, 48, 40)
events <- matrix(cbind(studies, events), ncol = 3)
zelen.test(events, r = 4)
but it continues to return and error stating
"Error in z[1, 1, ] : incorrect number of dimensions" for both cases I tried above.
Any help would be greatly appreciated!
If we check the source code by typing zelen.test on the console, if the example = TRUE, it is constructing a 3D array
...
if (example)
z <- array(c(2, 1, 2, 5, 1, 5, 4, 1), dim = c(2, 2, 2))
...
The input z dim is also specified in the documentation of ?zelen.test
z - data as an array of k 2x2 matrices. Small data sets only!
So, we may need to construct an array of dimensions 3
library(NSM3)
z1 <- array(c(4, 2, 3, 3, 8, 3, 4, 7), c(2, 2, 2))
zelen.test(z1, r = 4)
# Zelen's test:
# P = 1
Or with 3rd dimension of length 3
z1 <- array( c(4, 2, 3, 3, 8, 3, 4, 7, 0, 7, 1, 1), c(2, 2, 3))
zelen.test(z1, r = 4)
# Zelen's test:
#P = 0.1238

Create a graph from data frame with a layout base on attribute

I create a graph from a data frame. And I would like the vertices to be arranged and moved apart according to the hamming value that is contained in data.rw$Hamming.
I would like some help
data.rw <- structure(list(g1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6,
6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10,
11, 11, 12), g2 = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 5, 6, 7, 8, 9, 10, 11, 12, 13, 6, 7, 8, 9, 10, 11, 12, 13,
7, 8, 9, 10, 11, 12, 13, 8, 9, 10, 11, 12, 13, 9, 10, 11, 12,
13, 10, 11, 12, 13, 11, 12, 13, 12, 13, 13), Hamming = c(116,
86, 101, 92, 84, 78, 83, 102, 87, 100, 96, 97, 90, 111, 98, 90,
92, 87, 114, 95, 108, 104, 109, 85, 74, 68, 60, 67, 84, 71, 84,
78, 79, 83, 85, 79, 78, 101, 90, 101, 91, 92, 72, 66, 67, 92,
77, 90, 82, 83, 62, 59, 88, 71, 86, 78, 81, 59, 78, 63, 74, 68,
73, 83, 60, 77, 75, 72, 89, 100, 94, 97, 79, 75, 82, 90, 93,
91)), row.names = c(NA, -78L), class = "data.frame")
set.seed(1234)
vertice.df <- unique(c(data.rw$name1,data.rw$name2))
g <- graph_from_data_frame(d = data.rw, vertices = vertice.df, directed = F)
plot(g)
I recommend a distance-based layout for this task, multidimensional scaling comes to mind:
m <- get.adjacency(g, attr = "Hamming", sparse = F)
# optionally: m <- dist(m)
l <- layout_with_mds(g, dist = m, dim = 2)
First extract the weighted adjacency matrix from the graph and feed it into the layout function (dist = m). This returns a 2-dimensional matrix l (dim = 2) that you can use as layout for the position of the nodes.
plot(g, layout = l)
Have a look at ?cmdscale if you are interested in MDS and specifically the eig parameter to later assess the goodness-of-fit. Chances are that two dimensions are not enough to adequately reflect the distances between nodes. But that's for you to decide.

Specialised Boxplot: Plotting Lines to the Error Bars to Highlight the Data Range in R

Overview
I have a data frame called ANOVA.Dataframe.1 (see below) containing the dependent variable called 'Canopy_Index', and the independent variable called 'Urbanisation_index".
My aim is to produce a boxplot (exactly the same as the desired result below) for Canopy Cover (%) for each category of the Urbanisation Index with plotted lines pointing towards both the bottom and top of the error bars to highlight the data range.
I have searched intensively in order to find the code to produce the desired boxplot this (please see the desired result), but I was unsuccessful, and I'm also unsure if these boxplots have a specialised name.
Perhaps this can be achieved in either ggplot or Base R
If anyone can help, I would be deeply appreciative.
Desired Result ( Reference)
I can produce an ordinary boxplot with the R-code below, but I cannot figure out how to implement the lines pointing towards the ends of the error bars.
R-code
Boxplot.obs1.Canopy.Urban<-boxplot(ANOVA.Dataframe.1$Canopy_Index~ANOVA.Dataframe.1$Urbanisation_index,
main="Mean Canopy Index (%) for Categories of the Urbansiation Index",
xlab="Urbanisation Index",
ylab="Canopy Index (%)")
Boxplot produced from R-code
Data frame 1
structure(list(Urbanisation_index = c(2, 2, 4, 4, 3, 3, 4, 4,
4, 2, 4, 3, 4, 4, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
2, 2, 2, 4, 4, 3, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 4, 4, 4,
4, 4, 4, 4), Canopy_Index = c(65, 75, 55, 85, 85, 85, 95, 85,
85, 45, 65, 75, 75, 65, 35, 75, 65, 85, 65, 95, 75, 75, 75, 65,
75, 65, 75, 95, 95, 85, 85, 85, 75, 75, 65, 85, 75, 65, 55, 95,
95, 95, 95, 45, 55, 35, 55, 65, 95, 95, 45, 65, 45, 55)), row.names = c(NA,
-54L), class = "data.frame")
Dataframe 2
structure(list(Urbanisation_index = c(2, 2, 4, 4, 3, 3, 4, 4,
4, 3, 4, 4, 4, 4, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
2, 2, 2, 4, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 4, 4, 4, 4, 4, 4, 4
), Canopy_Index = c(5, 45, 5, 5, 5, 5, 45, 45, 55, 15, 35, 45,
5, 5, 5, 5, 5, 5, 35, 15, 15, 25, 25, 5, 5, 5, 5, 5, 5, 15, 25,
15, 35, 25, 45, 5, 25, 5, 5, 5, 5, 55, 55, 15, 5, 25, 15, 15,
15, 15)), row.names = c(NA, -50L), class = "data.frame")
Alice, is this what you are looking for?
You can do everything with ggplot2, but for non standard things you have to play with it for a while. My code:
library(tidyverse)
library(wrapr)
df %.>%
ggplot(data = ., aes(
x = Urbanisation_index,
y = Canopy_Index,
group = Urbanisation_index
)) +
stat_boxplot(
geom = 'errorbar',
width = .25
) +
geom_boxplot() +
geom_line(
data = group_by(., Urbanisation_index) %>%
summarise(
bot = min(Canopy_Index),
top = max(Canopy_Index)
) %>%
gather(pos, val, bot:top) %>%
select(
x = Urbanisation_index,
y = val
) %>%
mutate(gr = row_number()) %>%
bind_rows(
tibble(
x = 0,
y = max(.$y) * 1.15,
gr = 1:8
)
),
aes(
x = x,
y = y,
group = gr
)) +
theme_light() +
theme(panel.grid = element_blank()) +
coord_cartesian(
xlim = c(min(.$Urbanisation_index) - .5, max(.$Urbanisation_index) + .5),
ylim = c(min(.$Canopy_Index) * .95, max(.$Canopy_Index) * 1.05)
) +
ylab('Company Index (%)') +
xlab('Urbanisation Index')

Subset and remove rows from a dataset

I want to exclude some rows from my dataset while I also subset it. Something like I wrote below.
a<-c(2, 4, 6, 6, 8, 10, 12, 13, 14)
c<-c(2, 2, 2, 2, 2, 2, 4, 4,4)
d<-c(10, 10, 10, 30, 30, 30, 50, 50, 50)
ID<-rep(c("no","bo", "fo"), each=3)
mydata<-data.frame(ID, a, c, d)
gg.df <- melt(mydata, id="ID", variable.name="variable")
gg.df[gg.df$variable=="a"& gg.df$ID==-"fo", ]

How can I calculate the mean of the top 4 observations in my column?

How can I calculate the mean of the top 4 observations in my column?
c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)
For instance, in the above I would have (50+60+50+60)/4 = 55. I only know how to use the quantile, but it does not work for this.
Any ideas?
Since you're interested in only the top 4 items, you can use partial sort instead of full sort. If your vector is huge, you might save quite some time:
x <- c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)
idx <- seq(length(x)-3, length(x))
mean(sort(x, partial=idx)[idx])
# [1] 55
Try this:
vec <- c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)
mean(sort(vec, decreasing=TRUE)[1:4])
gives
[1] 55
Maybe something like this:
v <- c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)
mean(head(sort(v,decreasing=T),4))
First, you sort your vector so that the largest values are in the beginning. Then with head you take the 4 first values in that vector, subsequently taking the mean value of that.
To be different! Also, please try to do some research on your own before posting.
x <- c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)
mean(tail(sort(x), 4))
Just to show that you can use quantile in this exercise:
mean(quantile(x,1-(0:3)/length(x),type=1))
#[1] 55
However, the other answers are clearly more efficient.
You could use the order function. Order by -x to give the values in descending order, and just average the first 4:
x <- c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)
mean(x[order(-x)][1:4])
[1] 55

Resources