1 year ago
#342099
Quinn
Get warning: "longer object length is not a multiple of shorter object length" when standardizing coefficients in binomial glm using COEFS
I am using the range of my independent variables to standardize the coefficients in my binomial generalized linear model (Grace et al. 2018). I'm using the "glm" function to run my model. My independent variables are binomial factors and continuous numerics. The first nine rows of this data and code used to run the models are provided at the close of this question.
Several of my models have no problems, including a model with a binary independent variable (SEX). However, when I run models with a different binary independent variable (PP), I get the warning:
"Warning messages: In B * (sd.x/sd.y) : longer object length is not a multiple of shorter object length".
I've categorized PP as a factor with "Pre" and "Post"; I've coded it as a factor with "0" and "1". I get the error both ways and I don't know how this is any different than when I include 'SEX' in the other models. I verified that there are no NAs in 'PP' that would result in different sizes.
The model with a binary predictor that does work is disp.sex.hy.indv1c in code below.
The model that doesn't work is disp.sex.hy.indv4c in the code below.
This issue seems to be restricted to calculating standardized coefficients using the "coefs" function (piecewiseSEM package). I do not get this error when standardization = "none" and I do not get this error when I run the "summary" of my model. I'm not sure why the problem is "B * (sd.x/sd.y)", my understanding is that by using "range" to standardize, I'm avoiding the classic way of standardizing using standard deviations (SD).
My questions:
Why am I getting this error with model 4? The variable PP seems to be the issue but I don't know why. My other binary factor variable SEX seems to perform fine.
As a random side-question, why do I have to store my outcome variable as numeric 0/1? When stored as a factor and running "coefs" (both standardized and not), I get the error: "Error in emmeans::emmeans(model, specs = v, data = data) : No variable named DR in the reference grid".
First nine rows of data:
dataframe =
structure(
list(
ID = c(
"MSIMBS-659085366",
"MSIMBS803574472",
"MSIMBS1497710366",
"MSIMBS27387661",
"MSIMBS437446044",
"MSIMBS-797386402",
"MSIMBS1911522059",
"MSIMBS-215897866",
"MSI7433"
),
YR = c(2013,
2010, 2008, 2007, 2007, 2007, 2007, 2005, 1996),
PP = c(1, 1,
0, 0, 0, 1, 0, 1, 1),
REF = c(1191, 1195, 1173, 1184, 1188, 1194,
1182, 75, 44),
DR = c(0, 1, 0, 1, 0, 0, 1, 0, 0),
HI = c("MSI",
"MSI", "MSI", "MSI", "MSI", "MSI", "MSI", "MSI", "MSI"),
BI = c("MR",
"SINWR", "MR", "MR", "SINWR", "MR", "SINWR", "MR", "PMI"),
CDD = c(3,
2, 3, 3, 2, 3, 2, 3, 1),
CSD = c(1, 2, 1, 1, 2, 1, 2, 1, 4),
Evidence.of.breeding.dispersal = c("N", "N", "N", "N", "N",
"N", "N", "N", "N"),
Score = c(2, 2, 2, 2, 2, 2, 2, 1, 1),
Islands.visited.1 = c("MR", "SINWR", "PMI", "MR", "SINWR",
"MR", "PMI", "MR", "PMI"),
Islands.visited.2 = c(NA, NA,
"EER", NA, NA, NA, "SINWR", NA, NA),
Islands.visited.3 = c(NA,
NA, "MR", NA, NA, NA, NA, NA, NA),
Comments = c(
"observed once on MR at age 5",
"MSI at age 4; SINWR at age 5",
"PMI and EER at age 4; MR at age 5",
"observed on MR at age 5",
"observed on SINWR at age 4 and 5; MSI at age 4",
"MSI at age 4; MR at age 5",
"MSI and PMI at age 3; SINWR at age 5",
NA,
NA
),
Known.Breeder. = c(
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_
),
Known.Breeding.Island.Alpha = c(
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_
),
Breeding.Location = c(
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_
),
Meas.Year = c(2013, 2010,
2008, 2007, 2007, 2007, 2007, 2005, 1996),
IFD = c(235, 229,
212, 225, 217, 224, 225, 231, 228),
Fledge_Day_Correct = c(23,
17, 30, 13, 5, 12, 13, 19, 15),
Fledge_Month_Correct = c(8,
8, 7, 8, 8, 8, 8, 8, 8),
Fledge_Year = c(2013, 2010, 2008,
2007, 2007, 2007, 2007, 2005, 1996),
Fledge_Date = structure(
c(
1377292500,
1282082400,
1217451600,
1187042400,
1186316700,
1186956000,
1187042400,
1124487000,
840147000
),
tzone = "UTC",
class = c("POSIXct",
"POSIXt")
),
Culmen.mm = c(NA, 32.1, NA, 29.6, 31.8, 29.9,
33, 30.2, 31),
Feather.Score = c(2, 1, NA, 2, 1, 2, 2, 1,
1),
`Head-Bill.mm` = c(NA, NA, NA, 67.7, 68.2, 66.7, 71.2,
68.1, NA),
Weight.g = c(145, 210, 262, 330, 295, 270, 330,
255, 290),
Wing.Chord.mm = c(116, 126, 130, 136, 136, 136,
139, 139, 140),
DA = c(5, 5, 5, 5, 5, 5, 5, 6, 4),
NIP = c(1,
1, 3, 1, 1, 1, 2, 1, 1),
BCI = c(
-106.354370537169,
-80.1668007850088,-70.2816508629798,
-39.8096925166307,
-35.5874524012305,-32.571566604516,
-18.3298570846523,
-14.1639804745041,
-10.1488794987746
),
BS = c(0.15, 0.66, 0.62, 0.38, 0.38, 0.38, 0.38, 0.82,
0.54),
LGM = c(
"4.4000000000000004",
"4.8",
"5",
"5.9",
"5.9",
"5.9",
"5.9",
"3.3",
"5.3"
),
LGW = c(
"2.5",
"3",
"3.2",
"3.2",
"3.2",
"3.2",
"3.2",
"2.2999999999999901",
"2.2000000000000002"
),
PH = c(188, 172, 167, 176, 176, 176, 176, 184, 170),
Peak.Hatching.SD = c(8.1,
6.66, 6.2, 4.8, 4.8, 4.8, 4.8, 8.3, 6.6),
MFD = c(230, 222,
214, 218, 218, 218, 218, 226, 222),
JR = c(140.9, 118.9,
46.6, 52.3, 52.3, 52.3, 52.3, 71, 90.8),
JYR = c(
111.6,
111.2,
98.8,
143.4,
143.4,
143.4,
143.4,
68.0999999999999,
126
),
MSST = c(7.9, 8.8, 8.4, 7.3, 7.3, 7.3, 7.3, 8.4, 6.6),
JSST = c(
9.4,
9.5,
9.69999999999999,
9.19999999999999,
9.19999999999999,
9.19999999999999,
9.19999999999999,
9.6,
8.5
),
YSST = c(11.18,
11.4, 11.6, 10.3, 10.3, 10.3, 10.3, 9.4, 10.1),
WSST = c(
9.00499999999999,
7.5625,
6.79749999999999,
7.90499999999999,
7.90499999999999,
7.90499999999999,
7.90499999999999,
6.46,
7.04875
),
PHQ = c(
0.213130828154496,
0.558283058154208,
0.77098783216658,
0.410042625941335,
0.410042625941335,
0.410042625941335,
0.410042625941335,
0.104932861874709,
0.881615930442605
),
MHQ = c(
1.87816424779674,
4.29675845779791,
4.76746565483757,
3.54148462268968,
3.54148462268968,
3.54148462268968,
3.54148462268968,
0.402116165959344,
2.47670999592492
),
PLQ = c(
0.0211869058772459,
0.288244013114959,
0.0506968468600496,
0.390833165973166,
0.390833165973166,
0.390833165973166,
0.390833165973166,
0.404771422072388,
0.040190677191377
),
MLQ = c(
0.186704520808378,
2.21843540328106,
0.313488055362235,
3.37557502504414,
3.37557502504414,
3.37557502504414,
3.37557502504414,
1.55113593040094,
0.112907047735517
),
Pop.Size = c("NA", "NA", "NA", "NA", "NA", "NA", "NA",
"NA", "NA"),
Occupancy = c(
"0.75",
"0.76",
"0.81",
"0.74",
"0.74",
"0.74",
"0.74",
"0.78",
"0.8"
),
Total.mass.all.prey.items = c(
8213.02621541773,
3671.17317001492,
2430.14730477141,
2781.07195779501,
2781.07195779501,
2781.07195779501,
2781.07195779501,
1766.6110424835,
457.9133785991
),
Total.Feedings.all.prey.items = c(932, 477, 393, 322,
322, 322, 322, 461, 163),
Total.Mass.per.Total.Feedings = c(
8.81225988778727,
7.69637981135203,
6.18358092817152,
8.636869434146,
8.636869434146,
8.636869434146,
8.636869434146,
3.83212807480153,
2.80928453128282
),
Proportion.of.Moderate.Prey = c(
0.765682265968258,
0.153472928730833,
0.17831532097337,
0.199124208085499,
0.199124208085499,
0.199124208085499,
0.199124208085499,
0.490295716052903,
0.0781933923660182
),
Band_Number = c(
"1055-08758",
"1015-10894",
"1015-15506",
"1905-19615",
"1905-19743",
"1905-19613",
"1905-19630",
"1015-08747",
NA
),
SEX = c("F", "M", "F", "M", "F", "M", "F", "M", "F"),
HOW = c("pcr", "pcr", "pcr", "pcr", "pcr", "pcr", "pcr",
"pcr", "DFA"),
SexingNotes = c(
"2020 Quinn Carvey",
"2020 Quinn Carvey",
"2020 Quinn Carvey",
"2020 Quinn Carvey",
"2020 Quinn Carvey",
"2020 Quinn Carvey",
"2020 Quinn Carvey",
"2020 Quinn Carvey",
NA
)
),
row.names = c(NA,-9L),
class = c("tbl_df", "tbl",
"data.frame")
)
Subsequent code to run the models and calculate the coefficients:
#convert SEX to factor
dataframe$SEX=as.factor(dataframe$SEX)
#convert PP to factor
dataframe$PP=as.factor(dataframe$PP)
install.packages("piecewiseSEM")
library(piecewiseSEM)
install.packages("multcompView")
library(multcompView)
#need this to include a factor in my logistic regression
#two of my binomial GLMs
#IFD and BCI are continuous numeric variables
#SEX and PP are binary factor variables
disp.sex.hy.indv1c = glm(DR ~ IFD + SEX,
data = dataframe,
family = binomial(link="logit"),
na.action(na.omit))
disp.sex.hy.indv4c = glm(DR ~ BCI + IFD + PP,
data = dataframe,
family = binomial(link="logit"),
na.action(na.omit))
#raw coefficients
coefs(disp.sex.hy.indv1c, standardize = "none")
coefs(disp.sex.hy.indv4c, standardize = "none")
#standardized coefficients
coefs(disp.sex.hy.indv1c, standardize = "range", standardize.type = "latent.linear")
coefs(disp.sex.hy.indv4c, standardize = "range", standardize.type = "latent.linear")
r
glm
coefficients
0 Answers
Your Answer