Skip to content

{ Tag Archives } R

Different corr(s) of different IV scopes with same regression coef

With known in the linear relationship, can the correlation in the scatter plot of Y against X be estimated from the linear formula?
You may recall in Hierarchical Linear Model class, the scopes of the W dramatically impact the regression coefficients of F~W in the following R demo (hlm.jpg). While this time the regression [...]

Also tagged

“Effect Size” — same data, different interpretations

d<-32; ## Try d<-20 !
## to reduce the death rate by d%, From (50+d/2)% to (50-d/2)%
y<-c(rep("Live",50+d/2),rep("Death",50-d/2));
y<-c(y,rep("Live",50-d/2),rep("Death",50+d/2));
y<-(y=="Live"); ## TRUE vs FALSE
x<-c(rep("Treatment",100),rep("Control",100));
x<-(x=="Treatment");
## correlation^2
cor(x,y,method="pearson")^2
cor(x,y,method="spearman")^2
cor(x,y,method="kendall")^2
## R^2 of linear regression with norminal IV
## should we use logistic regression?
## However, R^2 is not available in GLM.
## summary(lm(y~x))
## names(summary(lm(y~x)))
summary(lm(y~x))$r.squared
## anova
## anova(lm(y~x))
## names(anova(lm(y~x)))
s<-anova(lm(y~x))$"Sum Sq";s[1]/sum(s)

Just a short R-script note to embody the 3-page-paper of [...]

Also tagged ,

Anscombe’s 4 Regressions — A Trivially Updated Demo

##----------
## This is a trivially updated version based on the R document "?anscombe".
require(stats); require(graphics)
anscombe
##-- now some "magic" to do the 4 regressions in a loop:##< -
ff = y ~ x
for(i in 1:4) {
ff[2:3] = lapply(paste(c("y","x"), i, sep=""), as.name)
assign(paste("lm.",i,sep=""), lmi <- lm(ff, data= anscombe))
}
## See how close they are (numerically!)
sapply(objects(pattern="lm\\.[1-4]$"), function(n) coef(get(n)))
lapply(objects(pattern="lm\\.[1-4]$"),
function(n) [...]

Also tagged ,

Understanding the nominal IV

n=10000;r=0.6;r_e=(1-r*r)^.5;
X=rnorm(n);
Y=X*r+r_e*rnorm(n);
Z=as.integer(X>0);
Y=Y*Z+(-Y)*(1-Z);
Z=as.factor(X>0); ## norminal IV
##Red (totally covered by Green) : Y~X
##Green: Y~X+Z Blue: Y~X+Z+X:Z
##Brown: X~Y+Z Yellow: X~Y+Z+Y:Z
plot(X,Y);
points(X,predict(lm(Y~X)),col="red");
points(X,predict(lm(Y~X+Z)),col="green");
points(X,predict(lm(Y~X+Z+X:Z)),col="blue");
points(predict(lm(X~Y+Z)),Y,col="brown");
points(predict(lm(X~Y+Z+Y:Z)),Y,col="yellow");

Also tagged

R: Simulating multiple normal distribution with any given corr matrix

For example , we have a corr matrix for five standardized factors (Hau, Chinese Textbook, pp. 49-50).
m_corr=c(1, .42, .41, .55, .42);
m_corr=cbind(m_corr, c(.42, 1,.48, .47, .46));
m_corr=cbind(m_corr, c(.41, .48,1, .48, .44));
m_corr=cbind(m_corr, c(.55, .47,.48, 1, .50));
m_corr=cbind(m_corr, c(.42, .46,.44, .50, 1));
m_corr;
## show the original corr matrix
dp=svd(m_corr);
plot(dp$d,type="o"); ## show the scree plot if there is a PC analysis
diag(dp$d); ## [...]

Also tagged ,