Get edge lenghts as spatial distances in a network in R - r

I have a list of nodes with spatial coordinates nodes_geo. The first column is the nodes' identifier, the other 2 columns are the x and y coordinates:
nodes_geo <- structure(list(grid_grid.g9.nodes = c(27, 28, 29, 40, 41, 42,
43, 55, 56, 58, 69, 81, 94, 98, 108, 110, 113, 114, 123, 124,
128, 138, 139, 141), grid_grid.coords.x1 = c(15.504078, 15.704078,
15.904078, 15.104078, 15.304078, 15.504078, 15.704078, 15.104078,
15.304078, 15.704078, 14.904078, 14.304078, 13.904078, 14.704078,
13.704078, 14.104078, 14.704078, 14.904078, 13.704078, 13.904078,
14.704078, 13.704078, 13.904078, 14.304078), grid_grid.coords.x2 = c(43.835623,
43.835623, 43.835623, 44.035623, 44.035623, 44.035623, 44.035623,
44.235623, 44.235623, 44.235623, 44.435623, 44.635623, 44.835623,
44.835623, 45.035623, 45.035623, 45.035623, 45.035623, 45.235623,
45.235623, 45.235623, 45.435623, 45.435623, 45.435623)), class = "data.frame", row.names = c(NA,
-24L))
And I have an adjacency matrix for these nodes adjacency_matrix:
adjacency_matrix <- structure(c(0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1,
1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0,
0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 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, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(24L, 24L), .Dimnames = list(
c("27", "28", "29", "40", "41", "42", "43", "55", "56", "58",
"69", "81", "94", "98", "108", "110", "113", "114", "123",
"124", "128", "138", "139", "141"), c("27", "28", "29", "40",
"41", "42", "43", "55", "56", "58", "69", "81", "94", "98",
"108", "110", "113", "114", "123", "124", "128", "138", "139",
"141")))
I would like to get the spatial lenght of edges in a network based on the above adjacency matrix. I would also like to be able to do the same from random networks generated from the real network with the same number of edges. The goal is to compare the mean edge lenght of the real network and that of the random networks for hypothesis testing.

Here is a starting point using the data from above (note that we assue these
coordinates are planar with Euclidean distances, so you should really project
first):
library(spatstat)
nodes_ppp <- as.ppp(nodes_geo[,c(2:3,1)], W = bounding.box.xy(nodes_geo[,-1]))
head(as.data.frame(nodes_ppp))
#> x y marks
#> 1 15.50408 43.83562 27
#> 2 15.70408 43.83562 28
#> 3 15.90408 43.83562 29
#> 4 15.10408 44.03562 40
#> 5 15.30408 44.03562 41
#> 6 15.50408 44.03562 42
a_mat <- adjacency_matrix > 0 # Logical matrix required
typeof(adjacency_matrix)
#> [1] "double"
typeof(a_mat)
#> [1] "logical"
network <- linnet(nodes_ppp, a_mat) # Linear network
#> Warning: Network is not connected
plot(network)
segs <- as.psp(network) # Segments of linear network
rslt <- lengths_psp(segs) # Segment lengths
hist(rslt)

Related

Errors with distance-decay using betapart and ddecay packages

My goal is to create a distance-decay curve for species data vs geographic distance. However, I am running into errors. For the betapart package, this may be due to the lack of columns relative to the number of rows. Is there a way to get past this? If not, is there another method for creating a distance-decay curve (and plotting it)? I also tried the ddecay package but ran into errors there too. Any help is much appreciated. Data is in structure form below.
# BETAPART -------------------------------------------------
library(betapart)
spat.dist<-dist(coords)
dissim.BCI<-beta.pair.abund(spec)$beta.bray.bal
plot(spat.dist, dissim.BCI, ylim=c(0,1), xlim=c(0, max(spat.dist)))
BCI.decay.exp<-decay.model(dissim.BCI, spat.dist, y.type="dissim", model.type="exp", perm=100)
#========================================================================================================
I also tried a few other packages --------------------------
# ddecay package -------------------------------------------
devtools::install_github("chihlinwei/ddecay")
the issue with this method is that it requires the use of a gradient however, I would like to avoid that if possible but I do not see a way around this. Also they do not include their example data in the package.
dd <- beta.decay(gradient=spat.dist, counts=decostand(spec, method="pa"),
coords=coords, nboots=1000,
dis.fun = "beta.pair", index.family = "sorensen", dis = 1, like.pairs=T)
x <- vegdist(coords, method = "euclidean")
y <- 1 - dist(decostand(spec, method="pa"), index.family = "sorensen")[[1]]
plot(x, y)
lines(dd$Predictions[, "x"], dd$Predictions[,"mean"], col="red", lwd=2)
#========================================================================================================
# DATA -----------------------------------------------------
spec <- structure(list(Ccol = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Acol = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0), NYcol = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0), Mcol = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0), AAcol = c(14, 0, 14, 3, 11, 1, 0, 2, 0,
3, 0, 4, 0, 1, 8, 2, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 7),
Ncol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0), Vcol = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0), Ocol = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0), AVcol = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), JXcol = c(0,
3, 3, 0, 0, 0, 0, 0, 8, 0, 0, 10, 3, 0, 0, 5, 0, 0, 0, 1,
0, 0, 0, 2, 4, 1, 0, 0, 0)), class = "data.frame", row.names = c(NA,
-29L))
coords <- structure(list(Lat.x = c(34.43363, 34.36784, 34.32587, 34.19891,
34.24217, 34.24863, 34.18137, 34.16838, 34.10961, 34.08329, 34.40571,
34.39591, 34.39292, 34.37466, 34.28948, 34.26146, 34.04687, 34.0409,
34.068339, 34.34679, 34.17161, 34.23308, 34.21544, 34.14922,
34.27539, 34.2323, 34.19057, 34.07042, 34.06289), Lon.x = c(-94.94494,
-94.92512, -94.94429, -94.84497, -94.8573, -94.85641, -94.887,
-94.91322, -94.92913, -94.93276, -95.02622, -95.04382, -94.96295,
-94.83733, -94.81071, -94.79161, -95.03968, -95.0608, -95.086986,
-95.03345, -95.23862, -95.25619, -95.1041, -95.02286, -95.02672,
-95.02626, -95.02941, -95.01746, -94.98786)), class = "data.frame", row.names = c(NA,
-29L))
You can get more answers, if you tell what was the problem. For instance, which functions failed and what was the error message. I had a look at betapart::decay.model(), where I could get this error message:
Error in eval(family$initialize) :
cannot find valid starting values: please specify some
I cut the long story short: you cannot use this function with your data because you have dissimilarities of 1 in your data, dissimilarities are turned into similarities with 1-dissimilarity and this makes these values zero similarities (that is, these pairs of sampling unit have nothing in common, they share no species). Function decay.model uses glm with gaussian family with log-link, and log-link requires that you give the starting values, if you have zeros in the y-variate.
I think that you have four alternatives:
You do not use the method as it does not suit your data.
You modify the decay.model function so that you can specify the starting values, like the error message suggested. This means that you add mustart to the function call so that it reads, e.g., glm(y ~ x, family=gaussian(link="log"), mustart=pmax(y, 0.01)). This replaces zeros with 0.01 as starting values.
You change maximum distances from 1 to something smaller, for instance, 0.99: dissim.BCI[dissim.BCI==1] <- 0.99. However, this changes the data, and also changes the results from those you get with alternative 2 (which only changes starting values, but data are unmodified). However, the effect is not very large and any Bayesian would claim that dissimilarity 1 is just a frequentist folly (you just haven't seen the case that is in common with these sampling units).
You change the maximum distance to missing values. This will change data more than alternative 3. It removes maximum dissimilarities and these no longer influence the decay curve. The effect is the same as censoring greatest dissimilarities. The results change more than in alternative 3.

Drop first element of list of lists, condense list of lists? Too many elements?

Hello wonderful community!
I am having trouble working with a list of lists, each containing 4 DF. I only provide two lists here but I have 54. What I want to do: (1) I want to summarize the amount of rows in each "Comm". (2) I want to drop "comm". (3) I want to combine all of the other elements in each list, into one data frame.
Data below
I have tried all these approaches as well as others.
R list of lists to data.frame
R - list to data frame
Export each data frame within a list to csv
Any ideas on how to do this? Thanks!
> dput(c(output$`2000-Fall`,output$`2001-Spring`))
list(Comm = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(37L,
27L), .Dimnames = list(NULL, c("AS", "LNS", "SC", "BND", "RT",
"LND", "EBT", "BM", "CS", "BT", "BB", "WS", "GS", "CP", "YB",
"F", "P", "LMB", "YP", "RP", "B", "TD", "AE", "CCS", "A", "FSS",
"M"))), Coherence = structure(list(name = structure(c(1L, 6L,
3L, 4L, 5L, 2L), .Label = c("embAbs", "method = r1", "p", "simMean",
"simVariance", "z"), class = "factor"), stat = c(200, 11.3001899623484,
1.30934461418281e-29, 604.17, 35.7666553701019, NA)), class = "data.frame", row.names = c(NA,
-6L)), Turnover = structure(list(name = structure(c(5L, 6L, 2L,
3L, 4L, 1L), .Label = c("method = EMS", "p", "simMean", "simVariance",
"turnover", "z"), class = "factor"), stat = c(10893, -6.01765438449338,
1.7696251436801e-09, 5823.65, 842.412952971008, NA)), class = "data.frame", row.names = c(NA,
-6L)), Boundary = structure(list(name = structure(c(2L, 3L, 1L
), .Label = c("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,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = 33:32, .Dimnames = list(
NULL, c("TT", "BM", "LC", "RT", "SC", "CS", "AS", "CRC",
"LND", "BND", "EBT", "BT", "TD", "BL", "F", "SMB", "WS",
"RP", "P", "GSF", "YP", "AE", "CP", "LMB", "YB", "BS", "BB",
"GS", "B", "FSS", "K", "M"))), Coherence = structure(list(
name = structure(c(1L, 6L, 3L, 4L, 5L, 2L), .Label = c("embAbs",
"method = r1", "p", "simMean", "simVariance", "z"), class = "factor"),
stat = c(261, 8.8243058044828, 1.10140877775716e-18, 611.43,
39.711905702767, NA)), class = "data.frame", row.names = c(NA,
-6L)), Turnover = structure(list(name = structure(c(5L, 6L, 2L,
3L, 4L, 1L), .Label = c("method = EMS", "p", "simMean", "simVariance",
"turnover", "z"), class = "factor"), stat = c(10074, -4.74001047198576,
2.13707173521215e-06, 5701.71, 922.422012744687, NA)), class = "data.frame", row.names = c(NA,
-6L)), Boundary = structure(list(name = structure(c(2L, 3L, 1L
), .Label = c("df", "index", "p"), class = "factor"), stat = c(1.82705314009662,
0.000113336323694813, 30)), class = "data.frame", row.names = c(NA,
-3L)))
The object shown in the question is not a list of lists of 4 data frames each so if we call that object L then to get what is described we need to create LL like this. Now using LL we can compute the 3 items requested.
LL <- list(L[1:4], L[5:8])
res1 <- sapply(LL, function(x) nrow(x$Comm))
res2 <- lapply(LL, function(x) x[names(x) != "Comm"])
res3 <- lapply(LL, function(x) do.call("rbind", x[names(x) != "Comm"]))

"non-finite value supplied by optim" error when using betareg

I'm working with betareg package for beta regression but receive the below error:
Error in optim(par = start, fn = loglikfun, gr = gradfun, method = method, :
non-finite value supplied by optim
I can trace this error to creating the initial values for optim. Specifically, these lines of betareg.fit, which uses lm.wfit to generate starting values.
It turns out that one of the starting values is returned as NA for my dataset. I'm unsure why this is the case, since there are no missing values in the data / input to lm.wfit.
Reproducible example to see NA
## data -- a sample of 100 obs from my actual data
nobs <- 100L
w <- rep(1, nobs)
offset <- rep(0, nobs)
y <- stats::rbeta(nobs, 0.75, 1.658)
x <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0165928242550604,
0.0984749494334759, 0.05517578125, 0.0185352577155742, 0.168701442841287,
0.0514759697487192, 0.026507054296708, 0.0188496858385694, 0.108620689655172,
0.0722387772757858, 0.0272373540856031, 0.0538907902524382, 0.0295235311312482,
0.0318257956448911, 0.231788079470199, 0.0674772036474164, 0.14846108458939,
0.0969908238068386, 0.0441553321506012, 0.154121863799283, 0,
0.110460389247421, 0.0292207792207792, 0.0522853185595568, 0.205288796102992,
0.00961124552835874, 0.0546908714289824, 0.0268199233716475,
0.0253164556962025, 0.181780542384243, 0.0551724137931034, 0.128842504743833,
0.0751429349305745, 0.217853751187085, 0.0510314875135722, 0.108407709439207,
0.04, 0.0638009815535624, 0.128329297820823, 0.0398115958281933,
0.0513258247605534, 0.0520833333333333, 0.0956239870340357, 0.0742899497995351,
0.144527098831031, 0.0723209169054441, 0.140116763969975, 0.172426847735821,
0.00830471112933819, 0.0548386400835806, 0.0372010221576987,
0.0549927641099855, 0.0386658431130327, 0.0256367439122648, 0.0166402535657686,
0.0769230769230769, 0.0130681818181818, 0.0229684699649666, 0.0344827586206897,
0.0135106607557526, 0.0581090909090909, 0.0321364452423698, 0.0141176470588235,
0.0203003337041157, 0.0948080795499367, 0.0202898550724638, 0.0443828016643551,
0.105830475257227, 0.0482315112540193, 0.0394736842105263, 0,
0.071608040201005, 0.0416666666666667, 0.268330928934329, 0.0422895357985838,
0.127678318597993, 0.037029330162505, 0.0328938677375888, 0.10183299389002,
0.0628212450028555, 0.0283431291591781, 0.0690879300928454, 0.134792626728111,
0.0770505385252693, 0.174605316421536, 0.0842012497997116, 0.068774108570891,
0.137089781654799, 0.00986436498150432, 0.0812065297585365, 0.0904255319148936,
0.0205776173285199, 0.0124064303568112, 0.0229630147033144, 0.0578925872983459,
0.0709677419354839, 0.0640070144673389, 0.106259964391839, 0.0315146661646867,
0.0356999429308195, 0.0268438884545218, 0.0748295057905382, 0.0556640625,
0.021107943539976, 0.199778024417314, 0.0175652598194682, 0.0487387772552373,
0.00289995166747221, 0.0672413793103448, 0.101364990868019, 0.0233463035019455,
0.0732353773706287, 0.022508038585209, 0.0368509212730318, 0.101545253863135,
0.0158054711246201, 0.152565574210159, 0.123442866663249, 0.0672186083185492,
0.129032258064516, 0, 0.104565780781544, 0.0551948051948052,
0.03601108033241, 0.160055671537926, 0.02201309207499, 0.0668891510112489,
0.0421455938697318, 0.0632911392405063, 0.234027661399237, 0.0206896551724138,
0.0950664136622391, 0.0936564116526001, 0.183475783475783, 0.0466883821932682,
0.088748974363268, 0.0422641509433962, 0.0467084108986292, 0.0920096852300242,
0.0401480318492767, 0.05103760198652, 0.0208333333333333, 0.0470016207455429,
0.0887666928515318, 0.075451647183847, 0.0310601719197708, 0.0928685551212356,
0.148991255923013, 0.0204541959296663, 0.0689569784090949, 0.0356901206750445,
0.0680173661360347, 0.0508956145768993, 0.0320699343321964, 0.0293185419968304,
0.0659340659340659, 0.00284090909090909, 0.0402373780415312,
0, 0.0230103440996411, 0.0629090909090909, 0.0229802513464991,
0, 0.00778642936596218, 0.0745676859521289, 0.0191304347826087,
0.0443828016643551, 0.0975012248897599, 0.00964630225080386,
0, 0, 0.0678391959798995, 0.05, 0.167973405256225, 0.0427812745869394,
0.197810150080232, 0.0363158937772493, 0.0415070411371503, 0.109979633401222,
0.0285551113649343, 0.0348520911254675, 0.0730930274895321, 0.119239631336406,
0.087821043910522, 0.251855350155175, 0.0668162153501042, 0.0731018910527801,
0.0945505662261226, 0.0530209617755857, 0.0879397164898608, 0.0531914893617021,
0.0339350180505415, 0.0144171389279045, 0.0176020675444417, 0.0758627731264039,
0.0387096774193548, 0.0791319596668128, 0.0898373582199164, 0.0641540015091306,
0.0308596309526326, 0.0853097037616193, 0.145443642937691, 0.134765625,
0.0735152424185233, 0.207547169811321, 0.0712368870456209, 0.1626763574177,
0.0128081198646689, 0.096551724137931, 0.130923771988849, 0.0525291828793774,
0.230005661202748, 0.0833089739842151, 0.123953098827471, 0.344370860927152,
0.0419452887537994, 0.188428900927962, 0.237145640026657, 0.184407648334319,
0.211469534050179, 0.0576923076923077, 0.182300929925301, 0.165584415584416,
0.0806786703601108, 0.274298306657388, 0.0837699832267066, 0.137316953882448,
0.157088122605364, 0.10126582278481, 0.260575112627044, 0.0413793103448276,
0.198292220113852, 0.177511570922951, 0.296296296296296, 0.139522258414767,
0.138297694201175, 0.0645283018867925, 0.12692502961584, 0.162227602905569,
0.0730066165750813, 0.142559418233416, 0.239583333333333, 0.152350081037277,
0.204935285553934, 0.155154091392136, 0.0889398280802292, 0.135152787965875,
0.235552733134678, 0.0947352232531912, 0.154476512637182, 0.137157008940595,
0.162083936324168, 0.138789376158122, 0.104513252539252, 0.0935023771790808,
0.120879120879121, 0.0133522727272727, 0.0824038693376326, 0.0344827586206897,
0.0789529237914292, 0.133527272727273, 0.0569120287253142, 0.0117647058823529,
0.0233592880978865, 0.158352535758029, 0.0527536231884058, 0.104022191400832,
0.201371876531112, 0.0289389067524116, 0.144736842105263, 0,
0.10678391959799, 0.0583333333333333, 0.233080348742395, 0.127753737214791,
0.233930088412044, 0.13375941339675, 0.13934317947634, 0.338085539714868,
0.0805254140491148, 0.101811822995094, 0.167819649250561, 0.142857142857143,
0.227009113504557, 0.254081770341384, 0.155103348822304, 0.161162856336438,
0.124957303134607, 0.155363748458693, 0.214043708410037, 0.111702127659574,
0.126714801444043, 0.0475465770701011, 0.195590169850639, 0.204512967122728,
0.158064516129032, 0.306298407131375, 0.191432758458608, 0.179792327044226,
0.072245355202807, 0.286421683606985, 0.19797912900281, 0.32275390625,
0.4288140812333, 0.180910099889012, 0.165406196633325, 0.539974348011971,
0.0396326727887869, 0.205172413793103, 0.528357204652504, 0.0953307392996109,
0.456736831270425, 0.312189418298743, 0.440536013400335, 0.322295805739514,
0.185410334346505, 0.289871801748646, 0.277182549853899, 0.478218016952494,
0.186379928315412, 0.730769230769231, 0.312846181208395, 0.573051948051948,
0.104916897506925, 0.251913709116214, 0.240451839867001, 0.390675796196668,
0.314176245210728, 0.151898734177215, 0.321921116708234, 0.191379310344828,
0.352371916508539, 0.384971413013885, 0.245204178537512, 0.314060803474484,
0.177397478189521, 0.128301886792453, 0.418175664240988, 0.0532687651331719,
0.263429404508243, 0.351343561546648, 0.520833333333333, 0.160453808752026,
0.40037131675589, 0.0701381509032944, 0.139484240687679, 0.208627671654089,
0.302720922280299, 0.464089813912954, 0.325967367919772, 0.509502256498519,
0.357452966714906, 0.387770228536133, 0.389464763566684, 0.401743264659271,
0.450549450549451, 0.0741477272727273, 0.371069078965643, 0.0344827586206897,
0.351488283723876, 0.394836363636364, 0.10394973070018, 0.0164705882352941,
0.0661846496106785, 0.247721658259119, 0.271552795031056, 0.375866851595007,
0.326800587947085, 0.090032154340836, 0.302631578947368, 0, 0.293969849246231,
0.0916666666666667, 0.262623094775136, 0.37765538945712, 0.437246326652613,
0.553230281411019, 0.553700229860763, 0.327902240325866, 0.18275271273558,
0.23291397483849, 0.407002852114813, 0.115207373271889, 0.569179784589892,
0.306166509243017, 0.536532606954014, 0.426756985605419, 0.214233164297538,
0.515413070283601, 0.401348069939186, 0.23936170212766, 0.406859205776173,
0.301968017675986, 0.46031385697156, 0.417500510516643, 0.280645161290323,
0.545082566125968, 0.308916785607088, 0.40121177121836, 0.110259770455074,
0.573885848318999, 0.0979737591064492, 0.05517578125, 0.43828037308911,
0, 0.0195169553549646, 0.106028217186832, 0.0224746254229096,
0.0913793103448276, 0.149860617129674, 0.0214007782101167, 0.138210545276756,
0.499269219526454, 0.351758793969849, 0, 0.0613981762917933,
0.172457222550019, 0.257446045009484, 0.141730731322689, 0.129032258064516,
0, 0.218723004217694, 0.116883116883117, 0.0207756232686981,
0.00150777081883554, 0.598457747628731, 0.310768434074628, 0.0344827586206897,
0.0759493670886076, 0, 0.0620689655172414, 0.0992409867172676,
0.175878028859243, 0.0207027540360874, 0.342562432138979, 0.0380448433496877,
0.0252830188679245, 0.138432898967676, 0, 0.510037007962319,
0.150097552323519, 0.0520833333333333, 0, 0.200037592718361,
0, 0.0220057306590258, 0.107599706582795, 0.0987006008499829,
0.381862921002717, 0.310497634940381, 0.266713526509752, 0.221418234442836,
0.376899320568252, 0.147387273053657, 0.412044374009509, 0.137362637362637,
0.266761363636364, 0.482401647112941, 0, 0.496200126662445, 0.145890909090909,
0.00933572710951526, 0, 0.000278086763070078, 0.0862344564156858,
0.598343685300207, 0.208044382801664, 0.171974522292994, 0.0418006430868167,
0.0921052631578947, 0, 0.268844221105528, 0.0333333333333333,
0.0333061531706705, 0.330841856805665, 0, 0.228289734443123,
0.226188274459035, 0.0712830957230143, 0.0177041690462593, 0.0492786710059746,
0.246950664482068, 0.00576036866359447, 0.0306545153272577, 0,
0.0744271751321904, 0.198701665255433, 0.268701227042224, 0.196054254007398,
0.0790648033408067, 0.0531914893617021, 0.00144404332129964,
0.620702802934266, 0.28496257232331, 0.197569940780069, 0.170967741935484,
0, 0.0478844352441583, 0.210611453120438, 0.0129145441863414,
0.000260620276257493, 0.208709971897542, 0.3251953125, 0.0114926544356481,
0.194228634850166, 0.640644059526714, 0.0280034202650705, 0.885693571773804,
0.313793103448276, 0.00884360280688263, 0.739299610894942, 0.0108913821054528,
0.00380005846243788, 0, 0, 0.610942249240122, 0.0171968030896315,
0, 0.0280898876404494, 0.100358422939068, 0.173076923076923,
0.0244422988973017, 0.0227272727272727, 0.62292243767313, 0.0628624449083739,
0.00386676364499993, 0.00405650335164993, 0.314176245210728,
0.126582278481013, 0.000798493705700851, 0.581034482758621, 0.0829222011385199,
0.0245031309556221, 0.00588793922127255, 0.0293159609120521,
0.380448433496877, 0.353207547169811, 0.0531392790658318, 0.414043583535109,
0.0632499719636649, 0.096842852075204, 0.0416666666666667, 0.494327390599676,
0.00292230188016789, 0.476089266737513, 0.557478510028653, 0.113709215510918,
0.0199428459772361, 0.00563900138411852, 0.0115416622646005,
0.0101230399337831, 0.0419681620839363, 0.00197652872143298,
0.164294749497994, 0.00475435816164818, 0.137362637362637, 0.584090909090909,
0.000167333519259466, 0.885057471264368, 0.00401097741186405,
0.109381818181818, 0.647935368043088, 0.948235294117647, 0.50139043381535,
0.188100724502294, 0.00447204968944099, 0.105409153952843, 0.0264576188143067,
0.765273311897106, 0.368421052631579, 0.841726618705036, 0.0766331658291457,
0.241666666666667, 0.0153672458132096, 0.0698269079464988, 0.00169902148947551,
0.00340864050733254, 0.00261565695262755, 0.0366598778004073,
0.572815533980582, 0.430635838150289, 0.0247284422598459, 0.285714285714286,
0.00165700082850041, 0.00580218593981919, 0.071062329754847,
0.0487345940351868, 0.0753895267873565, 0.00986436498150432,
0.0469890478271147, 0.340425531914894, 0.216967509025271, 7.16946330049502e-05,
0.0013271400132714, 0.0275678987134981, 0.0806451612903226, 0.00146134736226801,
0.0792675274278758, 0.0146813198032706, 0.472691340280273, 0.00295369646425159,
0.1175738808241, 0.0419921875, 0.00405272515967538, 0.048834628190899,
0.0239082703098317, 0.0611372381359555, 0.00894151764137264,
0.0827586206896552, 0.00552725175430164, 0.0330739299610895,
0.0168742441007694, 0.00701549254603917, 0.0100502512562814,
0, 0.0115501519756839, 0.0167676876039264, 0, 0.0364675734279519,
0.046594982078853, 0.0384615384615385, 0.0193480359774379, 0.0275974025974026,
0.0647506925207756, 0.0440733008582695, 0.0107987353233683, 0.0128455939468915,
0.10727969348659, 0.455696202531646, 0.000897073175540462, 0.0344827586206897,
0.0244781783681214, 0.0400217805608494, 0.0260208926875594, 0.0282301845819761,
0.0483765635727323, 0.295849056603774, 0.11034015907937, 0.150121065375303,
0.00336436021083324, 0.101764810216389, 0.0416666666666667, 0.0502431118314425,
0.0128435876929952, 0.0786397449521785, 0.065214899713467, 0.107117376930575,
0.0130062038981974, 0.0127646485876865, 0.0243222483755186, 0.00109047672226346,
0.0419681620839363, 0.00148239654107474, 0.0995579008011222,
0.0182250396196513, 0.010989010989011, 0.0190340909090909, 0.000341111490123878,
0.0114942528735632, 0.00960523538104285, 0.0609454545454545,
0.108617594254937, 0.00941176470588235, 0.370689655172414, 0.0941230200743186,
0.00919254658385093, 0.0527045769764216, 0.0416462518373346,
0.0128617363344051, 0.0263157894736842, 0.100719424460432, 0.0628140703517588,
0.383333333333333, 0.013861882958038, 0.00324547600314713, 0.00163609476764308,
0.00522195798652398, 0.00245713228883194, 0.00814663951120163,
0.0422615648201028, 0.0899839704667994, 0.00467261362946781,
0.165898617511521, 0.00662800331400166, 0.00748886789906895,
0.00480692196763339, 0.00997271615391853, 0.0314314616779211,
0.0357583230579531, 0.0671742820918722, 0.0585106382978723, 0.193501805054152,
0.000332402389386587, 0.00658331296056997, 0.00745354298550133,
0.106451612903226, 0.00401870524623703, 0.120851102817492, 0.0428155973269565,
0.222051954091015, 0.00773173486230562, 0.0590151610073926, 0.00927734375,
0.00420172240819286, 0, 0.0102464015613564, 0.0269345874305259,
0.00869985500241663, 0.0344827586206897, 0.00288378352398347,
0.00778210116731518, 0.0201551684207818, 0.0423852674656533,
0.0050251256281407, 0, 0.00547112462006079, 0.0142509252802661,
0.00779207463987287, 0.019712201852947, 0.043010752688172, 0,
0.0273133797449057, 0.00974025974025974, 0.0176592797783934,
0, 0.0310305927058439, 0.022756696107483, 0.00383141762452107,
0, 0, 0.0137931034482759, 0.0187855787476281, 0.0283147291042744,
0.00455840455840456, 0.0485884907709012, 0.0202783033875316,
0.0505660377358491, 0.0424775765781012, 0, 0.0069530111023887,
0.055028378857751, 0.03125, 0, 0.0158332727475844, 0, 0.0234957020057307,
0.0948079222645377, 0.00865859020077182, 0.0121494848003281,
0.0493989553698703, 0.00252254856234439, 0.0520984081041968,
0.0035206917850525, 0.0370753822968285, 0.0237717908082409, 0,
0.0267045454545455, 0.000411111567901742, 0, 0.0232214481739498,
0.0344, 0.0181328545780969, 0, 0.0100111234705228, 0.0560918394884889,
0.0242650103519669, 0.0651872399445215, 0.0284174424301813, 0.00321543408360129,
0.0263157894736842, 0.0575539568345324, 0.0515075376884422, 0.1,
0.00545694034999686, 0.00560582218725413, 0, 0.00274474831549742,
0.00129461808766414, 0.00610997963340122, 0.0125642490005711,
0.032180502258707, 0.00664482068086656, 0.0305299539170507, 0,
0, 0.00705015221919564, 0.0127951829899332, 0.0536469691794319,
0.0246609124537608, 0.022233842142586, 0.0531914893617021, 0,
0.00255493601254004, 0.0106578656328944, 0.0116397794568103,
0.0935483870967742, 0, 0.0555500678330224, 0.0552188638129316,
0.0432774619010378, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0,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1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 0), .Dim = c(100L, 35L), .Dimnames = list(c("2801", "2316",
"382", "8062", "2687", "2731", "8019", "5652", "8429", "3479",
"7753", "9001", "2188", "8121", "8478", "5817", "1528", "2460",
"3946", "3531", "3421", "2802", "1975", "3639", "2894", "5897",
"9331", "9490", "7135", "5858", "7724", "9414", "9095", "6601",
"5064", "7111", "3593", "7322", "9522", "7116", "6922", "5172",
"2458", "5199", "1387", "3878", "6119", "8722", "6378", "4661",
"6109", "3682", "5751", "9390", "7915", "5268", "1029", "5953",
"242", "2912", "8798", "9607", "9768", "2222", "8260", "851",
"4205", "1823", "5063", "4189", "7541", "608", "6849", "7220",
"2889", "6770", "7064", "646", "4919", "1404", "120", "9716",
"7722", "7700", "6638", "8176", "5745", "6", "9481", "2233",
"341", "228", "1543", "553", "9709", "9493", "881", "7647", "6039",
"2925"), c("(Intercept)", "x 1", "x 2", "x 3", "x 4", "x 5",
"x 6", "x 7", "x 8", "x 9", "x 10", "x 11", "x 12", "x 13", "x 14",
"x 15", "x 16", "x 17", "x 18", "x 19", "x 20", "x 21", "x 22",
"x 23", "x 24", "x 25", "x 26", "x 27", "x 28", "x 29", "x 30",
"x 31", "x 32", "x 33", "x 34")))
Inside betareg: the NA that causes the problem
linkfun <- function(mu) {.Call(stats:::C_logit_link, mu)}
auxreg_test <- lm.wfit(x, linkfun(y), w, offset)
# problem:
(beta <- auxreg_test$coefficients)
is.na(beta['x 8'])
> beta['x 8']
x 8
NA
I originally thought this might be related to using the CRAN version of betareg (3.1-0). But I updated to the rforge version (3.2-0) via devtools::install_github("rforge/betareg/pkg") and still have the same problem.
If I remove the offending predictor from my formula, betareg runs fine; however, the predictor is a necessary one.
NA coefficients from glm / lm / lm.fit / .lm.fit / lm.wfit imply the model matrix to be rank-deficient. They are just 0 with 0 standard error (i.e., fixed at 0).
I appreciated that you have done much debugging work and located the source of the error, but giving us a model matrix x directly is less informative for us to investigate. It would be good if you show us the model formula and the data frame.
Anyway, I have (with some pain) found the collinearity problem from your model matrix.
rowSums(, x[, 2:9])
#2801 2316 382 8062 2687 2731 8019 5652 8429 3479 7753 9001 2188 8121 8478 5817
# 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#1528 2460 3946 3531 3421 2802 1975 3639 2894 5897 9331 9490 7135 5858 7724 9414
# 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#9095 6601 5064 7111 3593 7322 9522 7116 6922 5172 2458 5199 1387 3878 6119 8722
# 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#6378 4661 6109 3682 5751 9390 7915 5268 1029 5953 242 2912 8798 9607 9768 2222
# 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#8260 851 4205 1823 5063 4189 7541 608 6849 7220 2889 6770 7064 646 4919 1404
# 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
# 120 9716 7722 7700 6638 8176 5745 6 9481 2233 341 228 1543 553 9709 9493
# 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
# 881 7647 6039 2925
# 1 1 1 1
Columns x1 to x8, if all included, has collinearity problem with the intercept (strange; those columns are not dummy ones so they are not from factor variables). If you don't want to drop any of them, drop intercept instead.

How to make R understand there are categories with aggregated count=0 [duplicate]

This question already has answers here:
grouped barplot: order x-axis & keep constant bar width, in case of missing levels
(4 answers)
Closed 6 years ago.
I have aggregated data on categories of funding research from a larger data file using the following synthax:
fund.tab2=aggregate(data.frame(count=sysrev$NO),list(value=sysrev$fgroup,sysrev$yrgroup),length)
The first part of the resulting data frame (fund.tab2) looks like this (after replacing the column titles):
cat time count
3 50 17
1 51 7
2 51 7
3 51 23
1 52 9
3 52 31
4 52 1
1 53 18
2 53 5
3 53 33
4 53 1
1 54 24
2 54 7
3 54 26
4 54 2
1 55 13
2 55 17
3 55 36
4 55 15
As it happens, count=0 at time=50 of cat=1, cat=2 and cat=4, at time=51 for cat=4, and at time=52 for cat=2. When using the full table to plot a stacked (dodged) bar it will look like this:
How do I get R to understand and place a zero in categories where count=0 in the aggregated data table?
Here are 75 lines (DPUT)from my original data frame to give you a reproducible example:
structure(list(YR = c(1975, 1974, 1974, 1974, 1974, 1974, 1974,
1974, 1973, 1973, 1973, 1973, 1973, 1973, 1973, 1973, 1973, 1973,
1972, 1972, 1972, 1972, 1972, 1972, 1972, 1971, 1971, 1971, 1971,
1971, 1971, 1971, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970,
1970, 1969, 1969, 1969, 1969, 1969, 1969, 1968, 1968, 1968, 1968
), NRAUTH = c(2, 1, 2, 1, 1, 1, 2, 3, 1, 1, 3, 1, 1, 1, 2, 1,
1, 3, 1, 1, 1, 3, 1, 2, 2, 1, 1, 1, 1, 4, 3, 1, 4, 1, 3, 2, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1), LEAD = structure(c(4L,
32L, 4L, 33L, 4L, 33L, 33L, 4L, 4L, 9L, 33L, 4L, 5L, 33L, 33L,
9L, 27L, 4L, 26L, 33L, 9L, 33L, 33L, 27L, 4L, 5L, 26L, 26L, 4L,
29L, 29L, 33L, 4L, 9L, 33L, 33L, 4L, 26L, 26L, 26L, 26L, 4L,
4L, 28L, 33L, 30L, 26L, 33L, 4L, 33L, 25L), .Label = c("AUS",
"AUT", "BEL", "CAN", "CHE", "CHN", "CRI", "CZE", "DEU", "DNK",
"ESP", "FIN", "FRA", "GBR", "GRL", "HOL", "HUN", "IRL", "ISL",
"ITA", "JPN", "KOR", "LUX", "MEX", "NLD", "NOR", "NZL", "POL",
"RUS", "SC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1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1), FOTH = c(1, 1, 1, 1, 0, 1, 1, 1, 1,
1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1,
0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1), fgroup = c(3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 1,
3, 3, 1, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 1, 3, 1, 3,
3, 3, 1, 3, 1, 3, 3, 2, 1, 2, 2, 3, 3, 3, 3, 3, 2, 3, 3,
3, 3), yrgroup = c(52, 52, 52, 52, 52, 52, 52, 52, 52, 52,
52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52,
52, 52, 52, 52, 52, 52, 52, 51, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51)), .Names = c("YR",
"NRAUTH", "LEAD", "CAN", "DNK", "GRL", "USA", "NOR", "RUS", "OTH",
"ALL", "AB", "BB", "BS", "CS", "DS", "EG", "FB", "GB", "KB",
"KS", "LS", "LP", "MC", "NB", "NW", "SB", "SH", "VM", "WH", "GVT",
"NGO", "COM", "ACA", "CLB", "ZAM", "OTH.1", "UNK", "POPSTAT",
"POPABU", "POPTR", "BOUND", "HARV", "CC", "HAB", "HABP", "POLL",
"SHIP", "TOUR", "BEH", "REPEC", "ZOO", "PHYS", "TEK", "HWC",
"PRED", "METH", "DIS", "ANA", "POPGEN", "EVO", "RESIMP", "OTHER",
"PROT", "PA", "PEFF", "REVIEW", "funk", "FOTH", "fgroup",
"yrgroup"),.internal.selfref = <pointer: (nil)>, row.names =
class = c("data.table", "data.frame"))
You can use complete from tidyr to insert rows of missing combinations, say if agg_df is the aggregated data frame:
ggplot(tidyr::complete(agg_df, cat, time, fill = list(count = 0)),
aes(x = time, y = count, fill = factor(cat))) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_discrete(guide = guide_legend(title = "cat"))
An alternative is to build a new data.frame with the categories filled in.
# merge to filled in data.frame
dfNew <- merge(df, expand.grid(cat=sort(unique(df$cat)),
time=sort(unique(df$time))),
by=c("cat", "time"), all=TRUE)
# fill missing values with 0
dfNew[is.na(dfNew)] <- 0
In the first line, expand.grid is used to construct a "dummy" data.frame that contains all interactions of cat and time levels. This is merged with your orginal data.frame and all observations are maintained with the all=TRUE argument. The second line fills in the NA values with 0.
Then
ggplot(dfNew, aes(x = time, y = count, fill = factor(cat))) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_discrete(guide = guide_legend(title = "cat"))
results in the same figure as #psidom's answer.

How to make a regression with many data frames

I have data about gender and petitioning. I want to make a regression between the "Femme" (woman) variable and the different issues of the petitions. I have regrouped those issues into data frames under general themes, and those themes are what I want to regress with the "Femme" (woman) variable.
P.S.: Some petitions have many issues (ex.: water + science). So one petition could be counted in two data frames at the same time.
1) Here is what I did for all issues, this one is an example with the "Aboriginal" issue to show you how I coded the initial issues (you can also see the "Femme" variable at the beginning, which is already coded "0" and "1" in the original dataset under "Female"):
DataPetitions$Femme <- DataPetitions$Female
DataPetitions$Aboriginal <- NA
DataPetitions$Aboriginal[grepl("Aboriginal", DataPetitions$Issue)] <-1
DataPetitions$Aboriginal[!grepl("Aboriginal", DataPetitions$Issue)] <-0
# ... (same for all 24 specific issues)
2) Creating 7 data frames for general petitioning themes:
EnvironmentalIssues <- c(DataPetitions$AirQuality,DataPetitions$Biological, DataPetitions$Climate, DataPetitions$Environmental, DataPetitions$Toxic, DataPetitions$Waste, DataPetitions$Water)
EconomicIssues <- c(DataPetitions$Natural, DataPetitions$Transport)
SocialIssues <- c(DataPetitions$Aboriginal, DataPetitions$Health)
AgriculturalIssues <- c(DataPetitions$Agriculture,
DataPetitions$Fisheries, DataPetitions$Pesticides)
PoliticalIssues <- c(DataPetitions$Compliance, DataPetitions$Federal,
DataPetitions$Governance, DataPetitions$International)
ScientificIssues <- c(DataPetitions$Science)
OtherIssues <- c(DataPetitions$Other)
3) Trying to do a regression. This is my glm code:
model7 <- glm(DataPetitions$Femme ~ SocialIssues + PoliticalIssues +
ScientificIssues + EnvironmentalIssues + EconomicIssues +
AgriculturalIssues + OtherIssues, data = DataPetitions)
# When I try to run it, I get this error message:
Error in model.frame.default(formula = DataPetitions$Femme ~
SocialIssues + : variable lengths differ (found for
'SocialIssues')
With dput(head(DataPetitions,20)), I get this:
[...] class = "factor"), Femme = c(1, 1, 1, 0, 0, 0, 0, 0, 0,
0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1), AuMoinsUneFemme = c(1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1),
Homme = c(1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1,
1, 1, 1, 2), AuMoinsUnHomme = c(1, 0, 0, 0, 1, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1), Individual1 = c(0, 0, 0,
1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0), Group1 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1),
Organisation1 = c(1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0,
0, 0, 1, 0, 0, 0, 0), Aboriginal = c(1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0), Agriculture = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0),
AirQuality = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0), Biological = c(0, 1, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0), Climate = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1), Compliance = c(0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0),
Environmental = c(0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0), Federal = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Fisheries = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0), Governance = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Health = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0), International = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0), Natural = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0), Other = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Pesticides = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), Science = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Toxic = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Transport = c(0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Waste = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0), Water = c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Data.", "Title", "Number", "Issue", "Petitioner", "Individual", "Group", "Organisation",
"Female", "Male", "Unknown", "DateReceived", "Status", "Summary",
"Hyperlink", "Femme", "AuMoinsUneFemme", "Homme", "AuMoinsUnHomme",
"Individual1", "Group1", "Organisation1", "Aboriginal", "Agriculture",
"AirQuality", "Biological", "Climate", "Compliance", "Environmental",
"Federal", "Fisheries", "Governance", "Health", "International",
"Natural", "Other", "Pesticides", "Science", "Toxic", "Transport",
"Waste", "Water"), row.names = c(NA, 20L), class = "data.frame")

Resources