Getting 2 Scatterplots and Histograms from a set of Data - r

so I have the following set of Data
> sleep
extra group ID
1 0.7 1 1
2 -1.6 1 2
3 -0.2 1 3
4 -1.2 1 4
5 -0.1 1 5
6 3.4 1 6
7 3.7 1 7
8 0.8 1 8
9 0.0 1 9
10 2.0 1 10
11 1.9 2 1
12 0.8 2 2
13 1.1 2 3
14 0.1 2 4
15 -0.1 2 5
16 4.4 2 6
17 5.5 2 7
18 1.6 2 8
19 4.6 2 9
20 3.4 2 10
My Task is to generate two Scatterplot that shows the effects of Drug 1 and 2(group) also two Histog, I've been using different things but seriously not clue and I cannot use ggplot as I'm not able to instal anything on my computer labs! Please help!.

Something like this, maybe:
pdf("my_plots.pdf")
for (g in unique(sleep$group)) {
with(sleep[sleep$group==g,], plot(ID, extra, main=paste0("Group = ",g)))
hist(sleep$extra[sleep$group==g], main=paste0("Group = ",g))
}
dev.off()
Per #rawr's comment, you can also have two or four plots on a single page by adding par(mfrow=c(1,2)) or par(mfrow=c(2,2)) before running the code above.

Related

random_walk function from iGraph in R does not stop at absorbing states

I'm trying to emulate a random walk/ Markov chain using R. As you can see I've set up a transition matrix, and then I'm trying to run a random walker on this. The thing here is that when the random walker meets an absorbing state (like 2 and 5) does not stop but continue running.
Am I using the wrong functions for something like this or there is somewhere else the problem? Actually what I want to achieve is to print out al the vertices that the walker visited.
library(igraph)
# Produce a transition matrix.
tm <- read.table(row.names=1, header=FALSE, text="
1 0.2 0.3 0.1 0.2 0.1 0.1
6 0.3 0.2 0.4 0.1 0 0
3 0 0.2 0.4 0.1 0.2 0.1
4 0.2 0.1 0.2 0.3 0.1 0.1
5 0 0 0 0 1 0
2 0 0 0 0 0 1")
tm<-as.matrix(tm)
row.names(tm) <- c(1,6,3,4,5,2)
colnames(tm) <- c(1,6,3,4,5,2)
g1 <- graph.adjacency(tm, mode="undirected", weighted=TRUE)
random_walk( graph = g1, start = '4', steps = 100, stuck = "error" )
An output example :
[1] 4 5 3 4 4 4 3 3 2 2 4 4 2 2 3 4 4 5 5 3 5 5 3 6 3 3 3 4 4 4 6 4 4 4 4 2 2 4 3 6 3 2 2 2 4 1
[47] 4 3 4 1 1 4 2 3 6 6 6 6 4 3 6 6 6 3 5 5 3 5 5 5 3 1 1 3 2 4 4 2 1 1 1 2 3 1 2 1 1 2 2 1 5 3
[93] 5 4 4 2 4 3 4 4
And as you can see it doesn't stop, neither at 2 nor at 5.
If you think these states are absorbing, then you are interpreting your adjacency graph as directed, not undirected. Use mode="directed". Then when you plot the graph, you will see the loops better indicate where you can move (notice how there are no longer any lines leading "out" of node 2).

How to find what objects get plotted in a region in R? [duplicate]

How can I subset data with logical conditions.
Assume that I have data as below. I would like to subset data set with first condition that all animals having FCR record, then I would like to take all animals in same pen with these animals in new data set.
animal Feed Litter Pen
1 0.2 5 3
2 NA 5 3
3 0.2 5 3
4 0.2 6 4
5 0.3 5 4
6 0.3 4 4
7 0.3 5 3
8 0.3 5 3
9 NA 5 5
10 NA 3 5
11 NA 3 3
12 NA 3 5
13 0.4 7 3
14 0.4 7 3
15 NA 7 5
I'm assuming that "FCR record" (in your question) relates to "Feed". Then, if I understand the question correctly, you can do this:
split(df[complete.cases(df),], df[complete.cases(df), 4])
# $`3`
# animal Feed Litter Pen
# 1 1 0.2 5 3
# 3 3 0.2 5 3
# 7 7 0.3 5 3
# 8 8 0.3 5 3
# 13 13 0.4 7 3
# 14 14 0.4 7 3
#
# $`4`
# animal Feed Litter Pen
# 4 4 0.2 6 4
# 5 5 0.3 5 4
# 6 6 0.3 4 4
In the above, complete.cases drops any of the incomplete observations. If you needed to match the argument on a specific variable, you can use something like df[!is.na(df$Feed), ] instead of complete.cases. Then, split creates a list of data.frames split by Pen.
# all animals with Feed data
df[!is.na(df$Feed), ]
# all animals from pens with at least one animal with feed data in the pen
df[ave(!is.na(df$Feed), df$Pen, FUN = any), ]

How to get correct ticklabs in a 3d-scatterplot in R?

Please see this example. Look at y axis. The data there has only two levels: 1 and 2. But in the plot 6 tickmarks drawn on that axis. How could I fix that. The x axis has the same problem.
The data
extra group ID
1 0.7 1 1
2 -1.6 1 2
3 -0.2 1 3
4 -1.2 1 4
5 -0.1 1 5
6 3.4 1 6
7 3.7 1 7
8 0.8 1 8
9 0.0 1 9
10 2.0 1 10
11 1.9 2 1
12 0.8 2 2
13 1.1 2 3
14 0.1 2 4
15 -0.1 2 5
16 4.4 2 6
17 5.5 2 7
18 1.6 2 8
19 4.6 2 9
20 3.4 2 10
The script
require('mise')
require('scatterplot3d')
mise() # clear the workspace
# example data
print(sleep)
# plot it
scatterplot3d(x=sleep$ID,
x.ticklabs=levels(sleep$ID),
y=sleep$group,
y.ticklabs=levels(sleep$group),
z=sleep$extra)
The result
How about this:
scatterplot3d(x=sleep$ID, y=sleep$extra, z=sleep$group, lab.z = c(1, 2))

Dividing data and putting into two boxplot

> sleep
extra group ID
1 0.7 1 1
2 -1.6 1 2
3 -0.2 1 3
4 -1.2 1 4
5 -0.1 1 5
6 3.4 1 6
7 3.7 1 7
8 0.8 1 8
9 0.0 1 9
10 2.0 1 10
11 1.9 2 1
12 0.8 2 2
13 1.1 2 3
14 0.1 2 4
15 -0.1 2 5
16 4.4 2 6
17 5.5 2 7
18 1.6 2 8
19 4.6 2 9
20 3.4 2 10
I have this set of Data and Im supposed to Divide it by the effects that GROUP have on different people and put it into two different boxplot but as you can see theres group 1 and group 2 and they are on the same data which is group so I dont know how to divede the data into group 1 and group 2 can u help me with this?
You don't need to divide the data to put it into a boxplot:
boxplot(extra~group,data=sleep)
You can explore the different options available by using ?boxplot.
Some people like to use the ggplot2 package:
library(ggplot2)
ggplot(sleep,aes(x=group,y=extra,group=group))+geom_boxplot()
Others prefer lattice:
bwplot(group~extra,data=sleep)
This is a good dataset to use ggplot2 with.
library(ggplot2)
ggplot(sleep, aes(x=factor(group), y=extra)) + geom_boxplot()

R - subset data if conditions

How can I subset data with logical conditions.
Assume that I have data as below. I would like to subset data set with first condition that all animals having FCR record, then I would like to take all animals in same pen with these animals in new data set.
animal Feed Litter Pen
1 0.2 5 3
2 NA 5 3
3 0.2 5 3
4 0.2 6 4
5 0.3 5 4
6 0.3 4 4
7 0.3 5 3
8 0.3 5 3
9 NA 5 5
10 NA 3 5
11 NA 3 3
12 NA 3 5
13 0.4 7 3
14 0.4 7 3
15 NA 7 5
I'm assuming that "FCR record" (in your question) relates to "Feed". Then, if I understand the question correctly, you can do this:
split(df[complete.cases(df),], df[complete.cases(df), 4])
# $`3`
# animal Feed Litter Pen
# 1 1 0.2 5 3
# 3 3 0.2 5 3
# 7 7 0.3 5 3
# 8 8 0.3 5 3
# 13 13 0.4 7 3
# 14 14 0.4 7 3
#
# $`4`
# animal Feed Litter Pen
# 4 4 0.2 6 4
# 5 5 0.3 5 4
# 6 6 0.3 4 4
In the above, complete.cases drops any of the incomplete observations. If you needed to match the argument on a specific variable, you can use something like df[!is.na(df$Feed), ] instead of complete.cases. Then, split creates a list of data.frames split by Pen.
# all animals with Feed data
df[!is.na(df$Feed), ]
# all animals from pens with at least one animal with feed data in the pen
df[ave(!is.na(df$Feed), df$Pen, FUN = any), ]

Resources