Meed
Meed

Reputation: 313

How to do linear regression on a 'user-defined' formula in R?

I have a data frame with 5 independent variables and I want the linear equation to be in this form:

y = A (a + pA + qB + rC + sD + tE) 

where A, B, C, D and E are my independent variables, and p, q, r, s and t are the coefficients I need to find.

Upvotes: 2

Views: 1185

Answers (2)

AdamO
AdamO

Reputation: 4960

Use the : command between variables in the formula to obtain their multiplicative interaction without main effects. For instance:

y = A (a + pA + qB + rC + sD + tE)

I can't tell if you're trying to suppress the intercept, but I assume so since you haven't put a in the list of desired estimands.

y ~ 0 + A + A:B + A:C + A:D + A:E.

Upvotes: 1

user2005253
user2005253

Reputation:

Using the lm() command in R you could do the following:

#Pseudo Data
y = rnorm(100) 

A = rnorm(100)
B = rnorm(100)
C = rnorm(100)
D = rnorm(100)
E = rnorm(100)

AB = A*B
AC = A*C
AD = A*D
AE = A*E

model = lm(y~-1+A+AB+AC+AD+AE)

which yields:

> model

Call:
lm(formula = y ~ -1 + A + AB + AC + AD + AE)

Coefficients:
         A          AB          AC          AD          AE  
 0.1896753  -0.0835971  -0.0183475  -0.0007795  -0.0174815  

> 
> summary(model)

Call:
lm(formula = y ~ -1 + A + AB + AC + AD + AE)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.05531 -0.58641  0.08847  0.73281  2.86074 

Coefficients:
     Estimate Std. Error t value Pr(>|t|)  
A   0.1896753  0.1084157   1.750   0.0834 .
AB -0.0835971  0.1088133  -0.768   0.4442  
AC -0.0183475  0.1264781  -0.145   0.8850  
AD -0.0007795  0.0930502  -0.008   0.9933  
AE -0.0174815  0.1140712  -0.153   0.8785  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.957 on 95 degrees of freedom
Multiple R-squared:  0.03374,   Adjusted R-squared:  -0.01712 
F-statistic: 0.6634 on 5 and 95 DF,  p-value: 0.6521

Upvotes: 2

Related Questions