How to compute standard errors for predicted data - r

I am trying to generate standard errors for predicted values. I use the below code to generate the predicted values but it fails to also give the standard errors.
ord6 <- veg$ord1-2
laimod.group = lmer(log(lai+0.000019) ~ ord6*plant_growth_form +
(1|plot.code) +
(1|species.code),
data=veg,
REML=FALSE)
summary(laimod.group)
new.ord6 <- c(-1,0,1,2,3,4,5,6,7)
new.plant_growth_form <- c("fern", "grass", "herb","herbaceous climber",
"herbaceous shrub", "moss", "tree sapling",
"undet", "woody climber", "woody shrub")
newdat <- expand.grid(
ord6=new.ord6,plant_growth_form=new.plant_growth_form)
newdat$pred <- predict(laimod.group,newdat, se.fit=TRUE, re.form=NA)
newdat
comment 1: laimod.group = final model selected after comparison of five models using lmer (package lme4)
comment 2: predictSE.mer requires package AICcmodavg
I did try the below code as an alternative but continue to receive the the following error message: Error in fam.link.mer(mod) : object 'out.link' not found
newdat$pred <- predictSE.mer(laimod.group, newdat, se.fit = TRUE, type = "response",
level = 0, print.matrix = FALSE)
Please see a reproducible subset of my data:
structure(list(plot.code = structure(c(1L, 2L, 3L, 4L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L,
9L, 9L, 10L, 11L, 11L, 11L, 11L, 11L, 12L, 13L, 14L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 16L, 17L, 18L, 19L, 19L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 27L, 28L, 28L, 28L, 28L, 28L, 29L,
29L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 31L, 32L, 34L, 34L, 34L,
34L, 34L, 34L, 34L, 35L, 36L, 36L, 36L, 37L, 38L, 39L, 39L, 39L,
40L, 40L, 33L, 33L), .Label = c("a100f1r", "a100m562r", "a10m562r",
"a1f56r", "a1m5r", "b100f177r", "b100m17r", "b100m5r", "c100f17r",
"c100f1r", "c100f5r", "d100m56r", "d100m5r", "d10f1r", "d10f5r",
"e100m17r(old)", "e100m1r", "e100m5r", "e10f177r", "e10f17r(old)",
"e10f5r(old)", "e1f17r", "e1f5r", "f100m177r", "f10f177r", "f10f17r",
"f1m177r", "f1m56r", "lf1f1r", "lf1f5r", "lf1m1r", "og100f5r",
"og10f1r", "og10m1r", "og10m5r", "op100f562r", "op100m177r",
"op10f1r", "op10f5r", "op10m562r"), class = "factor"), species.code = structure(c(69L,
59L, 67L, 69L, 20L, 44L, 28L, 32L, 31L, 7L, 13L, 63L, 69L, 52L,
69L, 14L, 54L, 57L, 42L, 9L, 62L, 10L, 22L, 69L, 35L, 49L, 38L,
11L, 41L, 39L, 16L, 40L, 69L, 32L, 33L, 41L, 22L, 69L, 43L, 4L,
68L, 48L, 6L, 34L, 53L, 3L, 15L, 30L, 13L, 31L, 66L, 64L, 38L,
46L, 61L, 29L, 61L, 27L, 8L, 41L, 55L, 58L, 23L, 25L, 18L, 45L,
26L, 13L, 65L, 12L, 51L, 50L, 60L, 47L, 17L, 5L, 19L, 61L, 1L,
37L, 13L, 36L, 13L, 2L, 11L, 24L, 44L, 13L, 49L, 56L, 21L), .Label = c("agetri",
"alb214", "annunk", "arimin", "baudip", "beg032", "blurip", "buc009",
"cal079", "calplu", "chrodo", "cishas", "clihir", "cos049", "cycari",
"cypunk", "cyr075", "cyrped1", "dae205", "dalpin", "diapla1",
"dio063", "diosum", "emison", "ery046", "eryborb", "fic119",
"ficmeg", "friacu", "graunk", "indunk", "jactom", "lauunk", "leeind",
"luvsar", "lyccer", "mac068", "melmal", "mergra", "miccra1",
"mikcor", "mitken", "nep127", "nepbis", "paldas", "palunk", "panunk",
"penlax", "poaunk", "pol019", "pop246", "ptecog", "ptesub1",
"rubcle", "ryphul", "scamac", "scl051", "sclsum", "selcup", "selfro",
"spa098", "sphste1", "stitrut", "tet055", "tetdie", "tetdie1",
"tetkor", "xanfla", "zinunk"), class = "factor"), plant_growth_form = structure(c(3L,
6L, 9L, 3L, 7L, 1L, 7L, 4L, 8L, 5L, 5L, 1L, 3L, 7L, 3L, 3L, 9L,
2L, 9L, 7L, 9L, 7L, 7L, 3L, 9L, 2L, 10L, 4L, 4L, 9L, 2L, 7L,
3L, 4L, 7L, 4L, 7L, 3L, 1L, 4L, 7L, 7L, 3L, 10L, 7L, 7L, 1L,
2L, 5L, 8L, 9L, 9L, 10L, 7L, 9L, 9L, 9L, 7L, 7L, 4L, 7L, 2L,
7L, 10L, 3L, 7L, 10L, 5L, 9L, 9L, 7L, 7L, 6L, 7L, 3L, 9L, 9L,
9L, 9L, 7L, 5L, 6L, 5L, 9L, 4L, 3L, 1L, 5L, 2L, 7L, 7L), .Label = c("fern",
"grass", "herb", "herbaceous climber", "herbaceous shrub", "moss",
"tree sapling", "undet", "woody climber", "woody shrub"), class = "factor"),
ord1 = c(9L, 5L, 7L, 9L, 4L, 4L, 5L, 5L, 5L, 2L, 9L, 5L,
4L, 6L, 8L, 6L, 3L, 3L, 5L, 3L, 4L, 5L, 3L, 5L, 3L, 9L, 6L,
4L, 4L, 6L, 2L, 5L, 5L, 9L, 3L, 4L, 3L, 5L, 3L, 4L, 1L, 8L,
1L, 5L, 7L, 6L, 9L, 1L, 9L, 1L, 4L, 4L, 2L, 5L, 2L, 3L, 5L,
1L, 3L, 3L, 3L, 2L, 6L, 5L, 2L, 6L, 5L, 2L, 5L, 3L, 6L, 5L,
6L, 3L, 3L, 4L, 7L, 4L, 6L, 1L, 2L, 2L, 4L, 3L, 3L, 3L, 3L,
4L, 4L, 3L, 3L), lai = c(4.525068022, 0.325399379, 0.229222148,
4.076350538, 0.006889889, 0.003279268, 0.037268428, 0.056032134,
0.013573973, 0.001304667, 0.696949844, 1.256477431, 0.122569437,
0.191398415, 1.606070777, 0.425381508, 0.03013251, 0.00181661,
0.017317993, 0.014455456, 0.102704752, 0.031065374, 0.000923601,
0.453384679, 0.017859983, 7.765697214, 0.127071322, 0.102178413,
0.049099766, 0.427983019, 4.22e-05, 0.229034333, 0.694745347,
0.068069112, 0.218354525, 0.05883256, 0.032252145, 0.304812298,
0.009320025, 0.036424481, 0, 0.326, 0.000201724, 0.286106787,
0.556249444, 0.274764132, 4.21, 0, 0.695663959, 0.000213763,
0.00476907, 0.000205017, 3.77e-05, 0.134661951, 0.005631489,
0.0971, 0.172154618, 5.91e-05, 0.000371101, 0.000145266,
0.013382779, 0.00025348, 0.11016712, 0.0616302, 0.018011524,
0.107619537, 0.189926726, 0.000857257, 0.041252452, 0, 0.00475341,
0.077329281, 0.633865958, 0.038182437, 0.015560589, 0.010375148,
1.515423445, 0.008559863, 0.003636564, 0.000424537, 0.002786085,
0.091458876, 0.014216177, 0.165042816, 0.009187705, 0.00115711,
0.000920496, 0.009072635, 0.001443384, 0.001595447, 0.023263507
)), .Names = c("plot.code", "species.code", "plant_growth_form",
"ord1", "lai"), class = "data.frame", row.names = c(NA, -91L))

Related

R loess regression

I think I missed something in the use of the loess function and I can't understand what i did wrong. I have a data frame in which I store the output (count) of 3 different softwares for 26 different genes on the genomes of different patients. The 3 softwares were each used on the same genome but with different rate of downsampling.
I pooled the results of all the patients by genes. At the end I have a data frame with 4 columns: samplexxx (downsampling rate), software (name of the software I used), gene (the name of the gene) and count (count results given by the software).
My goal is to estimate the downsampling effect (samplexxx) on the count given by the software, and I want to do some regression to be able to compare them with each other.
rate <- c(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90,
95, 100)
my attempts:
datalist <- list()
for (i in 1:22) {
name <- genes[i]
print(name)
mod <- paste("mod_", name)
xfit <- paste("xfit_", name)
df <- paste("df_", name)
mod <- loess(data2[data2$gene == name,]$count ~
data2[data2$gene == name,]$samplexxx)
xfit <- predict(mod, newdata=data2[data2$gene == name,]$samplexxx)
df <- setNames(data.frame(matrix(ncol=4, nrow=60)),
c("down", "software", "gene", "loess"))
df$down <- data2[data2$gene == name,]$samplexxx
df$software <- data2[data2$gene == name,]$software
df$gene <- data2[data2$gene == name,]$gene
df$loess <- xfit
print(xfit)
datalist[[i]] <- df
}
data_loess <- do.call(rbind, datalist)
ggplot(data_loess, aes(x=gene, y=loess, fill=software)) +
geom_boxplot()
and:
mod <- loess(data2$count ~ data$samplexxx)
xfit <- predict(mod, newdata=data2$samplexxx)
for (i in 1:20) {
down <- rate[i]
print(name)
title <- paste("loess_downsampling", down)
out <- paste("loess_downsampling", down, ".pdf", sep="")
pdf(out, width=10)
print(ggplot(data2, aes(x=down, y=loess, fill=software))) +
geom_boxplot() + ggtitle(title))
dev.off()
}
Sample data:
> dput(data2)
structure(list(samplexxx = c(5L, 10L, 15L, 20L, 25L, 30L, 35L,
40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L,
5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L,
70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L,
35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L,
100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L,
65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L,
30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L,
95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L,
60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L,
25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L,
90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L,
55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L,
20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L,
85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L,
50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L,
15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L,
80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L,
45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L,
5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L,
70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L,
35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L,
100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L,
65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L,
30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L,
95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L,
60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L,
25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L,
90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L,
55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L,
20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L,
85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L,
50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L,
15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L,
80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L,
45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L,
5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L,
70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L,
35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L,
100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L,
65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L,
30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L,
95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L,
60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L,
25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L,
90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L,
55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L,
20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L,
85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L,
50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L,
15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L,
80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L,
45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L,
5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L,
70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L,
35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L,
100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L,
65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L,
30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L,
95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L,
60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L,
25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L,
90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L,
55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L,
20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L,
85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L,
50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L,
15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L,
80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L,
45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L,
5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L,
70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L,
35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L,
100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L,
65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L,
30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L,
95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L,
60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L,
25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L,
90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L,
55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L,
20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L,
85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L,
50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L,
15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L,
80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L,
45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L,
5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L,
70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L,
35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L,
100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L,
65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L,
30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L,
95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L,
60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L,
25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L,
90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L,
55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L, 15L,
20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L,
85L, 90L, 95L, 100L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L,
50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 5L, 10L,
15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L,
80L, 85L, 90L, 95L, 100L), software = 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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 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
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,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 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,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
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L), .Label = c("AFF2", "AR", "ATN1", "ATXN1",
"ATXN10", "ATXN2", "ATXN3", "ATXN7", "C9ORF72", "CACNA1A",
"CBL", "CNBP", "CSTB", "DIP2B", "DMPK", "FMR1", "FXN", "HTT",
"JPH3", "NOP56", "PPP2R2B", "TBP"), class = "factor"), count = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 17L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 15L, 15L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, NA, NA, NA, NA, 20L, 34L, 31L, 33L, 34L, 34L, 34L, 34L,
34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, NA, NA, NA, NA, NA,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, NA, NA, NA, NA, NA, 22L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, NA, NA,
NA, NA, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, NA, NA, NA, NA, 6L, 8L, 8L,
8L, 8L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, NA, NA,
NA, NA, 11L, NA, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, NA, NA, NA, 12L, 5L, NA, 12L,
12L, 5L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, NA, NA, NA, NA, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 20L, 20L, 18L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, NA, NA, NA, NA, 27L, 24L,
21L, 14L, 27L, 14L, 21L, 27L, 27L, 14L, 27L, 27L, 27L, 27L,
27L, 27L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 68L, 73L,
78L, 54L, 79L, 76L, 87L, 72L, 62L, 63L, NA, NA, NA, NA, NA,
27L, 27L, 27L, 28L, 27L, 27L, 64L, 27L, 64L, 64L, 27L, 27L,
27L, 27L, 27L, NA, NA, NA, NA, NA, 18L, 20L, 18L, 20L, 20L,
18L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, NA, NA,
NA, NA, NA, 15L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 9L, 7L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, NA, NA, NA, NA, NA, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, NA, NA, NA, NA, NA, 35L, 29L, 35L, 35L, 30L, 35L,
32L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 11L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 20L, 11L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 33L, 33L, 32L, 33L, 33L, 33L, 33L, 33L, 33L, 33L,
33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, NA, 21L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 19L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 19L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 8L, 8L,
7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 11L, NA, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 7L, 15L, 15L, 13L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 27L, 19L, 27L, 27L, 27L,
27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, NA, 76L, 23L, 23L, 23L, 32L, 65L, 32L, 28L, 32L,
28L, 32L, 32L, 23L, 28L, 32L, 28L, 28L, 32L, 84L, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 14L, 18L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 15L,
NA, NA, 15L, NA, 15L, NA, NA, 15L, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 9L, NA, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, NA, 28L, 36L, 36L, NA, 36L, 36L, 36L,
36L, NA, 36L, NA, 36L, 36L, 36L, 36L, 36L, NA, 36L, 36L,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
1L, 8L, 18L, 16L, 15L, 14L, 15L, 16L, 15L, 16L, 14L, 15L,
14L, 14L, 14L, 14L, 16L, 16L, 16L, 16L, 31L, 28L, 31L, 31L,
32L, 32L, 32L, 33L, 31L, 33L, 32L, 31L, 32L, 32L, 32L, 32L,
32L, 32L, 32L, 32L, 7L, 18L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
19L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 5L, 6L, 6L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 12L, 11L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 5L, 7L, 7L, 7L, 7L, 11L, 11L, 7L,
11L, 15L, 15L, 11L, 7L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
1L, 2L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 20L, 17L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 1L, 2L, 1L, 1L,
1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 1L, 15L, 6L, 22L, 13L, 14L, 13L, 14L, 13L, 14L, 14L,
27L, 27L, 14L, 14L, 27L, 14L, 27L, 14L, 27L, NA, 15L, 20L,
20L, 20L, 20L, 40L, 20L, 40L, 20L, 40L, 40L, 40L, 40L, 20L,
40L, 40L, 40L, 40L, 32L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 15L, 14L,
17L, 17L, 17L, 19L, 17L, 13L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 5L, 3L, 1L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 5L, 3L,
1L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 12L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, NA,
2L, 3L, 2L, 29L, 33L, 33L, 35L, 33L, 35L, 35L, 33L, 35L,
35L, 33L, 35L, 35L, 35L, 35L, 35L)), class = "data.frame", row.names = c(NA,
-1320L))
I believe the loess should be done on a split on the "software".
software <- unique(data2$software)
data_loess <- do.call(rbind, lapply(software, \(x) {
X <- subset(data2, software == x)
lo <- loess(count ~ samplexxx, X)
count_pred <- predict(lo, newdata=X)
return(cbind(X, count_pred))
}))
Note: R version 4.1.2 (2021-11-01)
Gives:
head(data_loess[data_loess$samplexxx > 80, ], 10)
# samplexxx software gene count count_pred
# 17 85 EH AFF2 24 22.69004
# 18 90 EH AFF2 24 22.31879
# 19 95 EH AFF2 24 21.83428
# 20 100 EH AFF2 24 21.25618
# 37 85 EH AR 21 22.69004
# 38 90 EH AR 21 22.31879
# 39 95 EH AR 21 21.83428
# 40 100 EH AR 21 21.25618
# 57 85 EH ATN1 NA 22.69004
# 58 90 EH ATN1 NA 22.31879
And here a plot of "count" predictions on "samplexxx".
plot(count_pred ~ samplexxx, data_loess, col=as.numeric(software) + 1,
pch=20, xlab='Downsampling', ylab='Count (LOESS)')
legend('topleft', legend=software, pch=19, col=as.numeric(software) + 1,
horiz=TRUE, cex=.7, title='Software')
Looks interesting, but I'm not sure if it's absolutely right.
In my answer you see something different from for loops, which is probably new to you, however it's the r-ish way and its much shorter to code. The looping job here does lapply().
Anyway, hope this helps.

Undirected network graph calculated by tidygraph shows more degree centrality than should be possible

I have a cleaned data set with 26 nodes. I am placing these 26 nodes in an undirected network graph using tidygraph, where I use the centrality_degree() function to calculate the centrality degree. However, when I graph the resulting network, my highest possible centrality degree is 40, which should not be possible. When I change the graph to directed, this is corrected.
I somewhat confused, as other methods I have used in the past, where I manually calculated the centrality degree, I have never once come across this issue.
Is this regular behaviour, or am I doing something wrong?
Reproducible example:
library(tidygraph)
library(ggraph)
library(tidyverse)
nodes <- structure(list(id = 1:26, label = c("a", "b", "c", "d", "e",
"f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z")), row.names = c(NA, -26L
), class = "data.frame")
edges <- structure(list(from = c(21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 25L, 25L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 17L, 17L, 17L, 17L, 17L),
to = c(1L, 12L, 3L, 16L, 24L, 4L, 10L, 6L, 22L, 2L, 8L, 1L,
12L, 13L, 3L, 18L, 16L, 24L, 5L, 7L, 14L, 4L, 10L, 6L, 9L,
22L, 15L, 2L, 20L, 8L, 21L, 12L, 13L, 3L, 16L, 24L, 5L, 7L,
14L, 4L, 10L, 6L, 22L, 15L, 2L, 8L, 17L, 21L, 1L, 13L, 3L,
16L, 5L, 7L, 14L, 10L, 6L, 9L, 22L, 15L, 2L, 20L, 8L, 17L,
21L, 1L, 3L, 18L, 16L, 5L, 7L, 14L, 4L, 10L, 6L, 25L, 9L,
22L, 15L, 20L, 8L, 17L, 21L, 11L, 1L, 12L, 13L, 18L, 16L,
24L, 5L, 7L, 14L, 4L, 10L, 6L, 25L, 9L, 22L, 15L, 20L, 8L,
17L, 1L, 3L, 10L, 6L, 22L, 20L, 8L, 21L, 11L, 1L, 13L, 3L,
18L, 24L, 7L, 4L, 10L, 6L, 25L, 9L, 22L, 15L, 2L, 20L, 8L,
17L, 21L, 11L, 1L, 12L, 13L, 18L, 16L, 5L, 7L, 14L, 10L,
6L, 25L, 9L, 22L, 15L, 20L, 8L, 17L, 1L, 3L, 18L, 16L, 7L,
14L, 4L, 10L, 6L, 9L, 22L, 15L, 2L, 20L, 8L, 17L, 21L, 11L,
1L, 12L, 13L, 3L, 18L, 16L, 24L, 14L, 4L, 10L, 6L, 25L, 9L,
22L, 15L, 2L, 20L, 8L, 11L, 1L, 3L, 18L, 16L, 7L, 10L, 6L,
9L, 22L, 15L, 2L, 20L, 8L, 17L, 21L, 11L, 1L, 12L, 13L, 3L,
18L, 16L, 24L, 5L, 7L, 14L, 10L, 6L, 25L, 9L, 22L, 15L, 2L,
20L, 8L, 17L, 21L, 11L, 1L, 12L, 13L, 3L, 18L, 16L, 24L,
5L, 7L, 14L, 4L, 6L, 25L, 9L, 22L, 15L, 2L, 20L, 8L, 17L,
21L, 11L, 1L, 12L, 13L, 3L, 18L, 24L, 5L, 7L, 14L, 4L, 10L,
25L, 9L, 22L, 15L, 2L, 20L, 8L, 21L, 1L, 13L, 3L, 18L, 5L,
10L, 6L, 22L, 2L, 20L, 8L, 21L, 1L, 13L, 3L, 18L, 16L, 24L,
4L, 10L, 6L, 22L, 15L, 2L, 20L, 8L, 11L, 1L, 12L, 13L, 3L,
16L, 24L, 5L, 7L, 14L, 4L, 10L, 6L, 25L, 9L, 15L, 2L, 20L,
8L, 17L, 21L, 1L, 12L, 3L, 18L, 16L, 24L, 7L, 10L, 6L, 25L,
9L, 22L, 2L, 20L, 8L, 17L, 21L, 11L, 1L, 12L, 13L, 3L, 18L,
16L, 24L, 5L, 7L, 14L, 4L, 6L, 25L, 9L, 22L, 15L, 20L, 8L,
17L, 21L, 11L, 1L, 3L, 16L, 24L, 7L, 10L, 6L, 22L, 2L, 8L,
21L, 11L, 1L, 12L, 13L, 3L, 18L, 16L, 24L, 14L, 4L, 10L,
6L, 25L, 9L, 22L, 2L, 20L, 7L, 6L, 25L, 22L, 8L), weight = c(3L,
1L, 3L, 2L, 1L, 1L, 5L, 1L, 8L, 2L, 1L, 2L, 3L, 2L, 5L, 1L,
4L, 1L, 4L, 4L, 4L, 1L, 5L, 13L, 3L, 7L, 3L, 2L, 3L, 8L,
1L, 1L, 1L, 15L, 10L, 7L, 2L, 4L, 2L, 5L, 19L, 23L, 6L, 2L,
11L, 7L, 1L, 1L, 2L, 3L, 3L, 5L, 4L, 5L, 4L, 4L, 21L, 2L,
9L, 8L, 1L, 1L, 12L, 1L, 2L, 1L, 3L, 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5L, 6L, 27L, 1L, 1L, 2L, 3L,
4L, 10L, 56L, 3L, 25L, 25L, 7L, 16L, 5L, 29L, 59L, 3L, 3L,
20L, 17L, 5L, 31L, 3L, 6L, 1L, 4L, 7L, 1L, 3L, 1L, 6L, 5L,
13L, 1L, 2L, 9L, 1L, 15L, 2L, 1L, 16L, 4L, 4L, 3L, 1L, 6L,
17L, 10L, 1L, 13L, 63L, 11L, 12L, 1L, 5L, 1L, 2L, 3L)), row.names = c(NA,
-383L), class = c("tbl_df", "tbl", "data.frame"))
routes_tidy <- tbl_graph(nodes=nodes, edges=edges, directed=FALSE) %>% mutate(neighbors = centrality_degree())
# Filtering out 3 nodes out of the graph as they have no connections and zoom the figure way out
ggraph(routes_tidy, layout="graphopt") +
geom_node_point(aes(size=neighbors, filter=(label!="z" & label!="s" & label!="w"))) +
geom_edge_link(aes(width=weight, alpha=weight)) +
scale_edge_width(range=c(0.2, 2)) +
geom_node_text(aes(label=label, fontface="bold", size=neighbors, filter=(label!="z" & label!="s" & label!="w")), repel=TRUE) +
labs(edge_width="N") +
theme_graph()
I'm new to the whole tidygraph thing, stumbled over this question, got confused, and figured it'd be a nice way to get to know stuff. So, I don't know if it's a bug or a feature, but the behaviour is triggered because you have doubled edges:
# Given your edges
edges %>%
filter((from == 1 & to == 2) | from == 2 & to == 1)
# A tibble: 2 x 3
from to weight
<int> <int> <int>
1 1 2 11
2 2 1 3
And those count as 2 connections in the calculation of the degree centrality. One way to remove those double edges is to convert the network to a simple network:
routes_simple <-
routes_tidy %>%
morph(to_simple) %>%
crystallise() %>%
pull(graph) %>%
getElement(1) %>%
activate(nodes) %>%
mutate(neighbors = centrality_degree())
Now the maximum degree is 22 (and the heighest possible, presumably, 25).

Tidying up the text to remove unwanted characters, in output

Assuming that the dataframe is stored as fruit, and is in the following format:
State Fruit Category Fruit Type Gross Value
ACT CitrusFruit Mandarins $4,500,000
ACT CitrusFruit Oranges
NSW PomeFruit Apple $139,130,203.50
NSW Grapes Wine Production $50,000,000
NSW OrchardStoneFruit Avocados $10,031,123
QLD CitrusFruit Oranges
Output from dput(fruit)
structure(list(State = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L,
8L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L,
8L), .Label = c("ACT", "NSW", "NT", "QLD", "SA", "TAS", "VIC",
"WA"), class = "factor"), Fruit.Category = structure(c(6L, 6L,
6L, 8L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L), .Label = c(" Grapes ", " OrchardStoneFruit ", " OtherFruit ",
" PomeFruit ", " CitrusFruit ", " CitrusFruit ", " Grapes ",
" Grapes ", " OrchardStoneFruit ", " OtherFruit ", " PomeFruit "
), class = "factor"), Fruit.Type = structure(c(5L, 8L, 13L, 18L,
31L, 2L, 4L, 6L, 7L, 9L, 14L, 17L, 3L, 11L, 12L, 15L, 1L, 10L,
16L, 13L, 23L, 26L, 13L, 23L, 26L, 13L, 23L, 26L, 13L, 23L, 26L,
13L, 23L, 26L, 13L, 23L, 26L, 13L, 23L, 26L, 18L, 31L, 18L, 31L,
18L, 31L, 18L, 31L, 18L, 31L, 18L, 31L, 18L, 31L, 14L, 17L, 20L,
22L, 24L, 25L, 27L, 14L, 17L, 20L, 22L, 24L, 25L, 27L, 14L, 17L,
20L, 22L, 24L, 25L, 27L, 14L, 17L, 20L, 22L, 24L, 25L, 27L, 14L,
17L, 20L, 22L, 24L, 25L, 27L, 14L, 17L, 20L, 22L, 24L, 25L, 27L,
14L, 17L, 20L, 22L, 24L, 25L, 27L, 15L, 21L, 29L, 30L, 15L, 21L,
29L, 30L, 15L, 21L, 29L, 30L, 15L, 21L, 29L, 30L, 15L, 21L, 29L,
30L, 15L, 21L, 29L, 30L, 15L, 21L, 29L, 30L, 16L, 19L, 28L, 16L,
19L, 28L, 16L, 19L, 28L, 16L, 19L, 28L, 16L, 19L, 28L, 16L, 19L,
28L, 16L, 19L, 28L), .Label = c(" Apples ", " Avocados ",
" Bananas ", " Cherries ", " Mandarins ", " Mangoes ",
" Nectarines ", " Oranges ", " Peaches ", " Pears ",
" Pineapples ", " Strawberries ", " AllOtherCitrusFruit ",
" AllOtherOrchardFruit ", " AllOtherOtherFruit ", " AllOtherPomeFruit ",
" AllOtherStoneFruit ", " AllOtherUses ", " Apples ", " Avocados ",
" Bananas ", " Cherries ", " Mandarins ", " Mangoes ", " Nectarines ",
" Oranges ", " Peaches ", " Pears ", " Pineapples ", " Strawberries ",
" WineProduction "), class = "factor"), Gross.Value = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 60L, 97L, 23L, 104L, 1L, 1L, 56L, 98L, 36L, 101L, 68L,
11L, 1L, 1L, 1L, 91L, 96L, 57L, 99L, 92L, 21L, 71L, 29L, 48L,
1L, 76L, 51L, 46L, 58L, 1L, 34L, 37L, 14L, 22L, 70L, 18L, 59L,
28L, 32L, 41L, 83L, 61L, 69L, 30L, 1L, 1L, 26L, 1L, 1L, 25L,
35L, 19L, 2L, 80L, 9L, 8L, 7L, 102L, 47L, 31L, 1L, 85L, 75L,
1L, 88L, 93L, 52L, 1L, 66L, 50L, 100L, 43L, 89L, 95L, 2L, 82L,
65L, 5L, 24L, 94L, 33L, 64L, 10L, 90L, 78L, 84L, 62L, 3L, 86L,
20L, 73L, 1L, 38L, 67L, 72L, 15L, 63L, 1L, 1L, 39L, 17L, 1L,
1L, 16L, 40L, 1L, 1L, 103L, 79L, 49L, 1L, 44L, 6L, 105L, 53L,
1L, 1L, 1L, 1L, 81L, 54L, 27L, 87L, 13L, 1L, 55L, 106L, 4L, 42L,
12L, 45L, 77L, 74L), .Label = c("", "$0.00", "$1,025,861.63",
"$1,107,476.82", "$1,135,055.74", "$1,148,385.97", "$1,514,089.93",
"$1,539,762.85", "$1,565,234.83", "$10,469,580.98", "$100,622,922.20",
"$106,039,956.40", "$11,648,561.35", "$113,930,475.80", "$114,195,162.80",
"$12,169,338.44", "$12,492,792.64", "$12,843,528.01", "$120,877,197.60",
"$13,245.08", "$13,331,668.11", "$13,981,075.51", "$130,258,416.50",
"$14,203,578.43", "$14,697,408.09", "$15,085,825.24", "$15,196.71",
"$15,246,349.76", "$154,858,589.30", "$168,325.78", "$17,661,100.37",
"$18,278,371.16", "$188,414.59", "$19,896,312.15", "$2,370,402.03",
"$2,557,589.86", "$209,648,663.50", "$21,426,350.11", "$22,482,034.46",
"$23,929,331.35", "$238,668.61", "$249,675,376.10", "$26,669,599.23",
"$27,540,236.71", "$270,903.84", "$3,485,520.14", "$3,520,605.89",
"$3,659,706.68", "$3,829,198.67", "$301,644.66", "$301,976.25",
"$31,133,715.88", "$313,144.86", "$334,363.30", "$35,212,772.81",
"$37,927,507.70", "$38,989,343.33", "$385,858,491.60", "$4,447,813.26",
"$4,549,208.46", "$4,569,373.00", "$4,702.20", "$4,712,329.56",
"$4,995,833.14", "$40,133,037.39", "$40,481.05", "$435,712,531.70",
"$44,434,103.55", "$443,017.10", "$45,665,029.35", "$45,888,545.67",
"$46,638,011.92", "$47,589.51", "$5,793,841.42", "$5,854,982.37",
"$51,534,636.09", "$53,367,548.56", "$53,377,925.45", "$555,799.71",
"$57,522,144.94", "$57,930,562.37", "$58,316,912.75", "$6,170,170.78",
"$6,791,088.95", "$6,824,520.08", "$623,030.52", "$63,493,163.21",
"$664,237.23", "$7,066,407.60", "$7,168,380.92", "$7,364,245.36",
"$7,426,224.28", "$7,894.54", "$70,218,810.35", "$76,591,000.57",
"$8,596,626.45", "$8,713,417.54", "$85,876,834.41", "$873,748.40",
"$9,262,889.69", "$9,731,658.36", "$9,991,440.81", "$91,781,453.44",
"$92,299.72", "$95,677,012.68", "$983,780.33"), class = "factor")), class = "data.frame", row.names = c(NA,
-152L))
I am trying to sum the Gross Value, based on the Fruit Category, and have used the following code for it:
fruit %>%
mutate(Gross.Value = as.numeric(gsub("[^0-9.]", "", as.character(Gross.Value)))) %>%
group_by(Fruit.Category) %>%
summarize(Gross.Value = sum(Gross.Value, na.rm=TRUE))
However, this is resulting in an output that looks a little like this:
A tibble: 11 x 2
Fruit.Category Gross.Value
<fct> <dbl>
1 " Grapes " 0
2 " OrchardStoneFruit " 0
3 " OtherFruit " 0
4 " PomeFruit " 0
5 " CitrusFruit " 501345814.
6 " CitrusFruit " 0
7 " Grapes " 1048709022.
8 " Grapes " 0
9 " OrchardStoneFruit " 679997807.
10 " OtherFruit " 879348015.
11 " PomeFruit " 683012047.
How would I alter the output so that I can remove the quotation marks and any trailing or leading spaces. Essentially, just tidy up the text.
Also, any suggestions on how I would go about to display the output in a descending order (based on total gross value) would be greatly appreciated. The only method I know is to add:
%>% arrange(desc(n))
at the end of the code. However, this does not seem to work for this.
A continuation of your last question :-)
fruit %>%
mutate_if(~is.factor(.) | is.character(.), ~trimws(as.character(.))) %>%
mutate(Gross.Value = as.numeric(gsub("[^0-9.]", "", Gross.Value))) %>%
group_by(Fruit.Category) %>%
summarize(Gross.Value = sum(Gross.Value, na.rm=TRUE)) %>%
arrange(desc(Gross.Value))
# # A tibble: 5 x 2
# Fruit.Category Gross.Value
# <chr> <dbl>
# 1 Grapes 1048709022.
# 2 OtherFruit 879348015.
# 3 PomeFruit 683012047.
# 4 OrchardStoneFruit 679997807.
# 5 CitrusFruit 501345814.
Because we trim the extra whitespace before summarization, we're able to reduce some of the incorrect uniqueness.
The meat of the answer is in that first line of the pipe:
mutate_if(~is.factor(.) | is.character(.), ~trimws(as.character(.))) %>%
The mutate_if says "mutate all columns that meet a specific condition". In this case, I limited it to those columns that are either character or quasi-char factors (since it would not do well to convert already-numeric columns to character).
From there, plan to arrange(desc(Gross.Value)). (Not sure where arrange(desc(n)) came in ...)

How do I sum a column based on another column?

Assuming that the dataframe is stored as fruit, and is in the following format:
State Fruit Category Fruit Type Gross Value
ACT CitrusFruit Mandarins $4,500,000
ACT CitrusFruit Oranges
NSW PomeFruit Apple $139,130,203.50
NSW Grapes Wine Production $50,000,000
NSW OrchardStoneFruit Avocados $10,031,123
QLD CitrusFruit Oranges
How would I sum the gross value, based on the State - while excluding blank values. But at the same time, the gross value of each state should be summed, rather than displayed separately for CitrusFruit, PomeFruit, etc.
I have tried to use the
library(plyr)
counts
method to no avail.
Any help would be greatly appreciated.
EDIT:
I have tried to use the following method:
library(dplyr)
fruit %>%
group_by(State) %>%
summarise(Gross = sum(Gross))
However, I am getting an error that says:
Evaluation Error: 'sum' not meaningful for factors.
EDIT:
Output from dput(fruit)
structure(list(State = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L,
8L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L,
8L), .Label = c("ACT", "NSW", "NT", "QLD", "SA", "TAS", "VIC",
"WA"), class = "factor"), Fruit.Category = structure(c(6L, 6L,
6L, 8L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L), .Label = c(" Grapes ", " OrchardStoneFruit ", " OtherFruit ",
" PomeFruit ", " CitrusFruit ", " CitrusFruit ", " Grapes ",
" Grapes ", " OrchardStoneFruit ", " OtherFruit ", " PomeFruit "
), class = "factor"), Fruit.Type = structure(c(5L, 8L, 13L, 18L,
31L, 2L, 4L, 6L, 7L, 9L, 14L, 17L, 3L, 11L, 12L, 15L, 1L, 10L,
16L, 13L, 23L, 26L, 13L, 23L, 26L, 13L, 23L, 26L, 13L, 23L, 26L,
13L, 23L, 26L, 13L, 23L, 26L, 13L, 23L, 26L, 18L, 31L, 18L, 31L,
18L, 31L, 18L, 31L, 18L, 31L, 18L, 31L, 18L, 31L, 14L, 17L, 20L,
22L, 24L, 25L, 27L, 14L, 17L, 20L, 22L, 24L, 25L, 27L, 14L, 17L,
20L, 22L, 24L, 25L, 27L, 14L, 17L, 20L, 22L, 24L, 25L, 27L, 14L,
17L, 20L, 22L, 24L, 25L, 27L, 14L, 17L, 20L, 22L, 24L, 25L, 27L,
14L, 17L, 20L, 22L, 24L, 25L, 27L, 15L, 21L, 29L, 30L, 15L, 21L,
29L, 30L, 15L, 21L, 29L, 30L, 15L, 21L, 29L, 30L, 15L, 21L, 29L,
30L, 15L, 21L, 29L, 30L, 15L, 21L, 29L, 30L, 16L, 19L, 28L, 16L,
19L, 28L, 16L, 19L, 28L, 16L, 19L, 28L, 16L, 19L, 28L, 16L, 19L,
28L, 16L, 19L, 28L), .Label = c(" Apples ", " Avocados ",
" Bananas ", " Cherries ", " Mandarins ", " Mangoes ",
" Nectarines ", " Oranges ", " Peaches ", " Pears ",
" Pineapples ", " Strawberries ", " AllOtherCitrusFruit ",
" AllOtherOrchardFruit ", " AllOtherOtherFruit ", " AllOtherPomeFruit ",
" AllOtherStoneFruit ", " AllOtherUses ", " Apples ", " Avocados ",
" Bananas ", " Cherries ", " Mandarins ", " Mangoes ", " Nectarines ",
" Oranges ", " Peaches ", " Pears ", " Pineapples ", " Strawberries ",
" WineProduction "), class = "factor"), Gross.Value = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 60L, 97L, 23L, 104L, 1L, 1L, 56L, 98L, 36L, 101L, 68L,
11L, 1L, 1L, 1L, 91L, 96L, 57L, 99L, 92L, 21L, 71L, 29L, 48L,
1L, 76L, 51L, 46L, 58L, 1L, 34L, 37L, 14L, 22L, 70L, 18L, 59L,
28L, 32L, 41L, 83L, 61L, 69L, 30L, 1L, 1L, 26L, 1L, 1L, 25L,
35L, 19L, 2L, 80L, 9L, 8L, 7L, 102L, 47L, 31L, 1L, 85L, 75L,
1L, 88L, 93L, 52L, 1L, 66L, 50L, 100L, 43L, 89L, 95L, 2L, 82L,
65L, 5L, 24L, 94L, 33L, 64L, 10L, 90L, 78L, 84L, 62L, 3L, 86L,
20L, 73L, 1L, 38L, 67L, 72L, 15L, 63L, 1L, 1L, 39L, 17L, 1L,
1L, 16L, 40L, 1L, 1L, 103L, 79L, 49L, 1L, 44L, 6L, 105L, 53L,
1L, 1L, 1L, 1L, 81L, 54L, 27L, 87L, 13L, 1L, 55L, 106L, 4L, 42L,
12L, 45L, 77L, 74L), .Label = c("", "$0.00", "$1,025,861.63",
"$1,107,476.82", "$1,135,055.74", "$1,148,385.97", "$1,514,089.93",
"$1,539,762.85", "$1,565,234.83", "$10,469,580.98", "$100,622,922.20",
"$106,039,956.40", "$11,648,561.35", "$113,930,475.80", "$114,195,162.80",
"$12,169,338.44", "$12,492,792.64", "$12,843,528.01", "$120,877,197.60",
"$13,245.08", "$13,331,668.11", "$13,981,075.51", "$130,258,416.50",
"$14,203,578.43", "$14,697,408.09", "$15,085,825.24", "$15,196.71",
"$15,246,349.76", "$154,858,589.30", "$168,325.78", "$17,661,100.37",
"$18,278,371.16", "$188,414.59", "$19,896,312.15", "$2,370,402.03",
"$2,557,589.86", "$209,648,663.50", "$21,426,350.11", "$22,482,034.46",
"$23,929,331.35", "$238,668.61", "$249,675,376.10", "$26,669,599.23",
"$27,540,236.71", "$270,903.84", "$3,485,520.14", "$3,520,605.89",
"$3,659,706.68", "$3,829,198.67", "$301,644.66", "$301,976.25",
"$31,133,715.88", "$313,144.86", "$334,363.30", "$35,212,772.81",
"$37,927,507.70", "$38,989,343.33", "$385,858,491.60", "$4,447,813.26",
"$4,549,208.46", "$4,569,373.00", "$4,702.20", "$4,712,329.56",
"$4,995,833.14", "$40,133,037.39", "$40,481.05", "$435,712,531.70",
"$44,434,103.55", "$443,017.10", "$45,665,029.35", "$45,888,545.67",
"$46,638,011.92", "$47,589.51", "$5,793,841.42", "$5,854,982.37",
"$51,534,636.09", "$53,367,548.56", "$53,377,925.45", "$555,799.71",
"$57,522,144.94", "$57,930,562.37", "$58,316,912.75", "$6,170,170.78",
"$6,791,088.95", "$6,824,520.08", "$623,030.52", "$63,493,163.21",
"$664,237.23", "$7,066,407.60", "$7,168,380.92", "$7,364,245.36",
"$7,426,224.28", "$7,894.54", "$70,218,810.35", "$76,591,000.57",
"$8,596,626.45", "$8,713,417.54", "$85,876,834.41", "$873,748.40",
"$9,262,889.69", "$9,731,658.36", "$9,991,440.81", "$91,781,453.44",
"$92,299.72", "$95,677,012.68", "$983,780.33"), class = "factor")), class = "data.frame", row.names = c(NA,
-152L))
A couple of problems here:
You don't have Gross Value in your data, you have Gross.Value.
That column is factor, which is a more storage-efficient form of strings. Neither factor nor character can be summed. R knows nothing about accounting so the "$" means nothing to it in that context.
Try this:
library(dplyr)
someData %>%
mutate(Gross.Value = as.numeric(gsub("[^0-9.]", "", as.character(Gross.Value)))) %>%
group_by(State) %>%
summarize(Gross.Value = sum(Gross.Value, na.rm=TRUE))
# # A tibble: 8 x 2
# State Gross.Value
# <fct> <dbl>
# 1 ACT 0
# 2 NSW 564400574.
# 3 NT 20133040.
# 4 QLD 1053007677.
# 5 SA 691850721.
# 6 TAS 112902970.
# 7 VIC 1069102796.
# 8 WA 281014929.
The only changes from my comment were (1) using the correct column name, and (2) adding na.rm=TRUE, since you have many blanks. This means you need to be careful how you use this data, as you now have biases and inaccuracies in your summary.
You should convert the factor to numeric and then sum. Here is the solution I came up with:
library(tidyverse)
##This line converts the factor into a numeric variable, by making it a character and then removing the commas and the dollar sign. Finally it converts to number
fruit$`Gross Value` <- as.numeric(str_replace_all(as.character(fruit$`Gross Value`),"\\$|\\,",""))
##Then you can run your sum function
fruit %>%
group_by(State) %>%
summarise(Gross = sum(`Gross Value`, na.rm = TRUE))

R and NLS: singular gradient matrix at initial parameter

I'm trying to use nls to estimate the parameters of a non linear model.
I first use nls2 to find good initial parameters with Random Search and I then use nls to improve the estimation with a Gauss-Newton approach.
The problem is I always get an "singular gradient matrix at initial parameter estimates" error.
I'm not sure I understand, because the input matrix doesn't seem to be a singular gradient matrix.
Moreover even if the fits I'm looking for is not perfect for this data, nls should find a way to improve the
parameters estimations. Isn't it ?
Question: Is there a way to improve the parameters estimation?
I've tried NLS.lm but I had the same problem.
Here is a reproductible example:
Data:
structure(list(x1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 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, 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, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3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L, 7L, 7L, 7L, 7L), x2 = c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L,
58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 0L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L,
43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L,
56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 0L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 0L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 0L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L,
43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L,
56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 0L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L,
58L, 59L, 60L, 61L, 62L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L,
61L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L,
40L, 41L, 42L, 43L, 44L, 45L), y = c(0.0689464583349188, 0.0358227182166929,
0.0187034836294036, 0.0227081421239796, 0.0146603483536504, 0.00562771204350896,
0.00411351161052011, 0.00356917888321555, 0.0028017552960605,
0.0024750328652541, 0.00243175013170564, 0.00242654283706898,
0.00235224917236107, 0.00176144220485858, 0.00138071934398105,
0.000696375069179013, 0.00106282865382483, 0.00114735219137874,
0.00277256441625284, 0.00214359572321392, 0.00144935953386591,
0.00249732559162499, 0.00225859018399108, 0.00201642941663214,
0.00232438586834105, 0.0016083751355862, 0.00143118376291818,
0.00158323933266031, 0.00157585431454131, 0.00169206800399143,
0.00158514119474578, 0.00134506293557103, 0.00119442163345335,
0.00101284069499962, 0.0012621113004254, 0.00128964367655383,
0.00102819258807122, 0.00125345601171754, 0.00116155619985178,
0.00142466624262548, 0.00141075318725309, 0.00106556656123991,
0.0010976347045814, 0.0012442089226047, 0.0010627617251863, 0.00125322168410487,
0.00112108560656369, 0.0012459199320756, 0.00135773322693401,
0.0013997982284804, 0.00155012485145915, 0.00151108062240688,
0.00149570655260348, 0.00152598641103596, 0.00108261570337346,
0.000992225418429453, 0.000769588971038765, 0.000700496873143604,
0.000688378351958078, 0.000595007407260441, 0.000557615594951187,
0.00040476923690092, 0.000492276455560289, 0.000447248723966691,
0.000388694992851599, 0.000346087542525691, 0.000189803623801549,
0.0709302325562937, 0.0424623423412875, 0.019085896698975, 0.0190650552541205,
0.014276898897581, 0.00593407290200902, 0.00445528598343583,
0.00371231334350143, 0.00253909496678967, 0.00263487912423124,
0.00248012072619926, 0.00263786771266913, 0.00219351150766708,
0.00179271674850348, 0.00139646119589996, 0.000911560061336614,
0.000989537441246412, 0.001046390000492, 0.00223993432619926,
0.00164189356162362, 0.00106041866437064, 0.00194151698794588,
0.0014213192200082, 0.00165239495268553, 0.00196583929282493,
0.00120501090643706, 0.001141403899631, 0.00122398595424354,
0.00124538223829438, 0.00123370121853218, 0.00136883147552275,
0.00110907318146781, 0.000965843164247642, 0.000859986264862649,
0.00104695561918819, 0.00103985460139401, 0.000455832014104141,
0.000704296760639607, 0.000870145383845838, 0.000919870911357114,
0.00101396309667897, 0.000781894087412874, 0.000909712365723658,
0.000889897365477655, 0.000933063039278393, 0.000779395399425994,
0.000789546295038951, 0.000773432990897909, 0.00125614787798278,
0.00123172652693727, 0.00078936677195572, 0.000952107503075031,
0.00105449131480115, 0.00123128091742517, 0.000889501370397704,
0.00085648642099221, 0.000830097733497335, 0.000653482256334563,
0.000521696831160312, 0.000612702433456335, 0.000513576588109881,
0.000475289330709307, 0.00041141913800738, 0.000328157997211972,
0.00031336264403444, 0.000328784093808938, 0.000237448446412464,
0.0520691145678866, 0.0281929482152033, 0.0219024230330532, 0.0141074098760277,
0.00691341703402584, 0.00445785262213699, 0.0034569415664917,
0.00234406584844369, 0.00257369504707459, 0.00234047371531346,
0.00227286083862502, 0.00248544382019894, 0.00180810413760828,
0.00138986347039715, 0.000911936124008956, 0.000932783218782117,
0.00108887529088974, 0.0017855660833578, 0.00159768589505946,
0.00124091041330201, 0.00203036436876009, 0.00154489107876964,
0.00111687975012847, 0.00163256939968433, 0.00143626193198502,
0.000996683818914256, 0.0010781399542101, 0.00122575793431581,
0.00115671467616723, 0.001069532453476, 0.0010106869893371, 0.000978618104445015,
0.000894478048836441, 0.000842874700392747, 0.000819009288742475,
0.000843003919670386, 0.000964158733115548, 0.000877802228013507,
0.00087592051873807, 0.000935810596369843, 0.000879047729316546,
0.000829181439950081, 0.0010295792954412, 0.000765620227389517,
0.00102511256239906, 0.000823109180461753, 0.00111669534392894,
0.000802757620485245, 0.00103231207284173, 0.000884354083467919,
0.00109278942886507, 0.000969283099489796, 0.000827480664091176,
0.000798564447676552, 0.000909248326695786, 0.000682209033640434,
0.000780593294853913, 0.000485172195712818, 0.000467514093470122,
0.000295219649739392, 0.000460636351123183, 0.00045060371687344,
0.000492590160218764, 0.000402536549331963, 0.000271941766535751,
0.000171012123770371, 0.0267385565244063, 0.0275426278720772,
0.0154589149018475, 0.00729065000152096, 0.00513675524527996,
0.00378848397112206, 0.00305965140790087, 0.00240428827949139,
0.00233604733730811, 0.00199601458903693, 0.00198302547453915,
0.00137121122011316, 0.00126241982975401, 0.0012413298189045,
0.00103044327584109, 0.00106759120581615, 0.00190957422380402,
0.00124400301656831, 0.000989035353673623, 0.00160702520431547,
0.0011515826661394, 0.00153203681379408, 0.00134897491229138,
0.000916492937174261, 0.00072393419977287, 0.00115124473393361,
0.00104241370079698, 0.000953324905193568, 0.00121656899373365,
0.000891420608484922, 0.000671666092758208, 0.000659860761797571,
0.000586145968952161, 0.00072735268499929, 0.000658407622538582,
0.000498831767252743, 0.000658345030520574, 0.000542106922897528,
0.000874560054044737, 0.000543320226217274, 0.000751139509440084,
0.000668632963233356, 0.000656903021131188, 0.000574965903652329,
0.0006661524076778, 0.000605171890653201, 0.000527045917239561,
0.000985791370586684, 0.000899420142057553, 0.000933015548254953,
0.00082137283567561, 0.000870124781995904, 0.000498046123582973,
0.000540181050881142, 0.000596948101336416, 0.000405622486362069,
0.000631594016548032, 0.000468749313033603, 0.000389576698910993,
0.000335624642574679, 0.000286763668856847, 0.000439039581432135,
0.000244767908276044, 0.000303911794528604, 0.000160988671898765,
0.0365772382134747, 0.0255898183301035, 0.010327803963121, 0.00714710822108354,
0.00506253612461807, 0.00447056668291465, 0.00322822676102386,
0.00328154620569948, 0.0028470908747756, 0.00253477302081723,
0.00187837758253778, 0.00116416512964702, 0.00119557763663167,
0.000993575112051645, 0.00136274483135782, 0.00204131052512691,
0.00157953945941769, 0.00116523253183218, 0.00190793844827791,
0.00144595416523011, 0.00157423646879793, 0.00126996001866537,
0.00115283860342634, 0.00116894693507543, 0.000930041619012519,
0.00106545753272384, 0.00123507493015348, 0.00130865599847824,
0.000940647984853709, 0.000836521897923032, 0.000778436697656724,
0.00100773629284415, 0.000956581999215341, 0.000808036977042788,
0.000597930101173421, 0.000776453419209873, 0.000630241947142534,
0.000649832426616575, 0.000782188275296327, 0.00102823806308181,
0.000830656989407107, 0.00051915559901561, 0.000537114715917872,
0.000872430107712244, 0.000549284113632851, 0.000738257038745497,
0.00097442578198376, 0.000879724260815807, 0.000884543540237537,
0.00100038027474944, 0.00103543285342337, 0.000875585441608313,
0.000829083410412184, 0.000760316116414823, 0.000712211369823927,
0.000386744815307978, 0.000428331410721292, 0.000397681982571065,
0.000213938551710199, 0.000370800615243779, 0.000281234314553042,
0.000267359921177464, 0.000358376119030352, 0.000337361541022196,
0.0310029062887812, 0.0154963087949333, 0.00959302943445506,
0.00645674376405936, 0.00525321947702945, 0.00386084394749159,
0.00374364242039947, 0.00351047952579374, 0.00298556939927835,
0.00199158625919048, 0.00206559575086432, 0.00169077836254661,
0.00139156751815451, 0.00170363478493893, 0.00250481301085496,
0.00182474837251083, 0.00116804333227652, 0.00155778636185214,
0.00183778204100427, 0.00135012918459471, 0.00166904872503284,
0.00120137403943415, 0.00108307957787943, 0.00146041465872549,
0.0014437889563235, 0.000975926161359965, 0.00102580511345623,
0.00112145083941, 0.000921884915530595, 0.00082253191796126,
0.000634876416504371, 0.00108601324863747, 0.000830573067167897,
0.000965052460105379, 0.000922667052402736, 0.000863193817654785,
0.000982111173513293, 0.000763009170856168, 0.000921755812461313,
0.000771609983091022, 0.000669047474976222, 0.000773869648383834,
0.00072022523061129, 0.000742426347056781, 0.000718728249316847,
0.000761437280522971, 0.000833112611531319, 0.000794451658438637,
0.000907360341651947, 0.00112083735676435, 0.00102996529205731,
0.000651843453054939, 0.000640968179416338, 0.000549646466476441,
0.000778958256714525, 0.000627413038784969, 0.000523658918731223,
0.000418571973368359, 0.000643352520494588, 0.000351378727146459,
0.000504093577607682, 0.000333827596358531, 0.000339505558071773,
0.0181836504450303, 0.0135527124187004, 0.00780738765319868,
0.00643260738080874, 0.00476881905655232, 0.00406986745617877,
0.00400325917456592, 0.00277499160186111, 0.00198311377238581,
0.00241837807740304, 0.00141018451525995, 0.00166798657140732,
0.0013970042073337, 0.00237332662413329, 0.00146721126831566,
0.000990562316636778, 0.00186106889002752, 0.00186322276224556,
0.00140391140302307, 0.00139027556176293, 0.00125730361478641,
0.00127044200804939, 0.00126655503830484, 0.00133956330669488,
0.00128219844136096, 0.00109531452608613, 0.00112195611926977,
0.00101411381866565, 0.00104786051750783, 0.000798711632769435,
0.000852432172756047, 0.000852720107765923, 0.00110385307389073,
0.00081385514739304, 0.00102898862672826, 0.000710330768658628,
0.000803425598538879, 0.000723455383750816, 0.00075034248654992,
0.000864917906994041, 0.000799733114881449, 0.000608518601191706,
0.000855476747683942, 0.000988548021123443, 0.00104800683206201,
0.000997051779707941, 0.000796235203259423, 0.000910577791459715,
0.000869997383535945, 0.000557402535474327, 0.000757813148434336,
0.000480807445269952, 0.000553425518375578, 0.000633029237291637,
0.00050222863978579, 0.000390945889771328, 0.000430333228928208,
0.000425167676834459, 0.000239604519722651, 0.000357021364759551,
0.000292330910803864, 0.000288851701197491, 0.0198837196044917,
0.0142208140311702, 0.00733039271103269, 0.00609158853724431,
0.00487605866828399, 0.00382636157210858, 0.00411545257392807,
0.00235906433257981, 0.00228491326937568, 0.00109255715480326,
0.00158036861847788, 0.00122011020381908, 0.00223761733564904,
0.00173284341769128, 0.00117538923471357, 0.00219622963095698,
0.00214263916211795, 0.0013198229549172, 0.00172951959530242,
0.00128074705482347, 0.00124062569884766, 0.00144218669111025,
0.00148407512819099, 0.00100716026446858, 0.0010842890711437,
0.000800686408079248, 0.000890454658065465, 0.000887152794471706,
0.00105780722647994, 0.000874948318354744, 0.000569126715186268,
0.000924642167943982, 0.000857013884141074, 0.000823122890591976,
0.00073038777177409, 0.000522615873628494, 0.00070936497950782,
0.000823074755104667, 0.000720588701733105, 0.000722724038337836,
0.00063458965098969, 0.000620049346639466, 0.000842327487089008,
0.000617708212493797, 0.000783953750160813, 0.00112567150392384
)), .Names = c("x1", "x2", "y"), class = c("tbl_df", "data.frame"
), row.names = c(NA, -500L))
Initial parameters: initial_par
structure(list(A1 = 0.0529486559121727, alpha1 = 0.00888818269595504,
B1 = 0.250994319084551, beta1 = 0.471984946168959, A2 = 0.281956987357551,
alpha2 = 0.325086771510541, B2 = 0.0562204262765557, beta2 = 0.725645614322275), class = "data.frame", row.names = c(NA,
-1L), .Names = c("A1", "alpha1", "B1", "beta1", "A2", "alpha2",
"B2", "beta2"))
Formula:
formula = y ~
(A1*exp(-alpha1*x1) + B1*exp(-beta1*x1)) *
(A2*exp(-alpha2*x2) + B2*exp(-beta2*x2))
Nls and the error message
final = nls(formula,
data=df,
start = as.list(as.vector(initial_par)))
Error in nlsModel(formula, mf, start, wts) :
singular gradient matrix at initial parameter estimates
The problem is that there is not a one to one relationship between your model and parameters. To see this write A1 = exp(a1+d), A2 = exp(a2-d), B1 = exp(b1+d), B2 = exp(b2-d) in which case we have:
y ~ exp(-alpha1 * x1 + a1 + d) * exp(-alpha2 * x2 + a2 - d) +
exp(-alpha1 * x1 + a1 + d) * exp(-beta2 * x2 + b2 - d) +
exp(-beta1 * x1 + b1 + d) * exp(-alpha2 * x2 + a2 - d) +
exp(-beta1 * x1 + b1 + d) * exp(-beta2 * x2 + b2 - d)
But d cancels in each of the 4 terms and so cancels entirely from the RHS. That is, the RHS is the same for any value of d thus the model is overparameterized and so will give a singular gradient.
Fix one of A1, A2, B1, B2 and then you should be able to get a solution:
A1 <- 1
nls(formula, df, start = initial_par[-1])
giving:
Nonlinear regression model
model: y ~ (A1 * exp(-alpha1 * x1) + B1 * exp(-beta1 * x1)) * (A2 * exp(-alpha2 * x2) + B2 * exp(-beta2 * x2))
data: df
alpha1 B1 beta1 A2 alpha2 B2 beta2
0.11902 1.21030 0.79076 0.04604 0.51697 0.00183 0.02317
residual sum-of-squares: 0.000685
Number of iterations to convergence: 11
Achieved convergence tolerance: 6.686e-06

Resources