1
$\begingroup$

As you see, I have more than 500 groups of data. how can I get the std.coef in mult-linear regression for each group? Until now, I only know how to get one group by using lm.beta. For example:

library(QuantPsyc)
p <- read.csv('Data1.csv')
model1 <- lm(YCOORD110 ~ TEMMIN + TEMVAR)
lm.beta(model1) 

For groups, several codes like by, apply can be used, but seem much more complicated beyond my control.

very appreciate for your answers!

enter image description here

$\endgroup$

2 Answers 2

2
$\begingroup$

Use a for loop:

library(QuantPsyc)
attach(iris)

group <- levels(Species)
group

out03 <- vector()
for (i in group){
   out00 <- subset(iris, Species==i)
   out01 <- lm(Petal.Width ~ Petal.Length + Sepal.Width, data=out00)
   out02 <- lm.beta(out01)
   out03 <- c(out03, out02)
}

finalTable <- cbind(group, data.frame(matrix(out03, ncol=2, byrow=T)))
colnames(finalTable) <- c("Species", "Petal.Length beta", "Sepal.Width beta")
finalTable

Final tabulated output:

     Species Petal.Length beta Sepal.Width beta
1     setosa         0.2997348        0.1794891
2 versicolor         0.6043644        0.3252391
3  virginica         0.1268589        0.4868520
$\endgroup$
0
$\begingroup$

As far as I can understand the query is how to find group coefficients in a single regression model, where some independent variables may be dependent on group (M/F, let us say). In this case you need to create n dummy binary variables (for n groups) as separate independent variables. These variables will have the values 1 for all individual observations of their respective group, and 0 values for all other observations. Now these dummy variables will be multiplied with each independent variable and included in the linear regression model.

E.g. Lets say your model is: (Y = C + A.x1 + B.x2) and we have 2 groups.

We will create 2 dummy variable, z1 and z2. z1/z2 is a binary variable with value =1 for all observations in the respective group and rest values will be 0. Now input your linear regression model as: Y = C + A1.x1.z1 + A2.x1.z2 + B1.x2.z1+ B2.x2.z2

A1, A2 will respectively, the coefficients for groups 1 and 2

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.