Anova with repeated replicated design - r

How can the analysis of repeated replicated design given on this page ( https://stats.stackexchange.com/questions/115135/repeated-measures-anova-with-replicated-measurements ) be done in R? I can perform ANOVA using aov() but I have some doubts as to the Error term there.
The data is as follows:
mydf = structure(list(User = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L),
Mode = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), Trial1Time = c(20L,
5L, 40L, 10L, 15L, 30L, 13L, 11L, 35L), Trial2Time = c(30L,
7L, 25L, 20L, 17L, 35L, 26L, 11L, 38L)), .Names = c("User",
"Mode", "Trial1Time", "Trial2Time"), class = "data.frame", row.names = c(NA,
-9L))

Related

geom_smooth only adding lines to my figure legend, not the plot itself

I want to add regression lines to my graph. geom_smooth only adds lines to my figure legend however.
Is the current code I've tried (geom_smooth has the exact same result) along with various attempts at tinkering myself. DGRP.Line & Diet is a factor while Weighted.average is an integer.
RENA <- read.csv('RENA.csv')
RENAVG <- aggregate(Weighted.average~Diet+DGRP.Line, data = RENA, FUN = sum)
ggplot(RENAVG, aes(x=DGRP.Line, y=Weighted.average, colour=Diet))+
geom_point()+
stat_smooth(method='lm')
I'm not sure if the failure to properly regress is a consequence of DGRP.Line being a factor or not. But I'd expect geom_smooth to just form regression lines from my .csv file anyway (RENAVG)
On another attempt using the main RENA.CSV I get this error
"mapping must be created by aes()"
but I'm not sure if that's relevant to the RENAVG I made on R.
My graph is included below. As you can see the figure legend is lined, but no regression lines are on the actual data set.
Edit:
I tried converting my original ggplot (excluding smooth) to its own 'RENAVGPLOT'. Then added smooth in as RENAVGPLOT _ Geom_Smooth, resulting in: Error: Don't know how to add RENAVGPLOT to a plot
dput(RENAVG)
structure(list(Diet = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Control",
"Rena"), class = "factor"), DGRP.Line = structure(c(1L, 1L, 3L,
3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L,
11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L,
17L, 18L, 18L, 19L, 19L, 20L, 20L), .Label = c("105a", "105b",
"348", "354", "362a", "362b", "391a", "391b", "392", "397", "405",
"486a", "486b", "712", "721", "737", "757a", "757b", "853", "879"
), class = "factor"), Weighted.average = c(3.618181818, 7.516666667,
7.5, 10.464285714, 5.830882353, 7.0625, 6.411392405, 7.413953488,
6.079053054, 7.0375, 6.373640273, 10.406521739, 6.948020792,
9.851458886, 9.176727909, 10.164712153, 6.23826291, 11.023310023,
7.908730159, 9.537815126, 5.314323607, 6.655822854, 5.669226044,
7.818181818, 4.761481935, 9.468873129, 6.577764637, 12.170588235,
5.742087177, 10.529411765, 8.891608391, 2, 11.036572623, 3, 9.739878543,
9.782051282, 7.741384687, 8.739583333)), row.names = c(NA, -38L
), class = "data.frame")
>
Example of what I'd like
mtcars
ggplot(mtcars, aes(x=mpg, y=wt, colour=cyl)) +
geom_point()+
geom_smooth()

Why does ggtern distort data

I can't work out why my data points in the ternary diagram appear distorted, particularly visible in Fe02 scale where none of the values approaching 50% seem to be plotting correctly. Does ggtern require some data transformation or am I missing something?
The dataset:
KiDaSm<-structure(list(Site = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Dakawa", "Fukuchani",
"Kilwa", "Mkokotoni", "Tumbe Chwaka", "Unguja Ukuu"), class = "factor"),
Sample = structure(c(7L, 8L, 9L, 10L, 11L, 14L, 15L, 16L,
17L, 19L, 20L, 21L, 23L, 24L, 25L, 26L), .Label = c("EB005",
"EB008", "EB009", "EB017", "EB018", "EB023", "EB028", "EB030",
"EB033", "EB034", "EB035", "EB036", "EB037", "EB038", "EB040",
"EBDAK002", "EBDAK006", "EBDAK007", "EBDAK009", "EBDAK012",
"EBDAK014", "EBDAK015", "EBDAK017", "EBDAK020", "EBDAK021",
"EBDAK022", "FKCH002", "FKCH003", "FKCH005", "FKCH006", "FKCH008",
"FKCH009", "FKCH010", "FKCH012", "FKCH014", "FKCH015", "FKCH016",
"FKCH017", "FKCH018", "FKCH019", "FKCH023", "MKK002", "MKK003",
"MKK007", "MKK009", "MKK011", "MKK013", "MKK014", "MKK017",
"MKK018", "MKK020", "MKK06", "TBCH001", "TBCH002", "TBCH003",
"TBCH005", "TBCH007", "TBCH008", "TBCH009", "TBCH010", "TBCH011",
"TBCH014", "TBCH017", "TBCH018", "TBCH021", "TBCH022", "UU001",
"UU003", "UU004", "UU005", "UU007", "UU008", "UU010", "UU011",
"UU012", "UU014", "UU018", "UU020", "UU022", "UU023", "UU026",
"UU031", "UU033"), class = "factor"), ID = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("ND", "Smelting", "Smithing"), class = "factor"),
Iron = c(52.2866002788889, 57.437955161, 55.880450631, 50.213473286,
53.068958017, 55.776340727, 56.764639409, 61.37738424, 75.741474131,
75.459980082, 69.785922113, 76.298245515, 75.860464737, 77.221978734,
76.602317775, 67.582636787), Aluminium = c(8.07348620588889,
6.9369729006, 6.4314347298, 7.7061493869, 7.3254949831, 7.2108549156,
7.2113019865, 8.2022565362, 4.570137602, 4.3668232665, 5.8538177888,
4.5660791632, 4.2671637947, 4.727287541, 4.7084385736, 6.0287010895
), Silicon = c(24.6786504477778, 22.516695383, 24.261662172,
26.81463386, 25.558654883, 23.062108874, 23.144722305, 26.480492462,
17.138349267, 16.917779397, 19.620246624, 16.265818105, 17.628059944,
15.696017597, 15.786928218, 22.04500569)), .Names = c("Site",
"Sample", "ID", "Iron", "Aluminium", "Silicon"), row.names = c(NA,
-16L), class = "data.frame")
My code:
library(ggtern)
ggtern(KiDaSm, aes(Iron,Silicon, Aluminium, color=Site, shape=Site )) + geom_point() +
labs(x = expression(FeO[2]), y=expression(SiO[2]), z=expression(Al[2]*O[3])) +
scale_color_manual(values = c("#FFC300", "#FF5733")) +
theme_bw()
Ternary diagram:

Re-assemble dataframe by index

What I'm trying to do is getting a dataframe where the repeated rows in the first column act as an index to copy the corresponding rows of other columns. I know this sound messy, and my inability to accurately state the issue is one of the reasons I'm having so many problems with this.
I'll provide a reproducible example below.
structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L), .Label = c("2016-01", "2016-02", "2016-03", "2016-04",
"2016-05", "2016-06", "2016-07", "2016-08", "2016-09", "2016-10",
"2016-11", "2016-12", "2017-01", "2017-02", "2017-03", "2017-04",
"2017-05"), class = "factor"), Var2 = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), .Label = c("B2B", "B2C", "B2K"), class = "factor"), Freq = c(5L,
13L, 8L, 13L, 36L, 5L, 18L, 1L, 12L, 24L, 22L, 6L, 24L, 15L,
11L, 26L, 1L, 338L, 285L, 291L, 232L, 142L, 42L, 92L, 9L, 46L,
34L, 45L, 35L, 30L, 31L, 36L, 56L, 9L, 0L, 1L, 0L, 0L, 0L, 0L,
7L, 0L, 13L, 0L, 1L, 0L, 0L, 0L, 0L, 2L, 0L)), .Names = c("Var1",
"Var2", "Freq"), class = "data.frame", row.names = c(NA, -51L
))
basically what I want is:
On Var1 no repeated dates
On the row where the date is repeated, take the value of Var2 and Freq and copy them in two new columns to the index of the unique date
This must be done for every distinct level of Var2
Thank you in advance!
I think what your trying to explain is a dcast. Does this end up how you want it?
library(reshape2)
dcast(x,Var1~Var2,value.var="Freq")
A base R option would be
xtabs(Freq~Var1 + Var2, df1)

plot group and category means with group_by

I am new to R and trying to figure out a way to plot means for individual samples as well as group means with ggplot.
I am following this articles on R-bloggers (last paragraph):
https://www.r-bloggers.com/plotting-individual-observations-and-group-means-with-ggplot2/
This is my code:
gd <- meanplot1 %>%
group_by(treatment, value) %>%
summarise(measurement = mean(measurement))
ggplot(meanplot1, aes(x=value, y=measurement, color=treatment)) +
geom_line(aes(group=sample), alpha=0.3) +
geom_line(data=gd, size=3, alpha=0.9) +
theme_bw()
Whilst the sample means are being shown, the group means arenĀ“t. I get the error
geom_path: Each group consists of only one observation. Do you need
to adjust the group aesthetic?
Upon adding group=1, I get a weirdly mixed category mean, but not what I am looking for..
I scrolled through a lot of articles already, but couldnt find an answer - I would be so happy if somebody could help me out here!! :)
My data (meanplot1) is formatted like this:
treatment sample value measurement
1 control, control 1, initial, 20,
2 control, control 1, 26, NA,
3 control, control 1, 26', 28,
12 control, control 2, initial, 22,
13 control control 2, 26, NA,
14 control control 2, 26', 36,
15 control control 2, 28, 45,
67 stressed, stress 1, initial, 37,
68 stressed, stress 1, 26, NA,
69 stressed, stress 1, 26', 17,
78 stressed, stress 2, initial, 36,
79 stressed, stress 2, 26, NA,
80 stressed, stress 2, 26', 25,
I am hoping to see 6 lines, one mean for stress 1, stress 2, control 1 and control 2, and one mean for all treatment=control, and one for all treatment=stressed
output dput(gd):
structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("control", "stressed"), class = "factor"), value = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L), .Label = c("26", "26'", "28", "28'",
"30", "30'", "32", "32'", "34", "34'", "initial"), class = "factor"),
measurement = c(NA, 32.3333333333333, 39.5, 30.3333333333333,
31.8333333333333, 31.8333333333333, NA, 36, 34.6666666666667,
36, 24.6666666666667, NA, 25.3333333333333, 33.3333333333333,
32, 50.1666666666667, 39.1666666666667, NA, 33.5, 24.3333333333333,
27.3333333333333, 36)), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -22L), vars = list(treatment), drop = TRUE, .Names = c("treatment",
"value", "measurement"))
output dput(meanplot1):
structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("control",
"stressed"), class = "factor"), sample = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("control 1",
"control 2", "control 3", "control 4", "control 5", "control 6",
"stress 1", "stress 2", "stress 3", "stress 4", "stress 5", "stress 6"
), class = "factor"), value = structure(c(11L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("26", "26'",
"28", "28'", "30", "30'", "32", "32'", "34", "34'", "initial"
), class = "factor"), measurement = c(20L, NA, 28L, 18L, 17L,
19L, 34L, NA, 23L, 29L, 27L, 22L, NA, 36L, 45L, 31L, 40L, 44L,
NA, 49L, 40L, 39L, 32L, NA, 35L, 57L, 30L, 37L, 29L, NA, 44L,
37L, 46L, 20L, NA, 39L, 27L, 30L, 40L, 25L, NA, 29L, 50L, 30L,
26L, NA, 28L, 45L, 47L, 27L, 35L, NA, 24L, 22L, 35L, 28L, NA,
28L, 45L, 27L, 28L, 24L, NA, 47L, 30L, 39L, 37L, NA, 17L, 29L,
29L, 31L, 29L, NA, 37L, 21L, 27L, 36L, NA, 25L, 41L, 51L, 66L,
50L, NA, 33L, 25L, 22L, 36L, NA, 33L, 45L, 26L, 72L, 59L, NA,
33L, 26L, 25L, 33L, NA, 21L, 33L, 25L, 29L, 21L, NA, 26L, 20L,
16L, 22L, NA, 30L, 27L, 28L, 57L, 41L, NA, 28L, 23L, 17L, 52L,
NA, 26L, 25L, 33L, 46L, 35L, NA, 44L, 31L, 57L)), .Names = c("treatment",
"sample", "value", "measurement"), class = "data.frame", row.names = c(NA,
-132L))
I suppose you are aiming to plot the treatment means.
By default, since you are using a categorical x-axis, the grouping is set to the interaction between x and color. You only want to group by treatment, however. So we'll add the correct grouping to the call.
ggplot(meanplot1, aes(x = value, y = measurement, color=treatment)) +
geom_line(aes(group=sample), alpha=0.3) +
geom_line(aes(group = treatment), gd, size=3, alpha=0.9) +
theme_bw()
Also note that
ggplot(meanplot1, aes(x=value, y=measurement, color=treatment)) +
geom_line(aes(group=sample), alpha=0.3) +
stat_summary(aes(group = treatment), fun.y = mean, geom = 'line', size=3, alpha=0.9) +
theme_bw()
Gives the same plot, without the interruption.

Prediction by neural network in R (nnet package)

I can't get why the testing of dataset is not working in R neural networks (nnet package).
I have two datasets with similar structures - for training (trainset, 17 cases) and prediction (testset, 9 cases). Each dataset has columns: Age, Gender, Height, Weight. In the testing dataset the age is unknown (NaN).
The formula for training is obtained successfully below:
library(nnet)
trainednetwork<-nnet(age~gender+emLength+action5cnt,trainset, size=17)
Anyway, if I try to use test dataset for prediction in the next string of the code,
prediction<-predict(trainednetwork,testset)
I get mistake "No component terms, no attribute". Can anyone help?
The data (obtained with dput() function):
testset:
structure(list(
age = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_),
gender = structure(
c(2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L),
.Label = c("f", "m"),
class = "factor"),
Height= c(9L, 11L, 9L, 11L, 9L, 11L, 9L, 11L, 9L),
Weight= c(1L, 41L, 2L, 1L, 2L, 29L, 12L, 6L, 12L)),
.Names = c("age", "gender", "Height", "Weight"),
class = "data.frame",
row.names = c(NA, 9L))
trainset:
structure(list(
age = c(43L, 35L, 22L, 28L, 20L, 47L, 41L, 23L,
42L, 27L, 22L, 60L, 62L, 47L, 42L, 26L, 54L),
gender = structure(
c(2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L),
.Label = c("f", "m"),
class = "factor"),
Height= c(7L, 9L, 11L, 11L, 11L, 9L, 11L, 9L, 23L, 9L,
9L, 9L, 10L, 7L, 7L, 11L, 7L),
Weight= c(2L, 2L, 9L, 9L, 28L, 8L, 6L, 3L, 1L, 2L, 40L,
1L, 9L, 1L, 7L, 4L, 35L)),
.Names = c("age", "gender", "Height", "Weight"),
class = "data.frame",
row.names = c(NA, 17L))
I think in the R neuralnet package the command to use for prediction is "compute", not predict, which is very confusing. A

Resources