成分分析等时替代模型代码

算侵权吧,没有经过小姐姐同意将代码放出来。如果小姐姐可以看到,可以私聊我修改。

#	load	the	required	R	package,	Compositions	
library(Compositions)
#	make the	composition by	binding	the	components	together
comp	<- cbind(sleep,SED,LPA,MVPA)	#variables are	in	min/day
#	tell	R	that	comp is	a	compositional	variable
comp	<- acomp(comp)
#	make the	ilr	multiple	linear	regression	model.	BF	represents	%body	fat.	# ilr()	is	the	default	isometric	log	ratio	transformation	included	in	the	
#  Compositions	package.
lm	<- lm(BF	~ ilr(comp)	+ ses)	#ses, an	example	covariate
#	determine the	mean	composition
comp.mean	<- mean(comp)	
#	because	comp	has	been	designated	as	a	compositional	
# variable,	R	calls	on	mean.acomp()	to	calculate the	
# compositional	mean	(i.e.,	geometric	mean of	each	component,
# then adjusted	so	all	components	sum	to	1). The	mean	is	
# therefore	expressed	in	proportions.
#	predict %BF	for	the	mean	composition from	above,	keeping	ses	
# constant	at	its	mean.
mean.pred	<- predict(lm,	newdata=list(comp=comp.mean,	ses=mean(ses)))	
#	next, construct a	new	composition,	where	30	min of	sedentary	
#time	have	been reallocated	to	sleep. However,	here	30	min	must	
#be	expressed	as	a	proportion	(remember,	from	above,	that	
#mean.acomp()	adjusts	the	mean	to	proportions).
#	The	reallocated	time	is	therefore	30/1440.
new.comp<- acomp(comp.mean+c(30/1440,-30/1440,0,0))	
#	Now,	we	predict	%BF	for	the	new	composition (new.comp),	
#keeping	ses	constant	at	its	mean.
pred	<- predict(lm,	newdata=list(comp=new.comp,	ses=mean(ses)))
#	Finally,	the	estimated	difference	in	%BF	for the	above	time	
#reallocation	is (Equation	4):
pred- mean.pred

你可能感兴趣的:(概率论)