Impute Quarterly data rows - r

First time doing TS and I want to calculate a time-series regression but first have to convert my data frame to a ts frame. The problem is, that I have missing quarters, so the ts conversion fails. How can I impute quarters and respective values to get a full ts frame?
Example with quarter 1981.3 missing:
1 1981.1 1.2 0.00000000 0 7.881275
2 1981.2 -0.2 1.17870604 0 7.676712
3 1981.4 -0.1 0.00000000 0 7.333129
4 1982.1 0.4 0.00000000 0 7.266816
This is the dput.
structure(list(Quarter.y = c(1981.1, 1981.2, 1981.4, 1982.1,
1982.4, 1983.4, 1984.1, 1984.3, 1984.4, 1985.2, 1985.4, 1986.1,
1986.4, 1987.2, 1987.4, 1988.2, 1988.4, 1989.2, 1989.4, 1990.1,
1990.2, 1990.4, 1991.2, 1991.4, 1992.2, 1992.4, 1993.2, 1993.3,
1993.4, 1994.1, 1994.2, 1994.3, 1995.1, 1995.2, 1995.3, 1995.4,
1996.1, 1996.2, 1996.4, 1997.1, 1997.2, 1997.4, 1998.2, 1998.4,
1999.1, 1999.2, 1999.4, 2000.1, 2000.2, 2000.3, 2000.4, 2001.1,
2001.2, 2001.3, 2001.4, 2002.1, 2002.3, 2002.4, 2003.1, 2003.3,
2003.4, 2004.1, 2004.2, 2004.4, 2005.1), GDPGrowth = c(1.2, -0.2,
-0.1, 0.4, 0.2, 1.4, 1.3, 2.7, 0.8, 1, 0.6, -0.6, 1, 2.2, 1.6,
1.8, 1.2, 0.4, 1.2, 2.1, 0.5, 1.8, -0.5, 1.3, -0.7, -0.3, 0,
0.6, -0.1, 1.4, 0.3, 0.7, -0.3, 0.8, 0.2, 0, -0.8, 1.4, 0.7,
-0.5, 1.3, 0.7, -0.4, -0.4, 1.2, 0, 0.8, 1.4, 0.8, 0, -0.3, 2,
0, -0.2, -0.2, -0.5, 0.5, -0.2, -1.5, 0.8, 0.3, -0.2, 0.5, -0.1,
0), AverageCONS = c(0, 1.17870603993396, 0, 0, 0, 0, 3.61936244127144,
0.416666666666667, 0, 4.32707915240231, 7.6364088578926, 0.257076257076257,
0, 22.4207759459411, 8.04871523114194, 14.6555459609091, 16.2250782932878,
1.17084307021898, 6.78706870557528, 0, 0, 9.7539118534683, 0,
0, 0, 0.0402095172505245, 0, 0, 0.0636265006342972, 0, 0.171974252305606,
0, 11.1524740312643, 2.68040672020172, 6.2111801242236, 3.24760735460988,
28.2976799963101, 0, 7.5732270977962, 0, 0, 1.49412278319752,
70.2064896755162, 34.0042105697558, 18.5823772614653, 18.0896275972026,
8.41449577357745, 10, 0, 0, 34.7491138493683, 8.36236933797909,
39.6563615833003, 74.4262295081967, 22.3611248302746, 10, 22.911760840126,
0, 0.0666722800439236, 0, 50.3843726943174, 0, 0, 1.72909969128655,
0), BRGOVMEHR = c(0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), ApprovalGOV = c(7.88127469343306, 7.67671239967451,
7.33312916270801, 7.26681550104175, 7.57774047678561, 7.44440610328638,
7.51950710910292, 7.43455364650782, 7.21735906430465, 6.93572597869877,
7.33281216290814, 7.34110311728404, 7.81953413874152, 7.78638539397364,
7.5580444419889, 6.70433786365357, 6.76698016084029, 6.9932335657093,
7.89148017350285, 8.24705859843768, 8.20161269191644, 6.13021112846596,
7.44587363057623, 7.48928443049876, 6.66476678973035, 6.48625864551226,
6.13914369458924, 6.05634679973895, 5.9702909369734, 6.19216550005443,
6.87967122943963, 7.49940214266322, 7.08991191311255, 7.3351806925688,
7.46762039999888, 7.22336518577119, 6.75192112299076, 6.61614229895973,
6.39396085192013, 6.09682321355397, 5.99711627005931, 5.95861508444216,
6.31451929735713, 6.07326509257996, 7.58677238161551, 7.24041796080827,
6.69547440053917, 7.72437292977251, 7.61985191697131, 7.85861327446016,
7.78974162557168, 8.00182694049075, 7.82019060836613, 7.58946475073855,
7.89751118735182, 7.14978411180804, 7.4578134112501, 6.24455242517448,
5.8823776113788, 6.08103241246385, 5.73879250743035, 5.68128370028589,
5.83312282222293, 6.49992542902688, 6.45920800878159)), row.names = c(NA,
-65L), class = c("tbl_df", "tbl", "data.frame"))

If the input is dat then convert that to a zoo object z having a yearqtr index. Converting z to a ts object will insert NAs for the missing entries and then we can use na.approx on that to fill in those NAs.
library(zoo)
z <- read.zoo(dat, FUN = function(x) as.yearqtr(paste(x), "%Y.%q"))
na.approx(as.ts(z))
EDIT
Simplified slightly.

The code below uses the tidyverse packages to create a new dataframe with 4 rows per each year in your dataset and then uses left_join to join your original dataset to that template. This will return NA values for missing quarters in your original data.
EDIT: removed assigning data. Data provided is in the df object.
library(tidyverse)
yrs <- df %>%
mutate(year = str_replace_all(Quarter.y, "\\.\\d{1}$", "")) %>%
distinct(year) %>%
as_vector()
impute <- tibble(yr = as.integer(rep(yrs, 4))) %>%
group_by(yr) %>%
mutate(qtr = row_number()) %>%
ungroup() %>%
arrange(yr, qtr) %>%
mutate(yr_qtr = paste0(yr, ".", qtr) %>%
as.numeric()) %>%
left_join(df, by = c("yr_qtr" = "Quarter.y"))
As far as imputing missing values goes, I'm not sure what the right approach here is -- it will depend on what patterns you expect to see/what a reasonable default value is.

Related

Aggregate similar constructs/ FA with binary variables

I would like to aggregate, in order to reduce the number of constructs, its following data frame containing only binary variables that correspond to "yes/no", its following data frame (first 10 row). The original data frame contains 169 rows.
outcome <-
structure(list(Q9_Automazione.processi = c(0, 0, 0, 0, 0, 0,
1, 1, 1, 0), Q9_Velocita.Prod = c(1, 0, 0, 1, 0, 0, 1, 1, 1,
0), Q9_Flessibilita.Prod = c(0, 0, 0, 1, 0, 0, 1, 1, 0, 1), Q9_Controllo.processi = c(0,
0, 0, 1, 0, 0, 1, 1, 0, 0), Q9_Effic.Magazzino = c(0, 0, 0, 1,
0, 0, 0, 0, 0, 0), Q9_Riduz.Costi = c(0, 1, 0, 0, 0, 0, 0, 0,
0, 1), Q9_Miglior.Sicurezza = c(0, 0, 0, 0, 0, 0, 1, 0, 1, 1),
Q9_Connett.Interna = c(0, 0, 0, 0, 0, 0, 0, 1, 1, 0), Q9_Connett.Esterna = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), Q9_Virtualizzazione = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0), Q9_Innov.Prod = c(0, 0, 0, 0, 0,
1, 0, 0, 0, 1), Q9_Person.Prod = c(0, 1, 0, 1, 0, 1, 0, 0,
0, 1), Q9_Nuovi.Mercati = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Q9_Nuovi.BM = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q9_Perform.Energ = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), Q9_Perform.SostAmb = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 10L), class = "data.frame")
I have tried performing factor analysis via the tethracoric method on the obtained correlation matrix ( the obtained value from the KMO function turns out to be inadequate) both directly on the dataframe and then using tethracoric correletions in fafunction (using cor = "tet" I get a negative Tucker Lewis Index).
I have been reading up on this but cannot find a methodology that is adequate and of which I am certain of the correctness of the analysis.
So basically what I would like to achieve is to aggregate similar constructs, e.g., assess whether column 5 has value 1 (i.e., "yes") almost always when column 11 has value 1 and then aggregate.
Here the code that I try to used
library(psych)
tet <- tetrachoric(outcome)
corrplot(tet$rho, "ellipse", tl.cex = 0.75, tl.col = "black")
par(mfrow = c(1,2))
corr_matrix %>%
ggcorrplot(show.diag = F,
type="lower",
lab=TRUE,
lab_size=2)
KMO(corr_matrix)
cortest.bartlett(corr_matrix)
fa.parallel(corr_matrix, fm = "ml")
factor <- fa(corr_matrix, nfactors = 3, rotate = "oblimin", fm = "ml")
print(factor, cut = 0.3, digits = 3)
# -------- Pearson --------
cor(outcome, method = 'pearson', use = "pairwise.complete.obs") %>%
ggcorrplot(show.diag = F,
type="lower",
lab=TRUE,
lab_size=2)
KMO(outcome)
cortest.bartlett(outcome)
fa.parallel(outcome)
factor1 <- fa(outcome, nfactors = 3, rotate = "oblimin", cor = "tet", fm = "ml")
print(factor1, cut = 0.3, digits = 3)

Predictive models with different sized time series

I'm trying to estimate an ARIMA, VAR and Neural Network models in R, to compare the quality of their predictions regarding unemployment, using the data regarding several economic indicators and COVID-19 related information which i would like to use as exogenous information. I would also like these models to use the rolling window approach, if possible.
My economic information extends from 2007 to 2021 (monthly) while the COVID data only comes from 2020, which obviously poses problems regarding the size of the time series.
Since i am trying to use rolling windows, every time I try to include COVID information i get the following error.
Error in solve.default(Sigma) :
system is computationally singular: reciprocal condition number = 2.6396e-19
Which I assume is because the covid information is all 0's (since we are using data from 2007) and therefore the model assumes a perfect correlation for that information.
Is there any way I can combine these 2 time series (the economic one from 2007 and the covid one) in the models mentioned, while using the rolling window approach?
dput version
structure(list(date = c("02/2007", "03/2007", "04/2007", "05/2007",
"06/2007", "07/2007", "08/2007", "09/2007", "10/2007", "11/2007",
"12/2007", "01/2008", "02/2008", "03/2008", "04/2008", "05/2008",
"06/2008", "07/2008", "08/2008", "09/2008", "10/2008", "11/2008",
"12/2008", "01/2009", "02/2009", "03/2009", "04/2009", "05/2009",
"06/2009", "07/2009", "08/2009", "09/2009", "10/2009", "11/2009",
"12/2009", "01/2010", "02/2010", "03/2010", "04/2010", "05/2010",
"06/2010", "07/2010", "08/2010", "09/2010", "10/2010", "11/2010",
"12/2010", "01/2011", "02/2011", "03/2011", "04/2011", "05/2011",
"06/2011", "07/2011", "08/2011", "09/2011", "10/2011", "11/2011",
"12/2011", "01/2012", "02/2012", "03/2012", "04/2012", "05/2012",
"06/2012", "07/2012", "08/2012", "09/2012", "10/2012", "11/2012",
"12/2012", "01/2013", "02/2013", "03/2013", "04/2013", "05/2013",
"06/2013", "07/2013", "08/2013", "09/2013", "10/2013", "11/2013",
"12/2013", "01/2014", "02/2014", "03/2014", "04/2014", "05/2014",
"06/2014", "07/2014", "08/2014", "09/2014", "10/2014", "11/2014",
"12/2014", "01/2015", "02/2015", "03/2015", "04/2015", "05/2015",
"06/2015", "07/2015", "08/2015", "09/2015", "10/2015", "11/2015",
"12/2015", "01/2016", "02/2016", "03/2016", "04/2016", "05/2016",
"06/2016", "07/2016", "08/2016", "09/2016", "10/2016", "11/2016",
"12/2016", "01/2017", "02/2017", "03/2017", "04/2017", "05/2017",
"06/2017", "07/2017", "08/2017", "09/2017", "10/2017", "11/2017",
"12/2017", "01/2018", "02/2018", "03/2018", "04/2018", "05/2018",
"06/2018", "07/2018", "08/2018", "09/2018", "10/2018", "11/2018",
"12/2018", "01/2019", "02/2019", "03/2019", "04/2019", "05/2019",
"06/2019", "07/2019", "08/2019", "09/2019", "10/2019", "11/2019",
"12/2019", "01/2020", "02/2020", "03/2020", "04/2020", "05/2020",
"06/2020", "07/2020", "08/2020", "09/2020", "10/2020", "11/2020",
"12/2020", "01/2021", "02/2021", "03/2021", "04/2021", "05/2021",
"06/2021", "07/2021", "08/2021", "09/2021", "10/2021", "11/2021"
), unemp = c(2.25752508, -1.0989011, 2.1978022, -2.1978022, -1.0989011,
-0.05972289, -1.08695652, -6.43115942, -1.00945017, 0, -1.07388316,
-3.04982818, -1.96717862, 1.96717862, -1.87441425, 2.90534208,
3.06056701, -2.02963918, 3.07130584, 2.08333333, 0, 0, 5.43478261,
5.6763285, 3.49563046, 2.05992509, 3.33333333, 0.87912088, 0.62625546,
4.58158018, -0.55504163, 1.06382979, 0, -1.06382979, -2.85150252,
-4.68041237, 0.62745098, -3.38935574, 0.5026455, -0.28619529,
-1.29525342, -2.95239548, -1.03740503, -1.76613018, -2.5210084,
2.59222333, -1.7518872, 1.7518872, -2.6894523, 1.65289256, -3.43801653,
-0.8503937, -2.3744125, 0.7751938, 2.29576625, 3.90578414, 4.94647471,
1.3640873, 4.04773622, -0.92273622, 3.125, 0.45673077, 3.84615385,
-0.95126248, 1.72049325, 0.56958309, 0.71246819, -3.64508393,
0.92207924, -2.02585375, -1.25114155, 0.66666667, -3.8008658,
-2.882786, -4.77313832, 0, -2.51458002, -1.30135713, -7.81499203,
-1.75438596, -3.87759171, -2.16419816, -3.70910671, -2.12494688,
0.46748831, -0.63842848, 0.38701623, -1.12359551, 1.43499197,
-1.14285714, -0.07142857, 0.31385281, 1.64714631, 0.44583317,
2.8365708, 2.38646704, -1.2987013, -1.44395079, -1.39857093,
-1.51117827, -0.67114094, 1.15965849, 3.67636416, 2.73483341,
-1.37718397, -0.78108941, -1.36176773, -2.05167173, 1.97459054,
1.16173728, -0.21885522, 0.47348485, -0.86122047, -0.78740157,
-0.78740157, -1.36727095, -1.66545543, 0.55803571, -3.31428571,
1.3398374, -3.09467611, 0.50237969, 0.36506406, 0.38394668, 0.55958863,
-1.04021115, -1.05878635, -1.78571429, 0.327654, -2.97315664,
2.71485077, -0.97087379, -0.66990291, -0.23232323, -1.50994481,
0.53174173, -1.32138858, 1.61887142, 2.80193237, -1.61616162,
1.79704017, 3.85706183, -0.21077989, 1.03395062, 1.96428571,
1.12781955, 4.93871665, 1.27473364, 2.65873016, -1.42857143,
-2.73641851, 6.98011599, -1.47058824, 2.94117647, 1.49253731,
1.49253731, -2.98507463, -4.54545455, 0.06784261, -5.97014925,
22.3880597, 10.78697422, 3.42657343, -6.75086108, -4.25146992,
-9.26916221, -4.41176471, -0.02131287, 3.09617918, 0.21645022,
4.61309524, 7.29166667, -26, -9.18518519, -4.65221319, 3.17073171,
4.21052632, 3.28947368), regist_un = c(-7.6, -8.1, -10.4, -13,
-12.2, -10.8, -10.2, -11.3, -12, -13.2, -13.8, -12.7, -11.6,
-11.4, -8.2, -3.6, -1.6, -2, -0.5, -0.7, 0.5, 2.9, 6.6, 12.1,
17.7, 23.8, 27.3, 27.6, 28.1, 30.1, 28.7, 29.1, 29.1, 28.2, 26.1,
25.1, 19.6, 18.1, 16.1, 14.6, 12.7, 10.3, 9.6, 8.9, 6.4, 4.4,
3.3, -0.5, -1, -3.5, -5, -5.4, -6, -4.4, -3, -0.3, 3, 6.7, 11.7,
14.4, 16.6, 19.8, 21, 20.8, 24.5, 25, 26.3, 23.4, 22.5, 19.6,
17.4, 16.05866431, 14.1343296, 11.04394749, 11.07092871, 9.66638699,
6.80821419, 4.99845882, 3.21403698, 2.00992748, -0.01381295,
-0.82689753, -2.83078075, -4.69352568, -5.22666645, -6.07571945,
-8.30308904, -9.4986526, -10.86351863, -11.10348947, -10.1911332,
-11.56954866, -12.86335954, -13.57419377, -13.31634168, -12.71367749,
-13.78692468, -14.3833581, -14.16732657, -12.93820022, -12.73630773,
-12.91458502, -14.04113868, -12.63480706, -10.48461147, -7.99771938,
-7.25281958, -7.35380587, -4.68547808, -2.62950703, -1.82217091,
-3.44938365, -4.66108643, -6.57689723, -7.04795734, -8.83698741,
-9.49043411, -11.59763744, -13.07912754, -13.26308777, -15.34204052,
-18.01521541, -19.89096413, -19.19477791, -18.26531051, -16.35403878,
-16.14554408, -16.34837215, -17.53504461, -16.81810893, -16.32660251,
-16.00691286, -17.02626382, -16.57334233, -16.61939724, -18.99258341,
-20.51560419, -20.5844694, -19.149043, -17.49773014, -17.38241663,
-17.23274637, -16.0328503, -15.58626266, -15.29940386, -15.14205448,
-14.5670108, -12.85161091, -10.29016682, -10.0720839, -10.00068018,
-11.10920973, -10.23871997, -8.6402685, -8.42184435, -8.61357235,
-7.91941687, 2.99152725, 22.12769269, 34.00159255, 36.37735545,
37.00494467, 34.50234942, 36.14288275, 34.50948107, 30.17574135,
29.55791318, 32.38134753, 36.84886013, 25.91626159, 8.04566645,
-1.65087765, -7.08027492, -9.47650638, -9.99850976, -12.4400864,
-12.85751101, -13.15709526), electr_cons = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1.52573055, -13.77356375, -2.91314706, -8.07503923,
5.64741668, 2.14652942, 8.96942648, -12.90473041, 6.7462353,
-1.99320589, 3.5519951, 3.96085785, 2.90812218, -3.01852589,
-0.72990857, 6.62024931, -4.03278316, 15.15581887, -5.80240675,
6.15355971, -8.9434497, 7.09174981, -3.03231017, 5.99309871,
0.12505412, 7.98920263, 4.97957359, -13.41437565, 3.1251824,
-19.61649962, 8.42404224, 3.9274678, -0.69310101, -6.20882422,
2.92246113, 0.84784002, -5.54698038, -0.34784001, -2.23860664,
2.52721219, 1.51556136, 0.11030442, -6.11617537, -0.35384086,
3.73968268, 1.36305933, -4.2866283, -3.01881185, 8.07963944,
5.51292808, -4.35131302, 3.6602547, -6.44713838, 1.39859892,
1.30415104, -1.60930464, -0.40894902, -0.56603623, 3.40891108,
-1.80100326, -5.44803444, -6.44187786, 38.45132631, -4.07719031,
0.61965483, -1.49863173, 3.30109363, 1.19224911, -2.92700391,
16.92568044, -15.15157455, 3.48316945, -1.00483719, -40.57315028,
9.7659042, 4.78461676, 0.28264713, -0.87145969, -3.86558313,
-0.0679219, 2.45947602, -9.41849532, 8.07657479, 3.93419348,
1.94861355, 52.56477551, -54.55955271, -3.99890956, 4.60669488,
1.67468875, 0.32376412, -0.80237405, -4.03956352, 1.8608071,
-0.74354869, -6.79369666, -2.74137491, 5.6219074, 5.13307485,
2.09278167, -8.97792005, 0.23895157, 2.34350504, 2.01565156,
0.34461909, -5.39315134, 3.70375643, 1.35911336, 0.14440375,
-10.59505494, 2.44997231, -3.43575023, 8.45149168, 1.82580942,
-5.09746181, 0.54849722, -0.40588822, 2.90913923, -2.59092777,
2.87179578, 3.5472478, 5.50635214, 3.45884957, 0.5574311, -9.17310051,
-2.3069345, 1.01792404, 3.39829408, 2.18975374, -3.29888986,
3.36520805, -6.06857013, 3.69912561, -5.88831257, -5.59447201,
6.93463825, 5.18518235, -4.45047426, 3.48886956, -4.22410882,
0.36229398, 4.38850288, 2.44283293, -4.13337598, 0.07853854,
0.33111425, -1.50291612, -10.73449339, -2.31367763, 6.06925576,
7.25078064, -1.2508886, 0.69265168, -1.6952393, -5.45510355,
10.36136532, 0.81292421, -5.78213732, 0.29608406, 8.58995306,
3.33976066, -16.05532167, 4.59091993, 2.96362702, -1.21232785,
0.44224015, 4.78743418), inflation = c(-0.3, 0.1, 0.4, -0.4,
0, -0.1, -0.4, 0.1, 0.5, 0.3, -0.1, 0.2, 0, 0.3, -0.7, 0.3, 0.6,
-0.3, 0, 0.1, -0.7, -1.1, -0.6, -0.7, 0, -0.7, 0.1, -0.8, -0.3,
0.1, 0.3, -0.6, 0.2, 0.8, 0.7, 0.2, 0.1, 0.4, 0.1, 0.4, 0, 0.8,
0.1, 0, 0.3, -0.1, 0.2, 1.2, -0.1, 0.4, 0.1, -0.3, -0.4, -0.3,
-0.2, 0.7, 0.5, -0.2, -0.3, -0.2, 0.2, -0.4, -0.2, -0.2, 0, 0.1,
0.3, -0.2, -0.8, -0.2, 0.2, -1.7, -0.2, 0.5, -0.3, 0.5, 0.3,
-0.4, -0.6, 0.1, -0.3, 0.1, 0.1, -0.1, -0.2, -0.3, 0.3, -0.2,
0.1, -0.5, 0.6, 0.1, 0.1, 0, -0.4, -0.1, 0.3, 0.5, 0.1, 0.5,
-0.2, -0.1, 0, 0.2, -0.2, -0.1, -0.3, 0.4, -0.5, 0.3, 0, -0.1,
0.3, 0, 0.1, -0.1, 0.4, -0.6, 0.4, 0.4, 0.3, -0.2, 1, -0.7, -0.7,
0, 0.3, 0.3, 0.3, -0.1, -0.2, -0.5, -0.4, 0.1, -0.5, 1.1, 0.6,
0.2, -0.9, 0.5, -1, 0.1, -0.3, 0, 0.3, -0.1, 0.1, -0.6, 0.4,
-1.4, 0.6, -0.2, 0.2, 0.3, 0.2, 0.4, -0.3, -0.4, -0.2, -0.5,
0.8, -0.3, -0.1, -0.6, 0.2, 0.2, 0.1, 0.5, 0.1, -0.2, -0.2, 0.6,
-1.1, 1.7, 0.2, 0, 0.5, 0.8), indust_prod = c(-2.572274344, -2.787547763,
2.430571433, -3.345301558, -0.463876872, -4.282068938, -1.120792803,
1.572826809, 5.587675149, -4.689662112, -4.039000653, 6.048760143,
3.054565546, -8.324475243, 8.938629302, -8.359749373, 1.285898904,
2.787686994, -1.18689554, -1.044345864, -5.377152698, 0.417765217,
-2.016437912, -6.076899004, 0.004832982, 6.553009253, -3.861674081,
2.856277, -0.305740286, 3.884000992, 0.228026533, 3.160838283,
0.718539054, -0.177278754, 5.861419366, 1.635627332, -0.233559319,
1.656666214, -3.416861953, 3.08593964, 0.219887065, -5.252665393,
2.67414747, -3.078208264, -1.786603334, 5.055713134, -0.487827087,
-1.085759889, 1.558281917, 1.522276844, -0.649720104, -2.25624012,
-3.000400858, 1.287503915, 2.146782202, -2.414634004, -0.710817622,
-2.037943864, -3.422580841, 2.574492816, -0.989720019, -3.140953611,
-2.00903906, 5.044475953, 2.421105335, 1.312884435, -0.423167539,
-1.618876184, 0.749480391, -0.067574147, 0.665361763, 2.736011629,
-0.984538393, 2.734504118, 3.689697632, -4.285546756, -1.453623444,
-3.036406761, 0.371745852, 5.268465577, 2.014992273, 0.230769423,
1.479245498, -0.601620521, 0.435743581, -6.919825179, 3.973589167,
-1.18054296, 1.009209474, 3.4645508, -4.268359859, -1.38055936,
1.398352479, -1.380441077, -0.945043656, -3.070867046, 2.286824653,
4.15932548, -0.885666142, 1.66749454, 1.079797346, 1.268988373,
-3.757792572, 3.297688911, 0.757515843, -3.344176468, 0.937671829,
1.942358369, -0.363516768, -2.258151209, 1.602633167, -3.43640148,
1.319433782, -3.471816444, 6.680682523, -3.215319966, -4.672113236,
6.216354428, 0.969772263, -0.213391145, -3.34142316, 5.648021789,
-8.289444236, 8.300362011, -2.663437754, 3.039228136, 3.239437295,
-6.66030595, 1.185659472, -1.499248124, -3.292614657, 2.681309878,
-0.579981109, 0.867209022, 2.022041722, -6.479958297, 1.680604654,
-0.873272771, -2.875794511, 3.56198993, 0.706252639, -3.546626564,
1.706203079, -1.238940266, 0.694831062, -4.576557295, 5.589573409,
1.887484409, -6.283451003, 3.968287705, -4.024275523, -0.142379113,
3.389769739, 5.34662785, -1.707063421, -1.962359729, 1.648794981,
-5.778920692, -8.801594747, 6.391085381, 3.735161238, 0.512586559,
1.347387952, 0.083401305, -0.958210945, 0.28448621, -0.398402291,
0.174892606, -2.923332835, 9.40208252, 8.440250585, -5.118029161,
-4.652533609, -1.031496063, 0.253718285, 0.116959064, -0.767472053,
-2.656293514), construction = c(-5.97969161, -4.79269684, -5.74581431,
-5.80595875, -3.91928599, -4.68384075, 2.11180124, -2.08333333,
-2.12510024, -2.80786201, -2.94938222, -2.5189924, -3.04, -3.39592489,
-2.50302786, -4.54176805, -5.77544426, -5.52825553, 2.83860503,
-6.58756137, -7.37402704, -6.9748246, -9.32238193, -11.64889253,
-10.35478548, -10.21505376, -11.09730849, -8.41121495, -8.65837977,
-11.66016472, -15.14195584, -9.46123522, -8.97832817, -9.40550133,
-10.00905797, -8.58867224, -10.86056144, -11.60755412, -10.75919888,
-11.03896104, -12.57625528, -8.83218842, -15.24163569, -12.48185776,
-12.97376093, -13.36924584, -11.67589331, -11.93499238, -10.22199277,
-11.04742053, -10.96033403, -13.3472367, -11.27214171, -12.86329386,
-14.03508772, -13.0458817, -14.40536013, -15.65856416, -14.52991453,
-13.49480969, -15.12363427, -15.58289397, -18.22977726, -17.74969916,
-16.57592257, -16.86226065, -14.66836735, -16.52892562, -16.1121983,
-15.8847185, -16.46666667, -19.2, -19.85094851, -17.4184594,
-16.27240143, -14.55742502, -15.88107324, -15.15601783, -15.8445441,
-16.22239147, -15.08553655, -13.14741036, -14.44533121, -14.68646865,
-11.3271344, -14.45378151, -9.93150685, -11.81506849, -10.94827586,
-10.07005254, -8.25932504, -6.18181818, -6.04395604, -6.05504587,
-3.91791045, -0.87040619, -4.09914204, 0.09823183, -3.8973384,
-2.23300971, -2.80735721, -2.72638754, -1.839303, -3.87596899,
-3.89863548, -4.4921875, -6.69902913, -5.75609756, -3.77733598,
-5.69185476, -3.85756677, -4.27010924, -5.37848606, -3.5035035,
-6.01577909, -3.83064516, -2.63691684, -2.04498978, 0.52029136,
3.10559006, 2.58264463, 1.144641, -0.30864198, 0.20746888, 2.31578947,
1.34854772, 2.51836306, 2.72536688, 1.66666667, 1.67014614, 3.51966874,
2.10843373, 1.91339376, 3.08641975, 3.81836945, 4.24430642, 3.90946502,
3.48004094, 4.81064483, 4.18367347, 4.4057377, 3.28542094, 1.7,
1.08161259, 1.97628458, 3.79241517, 5.96421471, 3.17775571, 2.27722772,
3.26409496, 2.44140625, 1.76297747, 2.15897939, 8.41012196, -0.16302847,
0.76178712, -4.69372663, -17.51199295, -45.16320079, -36.28914654,
-16.06074568, -4.87295907, 7.5668007, 3.72748757, 7.02862225,
3.38149083, 1.75109309, 4.74865906, 14.84745984, 34.803423, 100.52114863,
67.60757235, 30.44713435, 18.17227737, 11.63694169, 9.69843425,
4.74314833, -4.52314658), exports = c(24.96318942, 23.36969334,
22.92995604, 10.65726657, 16.4160009, 15.06347523, 7.05135338,
15.02299212, 19.0326442, 18.98558329, 3.33296617, 23.07105097,
24.1601689, 19.72166595, 27.16602809, 24.3702412, 14.56065717,
23.81003502, 12.51014556, 1.73165902, -13.84780944, -22.58391171,
-20.80753018, -33.06565254, -36.85899284, -37.79190796, -39.34913459,
-33.38661304, -27.85334908, -28.37492746, -23.27801369, -13.6431818,
6.74411365, 11.84361899, 13.51797098, 20.8751474, 22.95402278,
28.37464897, 21.54264152, 8.61516289, 3.13570326, 4.20220684,
8.75273789, 5.92066231, 6.95366577, 4.26020533, 11.40501619,
9.87457947, 21.13174127, 16.42066897, 31.89439842, 33.06799105,
34.07732157, 29.29939609, 27.02846164, 20.81549686, 14.50583432,
16.11199422, 5.25642553, 5.67979421, 5.03884106, 5.91383786,
-6.92432553, -4.54670446, -5.43453018, -11.06914116, -4.30401515,
-5.89475834, -6.20354881, -5.22134237, -1.09985899, 9.43369798,
1.66688812, 1.20138491, 6.90689277, 7.75020233, 10.13668917,
11.30827244, 9.29930475, 10.9321974, 9.07936123, 15.05633842,
13.19202501, 5.4705525, 6.82591085, 2.44716859, 4.11748271, 5.06863218,
6.22577349, 6.12185947, 0.58671313, -3.10270985, 1.18732102,
-8.2784308, -6.9219884, -15.34433626, -14.14267264, -15.499663,
-17.52534249, -13.08561877, -13.30851885, -14.85330263, -14.60224224,
-11.87824448, -10.99332931, -13.12023756, -13.1469306, -6.67165565,
-4.86353483, -1.18866693, 5.19617018, -5.56720735, -1.31601522,
2.27149134, -0.32052319, 6.34015708, -2.21439899, 8.54617508,
7.90296929, 8.97464479, 8.46001663, 14.8780712, 0.80639894, 12.94240637,
3.50598117, 9.14630966, 19.69171985, 15.802644, 16.51140186,
21.66597738, 20.28010486, 25.27245738, 22.10541531, 15.94575338,
26.38566548, 13.20690115, 17.00548484, 12.09162042, 0.06482127,
2.03901271, 0.44242727, -9.82586651, 0.27668163, -2.78174059,
-2.82866966, -3.82970378, -5.71082564, 2.43947926, -5.13803015,
-5.57470758, -5.42985341, -2.92568853, 3.29088281, 0.70260049,
-5.31805275, -3.60733326, -1.34953732, -13.82572968, -43.79388438,
-37.96950775, -25.84963925, -17.45358624, 1.37712882, -6.52424698,
-1.90571756, -7.56995756, 4.05542391, -3.88890971, 0.22947427,
18.0580403, 83.71922617, 69.97805207, 37.59500165, 29.96692408,
15.45334076, 19.1358433, 18.69987106, 1.361784), new_licences = c(-8.68185938,
-0.02872738, -4.63917526, -1.28723796, 6.61294804, 22.56276492,
0.7781103, 4.79877409, 10.14401124, 8.24945593, 12.81146637,
9.55541727, 22.9280397, 5.57950192, 13.32309582, 5.15754737,
-8.87412041, -4.33625964, 4.81079247, 6.37217177, -6.40984757,
-3.33605579, 37.89418817, -43.23130325, -42.15352673, -42.12746655,
-33.9205377, -33.62858734, -23.67129034, -20.50889878, -14.90326237,
-12.50180871, 3.46190541, 1.31288184, -17.85106785, 61.84546971,
53.12063809, 86.83179182, 33.04076778, 46.80088462, 62.54917879,
18.35461489, 13.50837139, 15.08185877, 1.04729285, 19.36104696,
61.72782699, -9.18395384, -12.57976299, -20.58231247, -11.10426044,
-23.56363636, -34.11195205, -29.70589684, -31.85551458, -33.86262394,
-40.44064924, -48.64559819, -59.99715485, -47.36404205, -48.59228363,
-49.05969361, -41.7394923, -27.49762131, -37.00874636, -35.13210456,
-33.07096298, -30.93970668, -19.15289482, -25.34798535, -43.46550498,
0.89093261, 8.91046074, -0.19703412, -4.26190476, -5.38995126,
17.88392113, 17.14563526, 12.84454245, 15.91945886, 23.0810884,
23.51044441, 35.77606542, 31.90428714, 40.30863376, 47.04904406,
53.12111415, 36.55008422, 23.55712603, 30.67416767, 26.05438854,
31.72750712, 29.72943247, 33.01929625, 37.87352328, 27.84796458,
35.64994785, 41.8951385, 21.94250447, 33.10840226, 33.91801716,
9.71134166, 21.90931404, 30.04017719, 16.31199661, 14.13943169,
10.39986559, 17.71959459, 25.98727895, 31.75638663, 6.44645711,
13.57863178, 10.93394077, 0.59826311, 12.84306453, 10.63138715,
8.87090896, 23.54216507, 29.26495206, 7.82034725, 4.63800277,
-1.80663693, 17.80530531, 13.51986946, 6.23716632, 12.3673104,
12.0950324, 6.38739706, 6.44081414, 6.66263238, 0.41205557, -2.8280543,
10.1373204, 7.07467283, 13.80775358, -0.3255401, 5.34750745,
13.59549283, 28.0137388, -14.04051962, -12.215373, -12.25462385,
-5.28197913, 7.40258851, -9.21869735, -10.48961104, -1.44190387,
-3.61384459, -3.27574344, -7.6398978, -18.62443557, 13.99263957,
12.13098309, 6.03905341, 9.71096119, -8.04004081, 7.45041892,
-57.44578313, -86.98451778, -74.73596198, -56.22999407, -17.50379692,
-0.14475271, -9.42437148, -12.58866381, -27.8902439, -19.59833014,
-30.46522915, -58.98435572, 19.84711212, 438.70498363, 190.21076468,
70.96424702, -18.97560655, -35.80575018, -18.2011224, -22.68440675,
-7.59343819), investm = c(9.59206261, 10.09104451, 8.49259555,
11.05912367, 11.33035356, 10.69893187, 9.27221449, 6.36319052,
5.1145808, 3.14675108, -2.0583252, -5.9801462, -8.54458193, -11.85690298,
-12.73704068, -15.30334665, -16.51488054, -18.55445193, -18.78407228,
-20.33141371, -23.20479413, -23.70413199, -25.82349247, -23.49151634,
-23.20331483, -21.76656318, -19.69767216, -16.1163604, -15.56801204,
-10.63893647, -6.90914634, -1.33860382, 5.00317034, 9.14637266,
13.66111398, 14.90618555, 16.27492141, 18.9726911, 17.81129998,
11.93953654, 9.97602683, 6.72450506, 3.35538125, 1.22492917,
-0.25529345, -3.58030117, -5.66383325, -5.99451638, -5.59778107,
-8.22207902, -8.75620416, -6.82634766, -7.76850362, -8.81317285,
-10.30877298, -12.31016477, -12.44698993, -11.49249967, -9.6953666,
-10.58305192, -9.65103885, -8.93856304, -8.31371275, -8.81656192,
-7.08877565, -5.92517826, -3.13301918, -0.90715848, 0.17047088,
0.71130641, 3.09817756, 6.3772002, 6.05805695, 4.99307084, 4.93907049,
7.50839619, 8.21051614, 7.46196337, 6.59668824, 6.44846351, 4.4735673,
3.33064046, -0.20557392, -0.17865147, -0.07705874, 1.55447287,
0.59002986, -0.47146863, 0.62804213, -2.08346753, -2.73661788,
-3.90829617, -3.78220665, -4.01170424, -5.17638734, -6.16018899,
-5.69641901, -6.49065187, -5.75373139, -6.16938142, -10.39770764,
-7.95466132, -7.83823783, -8.7925605, -7.90867493, -5.97676105,
-5.228471, -6.84892555, -8.8243345, -9.50405058, -10.14943257,
-10.02532808, -8.25385595, -7.41211911, -6.72331539, -4.65132561,
-3.88085377, -5.35115101, -3.6992884, 3.41808435, 5.01032011,
6.4338955, 7.44842984, 7.96282188, 10.42036183, 9.85846999, 10.3384518,
12.3886042, 13.55244814, 14.4281047, 12.93327424, 7.14080025,
6.37943542, 4.68820186, 5.38710032, 4.61464096, 3.14205599, 2.9423131,
2.15746296, 0.09387282, -2.38577599, -2.97124266, -4.32001028,
-4.33596506, -3.36175388, -1.65514585, -1.1147413, -1.1116245,
0.03453284, 1.51801102, 1.97354903, 3.29586522, 4.83238917, 5.70220137,
7.46051222, 7.48040345, 6.30281751, 0.14703508, -0.15754408,
0.73361141, 0.474155, 0.46358707, 1.64385862, 1.15369522, 0.67754708,
4.63841367, 6.78461903, 8.58994132, 10.23754398, 17.32260776,
19.2077649, 20.72874275, 21.76681016, 22.19165981, 23.17276241,
23.11683188, 23.85460849, 20.34415852), household_consupt = c(0,
0, 1.359168191, 0, 0, -0.342789739, 0, 0, 1.790564565, 0, 0,
-0.482967554, 0, 0, -1.762057837, 0, 0, -0.536837331, 0, 0, -4.302408991,
0, 0, -4.488751839, 0, 0, -1.381433473, 0, 0, 0.30585643, 0,
0, 3.227950235, 0, 0, 5.377633558, 0, 0, 1.854863297, 0, 0, -0.352069414,
0, 0, -0.701775487, 0, 0, -3.526502117, 0, 0, -2.191789397, 0,
0, -0.956817085, 0, 0, -2.394047662, 0, 0, 1.250192652, 0, 0,
-0.572344375, 0, 0, 0.753471833, 0, 0, 0.070143601, 0, 0, -0.298510806,
0, 0, 3.154164092, 0, 0, 0.883890768, 0, 0, 2.625083331, 0, 0,
0.256046586, 0, 0, -0.856863549, 0, 0, 0.590803335, 0, 0, -0.813925723,
0, 0, 0.125528578, 0, 0, 1.468506379, 0, 0, -0.670352866, 0,
0, -0.362744845, 0, 0, 1.32210668, 0, 0, -1.215018014, 0, 0,
0.331687133, 0, 0, 1.532560625, 0, 0, -0.533138522, 0, 0, -0.422657587,
0, 0, 0.037693685, 0, 0, -0.657257657, 0, 0, 0.489966584, 0,
0, 0.938809848, 0, 0, -0.343317849, 0, 0, 0.626641626, 0, 0,
0.067372918, 0, 0, -0.634514601, 0, 0, 0.004316699, 0, 0, -0.456951802,
0, 0, -3.945597519, 0, 0, -16.828812208, 0, 0, 12.869510817,
0, 0, -1.316414045, 0, 0, -1.582469509, 0, 0, 26.887239149, 0,
0, -14.346872459, 0, 0, 2.145073044, 0), covid_total_cases = 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, 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, 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, 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, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 236.490662, 29.76642, 29.664615,
21.193137, 13.588659, 30.217886, 87.020465, 110.973322, 38.789711,
74.173149, 11.664696, 2.132837, 1.797567, 1.506289, 3.587828,
10.127144, 7.154014, 3.020636, 1.99873, 5.189378), covid_total_vaccinations = 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, 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, 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, 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, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 964.859996,
163.568194, 94.43678, 87.634082, 73.551864, 47.399777, 42.470628,
20.985756, 8.109421, 1.48542, 7.961336)), class = "data.frame", row.names = c(NA,
-178L))
Code:
VAR_data_Total = data
f = function(data){
m1 = vars::VAR(data[,1:10], p=3, exogen = data[,11:12])
s1 = summary(m1)
return(s1$varresult$unemp$coefficients)
}
zoo::rollapply(VAR_data_Total, width = 41, FUN = f, by.column = FALSE)

Adding industry dummies to 2SLS in R

for my Master Thesis I want to regress the ESG score on the stock price drop during the pandemic. For OLS this works fine. To check for potential Endogeneity I also conduct a 2SLS regression with the industry average ESG score as instrument. This works fine as long as I leave out the industry dummies. When adding them I get the following error Message:
Warning message:
In pf(w, df[1L], df[2L], lower.tail = FALSE) : NaNs produced
Moreover, the diagnostics for weak instruments and Wu-Hausman also show NaN.
I am aware of the dummy variable trap so not all industries were included.
Does anyone know why I get this error message? Any help is appreciated. Below I will provide my results with and without dummies.
Without dummies
With dummies
I managed to replicate the warning message with the first 10 rows of my data:
structure(list(NAME = c("A-MARK PRECIOUS METALS", "AAON", "AAR", "ABBOTT LABORATORIES", "ABBVIE", "ABEONA THERAPEUTICS", "ABERCROMBIE & FITCH A", "ABIOMED", "ABM INDS.", "ABRAXAS PETROLEUM"), ESG = c(30.93, 46.31, 24.66, 70.67, 79.79, 36.58, 69.13, 25.88, 72.66, 18.88 ), LogAssets = c(13.538484837701, 13.0147959839376, 14.5473975668402, 18.0628622893116, 18.8299054426017, 11.9263455151269, 15.0139386185708, 13.9751825387466, 15.1444141253049, 11.9688365085588), Quick = c(0.38, 2.27, 1.69, 1.14, 0.6, 2.26, 1.24, 4, 1.32, 0.1), I_Agri = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), I_Cons = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), I_Fin = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), I_Man = c(0, 1, 0, 1, 1, 1, 0, 1, 0, 0), I_Mining = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1), I_Serv = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0), I_Trade = c(1, 0, 0, 0, 0, 0, 1, 0, 0, 0), I_Utility = c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0), Drop = c(0.107419712070875, 0.277738886944347, 0.791427308882015, 0.297000895255148, 0.31485022307202, 0.75, 0.527002967359051, 0.222692078618225, 0.473209685729006, 0.683189655172414), Leverage = c(0.8177, 0.0178, 0.3993, 0.3623, 0.8679, 0.0169, 0.2659, 0, 0.3257, 1.5458 ), ROA = c(0.0622, 0.1926, 0.0065, 0.0726, 0.0542, -0.4324, -0.0259, 0.1888, 0.0095, -0.6467), ESG_A = c(41.9334803921569, 41.6947268673356, 42.0122772277228, 41.6947268673356, 41.6947268673356, 41.6947268673356, 41.9334803921569, 41.6947268673356, 37.5789174311926, 34.9968604651163 )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame" ))
The code I used:
library(AER)
IV2=ivreg(Drop~ESG+LogAssets+Leverage+ROA+Quick+I_Cons+I_Fin+I_Man+I_Mining+I_Serv+I_Trade+I_Utility | ESG_A+LogAssets+Leverage+ROA+Quick+I_Cons+I_Fin+I_Man+I_Mining+I_Serv+I_Trade+I_Utility, data=mwe)
summary(IV2, diagnostics = TRUE)
Rstudio Version: 2022.2.0.443 Operating system: Windows 10 pro 64 bit
Thank you!

weighted mean R

I was wondering how I can get the weighted average of my data. I have already looked on the internet, but when I try the weighted.mean function, I keep getting the same result, so I was wondering what I am doing wrong.
Below is some information of the dataset:
dput(head(new))
structure(list(comp.1 = c(0.5, 0.25, 0, 0.25, 0.31, 0.3), comp.2 = c(0.3,
0.15, 0, 0.15, 0, 0), comp.3 = c(0.2, 0.6, 1, 0.6, 0.69, 0.7),
genderMale = c(0, 1, 1, 1, 0, 0), SeniorCitizen = c(0, 0,
0, 0, 0, 0), PartnerYes = c(1, 0, 0, 0, 0, 0), DependentsYes = c(0,
0, 0, 0, 0, 0), tenure = c(-1.28015700354285, 0.064298112878097,
-1.23941593940889, 0.512449818351747, -1.23941593940889,
-0.994969554605076), MultipleLinesYes = c(0, 0, 0, 0, 0,
1), `InternetServiceFiber optic` = c(0, 0, 0, 0, 1, 1), OnlineSecurityYes = c(0,
1, 1, 1, 0, 0), OnlineBackupYes = c(1, 0, 1, 0, 0, 0), DeviceProtectionYes = c(0,
1, 0, 1, 0, 1), TechSupportYes = c(0, 0, 0, 1, 0, 0), StreamingTVYes = c(0,
0, 0, 0, 0, 1), StreamingMoviesYes = c(0, 0, 0, 0, 0, 1),
`ContractOne year` = c(0, 1, 0, 1, 0, 0), `ContractTwo year` = c(0,
0, 0, 0, 0, 0), PaperlessBillingYes = c(1, 0, 1, 0, 1, 1),
`PaymentMethodCredit card (automatic)` = c(0, 0, 0, 0, 0,
0), `PaymentMethodElectronic check` = c(1, 0, 0, 0, 1, 1),
`PaymentMethodMailed check` = c(0, 1, 1, 0, 0, 0), MonthlyCharges = c(-1.16161133177258,
-0.260859369930086, -0.363897417225722, -0.747797238601399,
0.196164226945719, 1.15840663636787), TotalCharges = c(1.47494433546539,
3.27634689625303, 2.03402652377511, 3.26499480914874, 2.18084241464668,
2.91407858538911)), row.names = c("1", "2", "3", "4", "5",
"6"), class = "data.frame")
As you can see, I have 3 components (comp.1, comp.2, comp.3). All of these components have their posterior probabilities. And I am wondering how I can get the weighted averages for all of these and the final weighted averages. I have tried:
weighted.mean(new$comp.1, new$SeniorCitizen)
weighted.mean(new$comp.2, new$SeniorCitizen)
weighted.mean(new$comp.3, new$SeniorCitizen)
It gave me the output 0.24, 0.14 and 0.61. But irrespectively which variable I put, I get the same output. What am I doing wrong?

How to add ellipse in bray nmds analysis in vegan package

I have plotted point graph using vegan package but I want to circle the similarly treated species. As shown in the figure, 3 colors for 3 treatments. I want to circle them too.
Here is my code.
library(vegan)
library(MASS)
library(readxl)
bray1 <- read_excel("bray1.xlsx")
cols <- c("red", "blue","blue", "green","green","red","blue","green","green","red","red","blue")
row.names(bray1) <- c("SI1", "SII0", "SI0", "SII2", "SI2", "SII1", "SIII0", "SIV2", "SIII2", "SIV1", "SIII1", "SIV0")
bcdist <- vegdist(bray1, "bray")
bcmds <- isoMDS(bcdist, k = 2)
plot(bcmds$points, type = "n", xlab = "", ylab = "")
text(bcmds$points, dimnames(bray1)[[1]],col = cols,size=10)
[My data
bray1<-structure(list(`Andropogon virginicus` = c(0, 0, 0, 0, 2.7, 31.5333333333333, 0, 0, 0, 0, 0, 0), `Oenothera parviflora` = c(61.6,30.3333333333333, 7.53333333333333, 0, 11.7333333333333, 0, 0, 0,75.4, 0, 0, 0), `Lespedeza cuneata` = c(0, 0, 0, 0, 0, 46.7333333333333, 0, 0, 3, 0, 0, 0), `Lespedeza pilosa` = c(0, 1.93333333333333, 0, 0, 1.73333333333333, 0, 0, 0, 0, 1.7, 0, 0), `Chamaesyce maculata` = c(0, 0, 0,4.733333333, 0, 0, 0, 0, 0, 0, 0, 0), `Chamaesyce nutans` = c(0,0, 0, 0, 0,0, 0.166666666666667, 0, 0, 0, 0, 0), `Bidens frondosa` = c(0, 0, 0,1.76666666666667, 1.03333333333333, 3.23333333333333, 0, 0, 0, 0, 0, 0), `Erigeron annuus` = c(0, 0, 0, 0, 0.4, 0, 0, 0, 0, 0, 0, 0), `Erigeron canadensis` = c(0, 0, 0, 0, 0, 4.33333333333333, 0, 0, 9.1, 2.066666667, 0,0), `Equisetum arvense` = c(46, 62.7333333333333, 0, 1.66666666666667, 0, 0.533333333333333, 0, 0, 0, 0, 0, 0), `Erigeron sumatrensis` = c(0, 0, 0, 0, 0, 16.4333333333333, 0, 4, 0, 6.633333333, 0, 0), `Hypochaeris radicata` = c(0, 3.76666666666667, 116.6, 0, 5.033333333, 9.76666666666667, 29, 0, 23.1666666666667, 82.16666667, 0, 0), `Lactuca indica` = c(10.26666667, 0, 1.566666667, 120.1333333, 44.36666667, 42.0333333333333, 0, 14.2333333333333, 0, 0, 14.36666667, 22.2), `Solidago altissima` = c(0, 1.06666666666667, 33.93333333, 0, 0, 0, 0, 0, 0, 6.6, 0, 0), `Sonchus asper` = c(0, 35.9, 0, 0, 0, 7.46666666666667,
29.6666666666667, 4.96666666666667, 0, 0, 0.23, 2.933333333 )), .Names = c("Andropogon virginicus", "Oenothera parviflora", "Lespedeza cuneata", "Lespedeza pilosa", "Chamaesyce maculata", "Chamaesyce nutans", "Bidens frondosa", "Erigeron annuus", "Erigeron canadensis", "Equisetum arvense", "Erigeron sumatrensis", "Hypochaeris radicata", "Lactuca indica", "Solidago altissima", "Sonchus asper"), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"))
Here are a couple of alternatives based on the dataEllipse function in the car package. I have made a few minor alterations to your base graph. I found it hard to read the pure "green" color text, so I switched it to "darkgreen". I changed the plotting limits so that the full ellipses would be in the picture. Also, your text statement included an argument size. text does not have an argument size so I replaced it with cex to set the font size.
library(car)
Group = c(1,2,2,3,3,1,2,3,3,1,1,2)
cols <- c("red", "blue","blue", "darkgreen","darkgreen","red","blue",
"darkgreen","darkgreen","red","red","blue")
In the first version, I did what I think you asked for, ellipses marking the treatment groups.
plot(bcmds$points, type = "n", xlab = "", ylab = "",
xlim=c(-0.8,0.8), ylim=c(-0.8,0.8), asp=1)
text(bcmds$points, dimnames(bray1)[[1]],col = cols, cex=0.8)
dataEllipse(bcmds$points[,1], bcmds$points[,2], factor(Group),
plot.points=F, add=T, col=c("red", "blue", "green"),
levels=rep(0.6, 3), center.pch=0, group.labels="", lwd=1)
In the second version, instead of using the outline of the ellipse, I use a transparent fill color to show the ellipses.
plot(bcmds$points, type = "n", xlab = "", ylab = "",
xlim=c(-0.8,0.8), ylim=c(-0.8,0.8), asp=1)
text(bcmds$points, dimnames(bray1)[[1]],col = cols, cex=0.8)
dataEllipse(bcmds$points[,1], bcmds$points[,2], factor(Group),
plot.points=F, add=T, col=c("red", "blue", "green"),
levels=rep(0.6, 3), center.pch=0, group.labels="",
lty=0, fill=TRUE, fill.alpha=0.04)

Resources