简体   繁体   中英

How to use a loop to work out a model through several variables in R

Here my data for boosting

 new=structure(list(B1 = c(6914L, 6914L, 6914L, 6958L, 6958L, 6958L, 
    6958L, 6914L, 6914L, 6914L, 6914L, 5672L, 6014L, 6014L, 6014L, 
    6014L, 6014L, 6958L, 6958L, 6958L, 6958L, 6958L, 6958L, 6914L, 
    6914L, 6914L, 6914L, 6092L, 6092L, 6092L, 6092L, 6092L, 6239L, 
    6239L, 6239L, 6239L, 6239L, 6239L, 6615L, 6615L, 6615L, 6615L, 
    6615L, 7038L, 7038L, 7038L, 7038L, 7038L, 7038L, 6602L, 8136L, 
    8136L, 7369L, 8136L, 8136L, 7369L, 8136L, 8136L, 8136L, 7369L, 
    7369L, 8136L, 8136L, 8136L, 8136L, 7369L, 7369L, 8136L, 8136L, 
    8136L, 8136L, 8317L, 8317L, 8317L, 8821L, 8821L, 8821L, 8821L, 
    8821L, 8317L, 8317L, 8821L, 8821L, 8821L, 8821L, 8821L, 8821L, 
    8317L, 8821L, 8821L, 8821L, 8821L, 8821L, 8821L, 8821L, 8821L, 
    8821L, 8821L, 8821L, 9245L), B2 = c(5560L, 5380L, 5644L, 5088L, 
    5280L, 5200L, 5472L, 5568L, 5560L, 5424L, 5404L, 4784L, 4696L, 
    4820L, 4588L, 4544L, 4452L, 4716L, 5048L, 5236L, 5416L, 5584L, 
    5824L, 5800L, 5932L, 5980L, 6112L, 4796L, 4860L, 5396L, 5900L, 
    5968L, 5968L, 5776L, 5440L, 5248L, 4884L, 4760L, 4796L, 4860L, 
    4776L, 4664L, 4716L, 4952L, 5168L, 5316L, 5548L, 5768L, 5900L, 
    5948L, 5796L, 5756L, 5912L, 5956L, 6000L, 6196L, 6044L, 6164L, 
    6268L, 6588L, 6508L, 6460L, 6608L, 6592L, 6600L, 7100L, 7016L, 
    6988L, 6952L, 6800L, 6644L, 7308L, 7340L, 7528L, 7492L, 7304L, 
    6928L, 6748L, 6764L, 7492L, 7648L, 7580L, 7416L, 7108L, 6864L, 
    7056L, 7164L, 7744L, 7720L, 7360L, 7188L, 7204L, 7280L, 7236L, 
    7520L, 7352L, 7352L, 7376L, 7320L, 7428L), B3 = c(4768L, 4840L, 
    4936L, 4320L, 4388L, 4572L, 4640L, 4704L, 4696L, 4488L, 4396L, 
    4002L, 4030L, 3960L, 3684L, 3680L, 3896L, 4212L, 4364L, 4508L, 
    4732L, 4896L, 4848L, 4872L, 4960L, 5052L, 4848L, 4308L, 4800L, 
    5216L, 5224L, 5248L, 5136L, 4720L, 4428L, 4120L, 3918L, 4052L, 
    4058L, 3806L, 3802L, 3930L, 4092L, 4324L, 4476L, 4604L, 4768L, 
    4980L, 5080L, 5288L, 4840L, 4936L, 5096L, 5040L, 5096L, 5292L, 
    5280L, 5360L, 5480L, 5584L, 5528L, 5524L, 5700L, 5736L, 5732L, 
    6136L, 5980L, 5884L, 5904L, 5820L, 5864L, 6488L, 6572L, 6476L, 
    6256L, 6024L, 5844L, 5884L, 6100L, 6684L, 6596L, 6376L, 6188L, 
    5952L, 6044L, 6212L, 6268L, 6668L, 6484L, 6336L, 6164L, 6332L, 
    6432L, 6396L, 6592L, 6548L, 6500L, 6464L, 6460L, 7008L), B4 = c(4960L, 
    4964L, 4540L, 4164L, 4412L, 4608L, 4628L, 4588L, 4416L, 4312L, 
    4372L, 3806L, 3652L, 3570L, 3480L, 3708L, 3886L, 4188L, 4284L, 
    4344L, 4704L, 4776L, 4772L, 4980L, 5000L, 4852L, 4508L, 4916L, 
    5356L, 5400L, 5268L, 5156L, 4620L, 4324L, 4016L, 3884L, 3854L, 
    3854L, 3770L, 3562L, 3708L, 3854L, 4084L, 4228L, 4440L, 4532L, 
    4784L, 5008L, 5292L, 5464L, 4868L, 4996L, 4908L, 4932L, 5060L, 
    5136L, 5280L, 5444L, 5492L, 5500L, 5560L, 5604L, 5704L, 5660L, 
    5716L, 5892L, 5844L, 5796L, 5752L, 5816L, 5892L, 6500L, 6488L, 
    6212L, 5928L, 5796L, 5876L, 6084L, 6284L, 6660L, 6424L, 6088L, 
    6004L, 6044L, 6268L, 6336L, 6664L, 6500L, 6308L, 6152L, 6288L, 
    6424L, 6376L, 6860L, 6464L, 6500L, 6508L, 6468L, 7144L, 7652L
    ), B5 = c(5554L, 5554L, 4782L, 4736L, 4736L, 5018L, 5018L, 4968L, 
    4968L, 4677L, 4677L, 3814L, 3667L, 3667L, 3594L, 3975L, 3975L, 
    4348L, 4348L, 4736L, 4736L, 5018L, 5018L, 4968L, 4968L, 4677L, 
    4677L, 4930L, 5524L, 5524L, 5229L, 5229L, 4424L, 4424L, 4113L, 
    4113L, 4069L, 4069L, 3857L, 3932L, 3932L, 4228L, 4228L, 4591L, 
    4591L, 4918L, 4918L, 5324L, 5324L, 5543L, 5327L, 5327L, 5301L, 
    5471L, 5471L, 5301L, 5471L, 5471L, 5846L, 5977L, 5977L, 5899L, 
    5899L, 6099L, 6099L, 5977L, 5977L, 5899L, 5899L, 6099L, 6099L, 
    6857L, 6517L, 6517L, 6220L, 6220L, 6418L, 6418L, 6969L, 6517L, 
    6517L, 6220L, 6220L, 6418L, 6418L, 6969L, 6969L, 6861L, 6581L, 
    6581L, 6729L, 6729L, 7265L, 7265L, 6581L, 6729L, 6729L, 7265L, 
    7265L, 8025L), B6 = c(5249L, 5249L, 4428L, 4553L, 4553L, 4832L, 
    4832L, 4741L, 4741L, 4428L, 4428L, 3736L, 3464L, 3464L, 3509L, 
    3894L, 3894L, 4270L, 4270L, 4553L, 4553L, 4832L, 4832L, 4741L, 
    4741L, 4428L, 4428L, 5030L, 5441L, 5441L, 4926L, 4926L, 4146L, 
    4146L, 3907L, 3907L, 3910L, 3910L, 3721L, 3831L, 3831L, 4201L, 
    4201L, 4509L, 4509L, 4871L, 4871L, 5235L, 5235L, 5217L, 5207L, 
    5207L, 5087L, 5290L, 5290L, 5087L, 5290L, 5290L, 5777L, 5721L, 
    5721L, 5746L, 5746L, 5982L, 5982L, 5721L, 5721L, 5746L, 5746L, 
    5982L, 5982L, 6504L, 6116L, 6116L, 5946L, 5946L, 6257L, 6257L, 
    6916L, 6116L, 6116L, 5946L, 5946L, 6257L, 6257L, 6916L, 6916L, 
    6407L, 6293L, 6293L, 6545L, 6545L, 7197L, 7197L, 6293L, 6545L, 
    6545L, 7197L, 7197L, 7998L), B7 = c(4893L, 4893L, 4138L, 4527L, 
    4527L, 4681L, 4681L, 4505L, 4505L, 4170L, 4170L, 3629L, 3388L, 
    3388L, 3545L, 3982L, 3982L, 4288L, 4288L, 4527L, 4527L, 4681L, 
    4681L, 4505L, 4505L, 4170L, 4170L, 5127L, 5268L, 5268L, 4703L, 
    4703L, 3996L, 3996L, 3775L, 3775L, 3713L, 3713L, 3594L, 3813L, 
    3813L, 4166L, 4166L, 4462L, 4462L, 4836L, 4836L, 5277L, 5277L, 
    4910L, 5235L, 5235L, 5001L, 5241L, 5241L, 5001L, 5241L, 5241L, 
    5688L, 5539L, 5539L, 5599L, 5599L, 5988L, 5988L, 5539L, 5539L, 
    5599L, 5599L, 5988L, 5988L, 6278L, 5864L, 5864L, 5931L, 5931L, 
    6177L, 6177L, 6896L, 5864L, 5864L, 5931L, 5931L, 6177L, 6177L, 
    6896L, 6896L, 6212L, 6159L, 6159L, 6382L, 6382L, 7383L, 7383L, 
    6159L, 6382L, 6382L, 7383L, 7383L, 7856L), B8 = c(4836L, 4840L, 
    5044L, 4074L, 4236L, 4404L, 4592L, 4668L, 4796L, 4628L, 4632L, 
    3914L, 3896L, 3796L, 3580L, 3598L, 3596L, 3830L, 4096L, 4320L, 
    4460L, 4648L, 4904L, 4980L, 4940L, 5148L, 5180L, 4164L, 4628L, 
    5304L, 5512L, 5592L, 5500L, 5216L, 4732L, 4380L, 4036L, 4008L, 
    3994L, 3784L, 3660L, 3650L, 3794L, 4018L, 4212L, 4296L, 4516L, 
    4648L, 4908L, 5148L, 4876L, 4828L, 4936L, 4992L, 5052L, 5236L, 
    5236L, 5336L, 5380L, 5708L, 5648L, 5624L, 5580L, 5724L, 5796L, 
    6280L, 6156L, 6012L, 5876L, 5864L, 5868L, 6488L, 6636L, 6752L, 
    6612L, 6268L, 5924L, 5968L, 6092L, 6752L, 6876L, 6764L, 6484L, 
    6176L, 6108L, 6300L, 6460L, 6924L, 6724L, 6528L, 6484L, 6352L, 
    6500L, 6600L, 6784L, 6876L, 6676L, 6616L, 6732L, 6896L), B8A = c(4679L, 
    4679L, 4098L, 4524L, 4524L, 4643L, 4643L, 4460L, 4460L, 3987L, 
    3987L, 3413L, 3294L, 3294L, 3490L, 3840L, 3840L, 4140L, 4140L, 
    4524L, 4524L, 4643L, 4643L, 4460L, 4460L, 3987L, 3987L, 5232L, 
    5152L, 5152L, 4421L, 4421L, 3863L, 3863L, 3697L, 3697L, 3561L, 
    3561L, 3558L, 3788L, 3788L, 4110L, 4110L, 4493L, 4493L, 4894L, 
    4894L, 5032L, 5032L, 4606L, 5209L, 5209L, 4889L, 5233L, 5233L, 
    4889L, 5233L, 5233L, 5787L, 5324L, 5324L, 5492L, 5492L, 6018L, 
    6018L, 5324L, 5324L, 5492L, 5492L, 6018L, 6018L, 5872L, 5544L, 
    5544L, 5876L, 5876L, 6279L, 6279L, 6963L, 5544L, 5544L, 5876L, 
    5876L, 6279L, 6279L, 6963L, 6963L, 6134L, 6187L, 6187L, 6547L, 
    6547L, 7280L, 7280L, 6187L, 6547L, 6547L, 7280L, 7280L, 7968L
    ), B9 = c(6752L, 6752L, 6752L, 7098L, 7098L, 7098L, 7098L, 6752L, 
    6752L, 6752L, 6752L, 4997L, 5626L, 5626L, 5626L, 5626L, 5626L, 
    7098L, 7098L, 7098L, 7098L, 7098L, 7098L, 6752L, 6752L, 6752L, 
    6752L, 5698L, 5698L, 5698L, 5698L, 5698L, 5408L, 5408L, 5408L, 
    5408L, 5408L, 5408L, 5685L, 5685L, 5685L, 5685L, 5685L, 6352L, 
    6352L, 6352L, 6352L, 6352L, 6352L, 5794L, 8407L, 8407L, 7048L, 
    8407L, 8407L, 7048L, 8407L, 8407L, 8407L, 7048L, 7048L, 8407L, 
    8407L, 8407L, 8407L, 7048L, 7048L, 8407L, 8407L, 8407L, 8407L, 
    8487L, 8487L, 8487L, 9610L, 9610L, 9610L, 9610L, 9610L, 8487L, 
    8487L, 9610L, 9610L, 9610L, 9610L, 9610L, 9610L, 8487L, 9610L, 
    9610L, 9610L, 9610L, 9610L, 9610L, 9610L, 9610L, 9610L, 9610L, 
    9610L, 10128L), B10 = c(4170L, 4170L, 3407L, 3301L, 3301L, 3612L, 
    3612L, 3600L, 3600L, 3352L, 3352L, 2502L, 2388L, 2388L, 2403L, 
    2659L, 2659L, 2979L, 2979L, 3301L, 3301L, 3612L, 3612L, 3600L, 
    3600L, 3352L, 3352L, 3856L, 4192L, 4192L, 3840L, 3840L, 3187L, 
    3187L, 2782L, 2782L, 2634L, 2634L, 2501L, 2562L, 2562L, 2792L, 
    2792L, 3068L, 3068L, 3405L, 3405L, 3816L, 3816L, 3858L, 3343L, 
    3343L, 3188L, 3438L, 3438L, 3188L, 3438L, 3438L, 3774L, 3543L, 
    3543L, 3653L, 3653L, 3934L, 3934L, 3543L, 3543L, 3653L, 3653L, 
    3934L, 3934L, 4224L, 3999L, 3999L, 3881L, 3881L, 4162L, 4162L, 
    4724L, 3999L, 3999L, 3881L, 3881L, 4162L, 4162L, 4724L, 4724L, 
    4293L, 4161L, 4161L, 4380L, 4380L, 5052L, 5052L, 4161L, 4380L, 
    4380L, 5052L, 5052L, 5756L), B11 = c(3124L, 3124L, 2514L, 2969L, 
    2969L, 3137L, 3137L, 2922L, 2922L, 2487L, 2487L, 1850L, 1822L, 
    1822L, 2014L, 2309L, 2309L, 2600L, 2600L, 2969L, 2969L, 3137L, 
    3137L, 2922L, 2922L, 2487L, 2487L, 3753L, 3535L, 3535L, 2823L, 
    2823L, 2296L, 2296L, 2151L, 2151L, 1974L, 1974L, 1924L, 2100L, 
    2100L, 2386L, 2386L, 2731L, 2731L, 3087L, 3087L, 3380L, 3380L, 
    3006L, 2740L, 2740L, 2443L, 2800L, 2800L, 2443L, 2800L, 2800L, 
    3228L, 2669L, 2669L, 2935L, 2935L, 3338L, 3338L, 2669L, 2669L, 
    2935L, 2935L, 3338L, 3338L, 3049L, 2893L, 2893L, 3087L, 3087L, 
    3550L, 3550L, 4235L, 2893L, 2893L, 3087L, 3087L, 3550L, 3550L, 
    4235L, 4235L, 3184L, 3242L, 3242L, 3774L, 3774L, 4487L, 4487L, 
    3242L, 3774L, 3774L, 4487L, 4487L, 5217L), B = c(20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
    50L, 50L, 50L, 50L, 50L, 50L), E = c(20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
    30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
    30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
    30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
    30L, 30L, 30L, 30L), C = c(20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 20L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L), OC = c(30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
    30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
    30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
    30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
    30L, 30L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L
    ), OLS = c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA, -100L
    ))

Five column with depended vars (B,E, C,OC,OLS). I must perform 5 boosting models. Simple i can do it. Take this script, and just change target var 5 times(any dep var ~. )

# train GBM model
gbm.fit.final109 <- gbm(
  formula = Value ~ .,
  distribution = "gaussian",
  data = new,
  n.trees = 483,
  interaction.depth = 5,
  shrinkage = 0.1,
  n.minobsinnode = 5,
  bag.fraction = .65, 
  train.fraction = 1,
  n.cores = NULL, # will use all cores by default
  verbose = FALSE
)  

However, is it possible to create some kind of loop that substitutes column with depended var from the list into the script? For example (mylistwithdepended_vars=c((B,E,C,OC,OLS) ), IEwe take each dependent variable, as soon as it works out in a loop, we substitute another from the list, until through this loop we work out all the columns.

How can i do it. As output i just want 5 models

 gbm.fit.finalB
gbm.fit.finalE
gbm.fit.finalC
gbm.fit.finalOC
gbm.fit.finalOLS

Always appreciate your help, thanks.

Are you looking for something like the following?

library(gbm)
#> Loaded gbm 2.1.8

dependend <- c("B", "E", "C", "OC", "OLS")
mylistwithdepended_vars <- mget(dependend, envir = as.environment(new))

fun <- function(Value){
  gbm.fit.final109 <- gbm(
    formula = Value ~ .,
    distribution = "gaussian",
    data = new,
    n.trees = 483,
    interaction.depth = 5,
    shrinkage = 0.1,
    n.minobsinnode = 5,
    bag.fraction = .65, 
    train.fraction = 1,
    n.cores = NULL, # will use all cores by default
    verbose = FALSE
  )  
}


gbm_models <- Map(fun, mylistwithdepended_vars)

old_par <- par(mfrow = c(1, 5))
mapply(summary, gbm_models, main = names(gbm_models))

#>         B            E            C            OC           OLS         
#> var     character,17 character,17 character,17 character,17 character,17
#> rel.inf numeric,17   numeric,17   numeric,17   numeric,17   numeric,17
par(old_par)

Created on 2022-03-11 by the reprex package (v2.0.1)

The accepted solution fits a mysterious set of 5 models (and not the models the OP has in mind) because it's not clear what the predictors are in the formula Value ~. .

It's better to be explicit what the models, the response and the predictors are.

library("gbm")
#> Loaded gbm 2.1.8
library("tidyverse")

data <- tibble(new)

# Let's define a function to fit a model ...
model_fn <- function(formula, data) {
  # Generalized Boosted Regression
  gbm(
    formula = formula,
    distribution = "gaussian",
    data = data,
    n.trees = 483,
    interaction.depth = 5,
    shrinkage = 0.1,
    n.minobsinnode = 5,
    bag.fraction = .65,
    train.fraction = 1,
    n.cores = NULL, # will use all cores by default
    verbose = FALSE
  )
}
# ... as well as a function to extract information from the fitted model.
summary_fn <- function(fit) {
  # Generalized Boosted Regression
  summary(fit, plotit = FALSE) %>%
    as_tibble()
}

model_summaries <-
  data %>%
  pivot_longer(
    # We want to fit 5 models, one for each of the following responses:
    c(B, C, E, OC, OLS),
    names_to = "response",
    values_to = "y"
  ) %>%
  nest_by(response) %>%
  mutate(
    fit = list(model_fn(
      # For each response we want to use variables B1 through B11 as predictors.
      y ~ B1 + B2 + B3 + B4 + B5 + B6 + B7 + B8 + B9 + B10 + B11,
      data = data
    ))
  ) %>%
  summarise(
    summary_fn(fit)
  )
#> `summarise()` has grouped output by 'response'. You can override using the
#> `.groups` argument.
model_summaries
#> # A tibble: 55 × 3
#> # Groups:   response [5]
#>    response var    rel.inf
#>    <chr>    <chr>    <dbl>
#>  1 B        B1    1   e+ 2
#>  2 B        B2    4.11e-29
#>  3 B        B8    1.17e-29
#>  4 B        B3    8.67e-30
#>  5 B        B4    6.88e-30
#>  6 B        B11   5.43e-30
#>  7 B        B5    2.85e-30
#>  8 B        B7    6.14e-31
#>  9 B        B10   3.82e-31
#> 10 B        B6    1.84e-31
#> # … with 45 more rows

PS. The gbm models fit your data very poorly. Are you sure that the response is Gaussian? It's easy to check that each response takes two unique values, and that each value is observed exactly 50 times.

data %>%
  pivot_longer(
    # We want to fit 5 models, one for each of the following responses:
    c(B, C, E, OC, OLS),
    names_to = "response",
    values_to = "y"
  ) %>%
  count(
    response, y
  )
#> # A tibble: 10 × 3
#>    response     y     n
#>    <chr>    <int> <int>
#>  1 B           20    50
#>  2 B           50    50
#>  3 C           10    50
#>  4 C           20    50
#>  5 E           20    50
#>  6 E           30    50
#>  7 OC          10    50
#>  8 OC          30    50
#>  9 OLS          0    50
#> 10 OLS         10    50

Created on 2022-03-12 by the reprex package (v2.0.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM