0

I am trying to solve this question but my result is wrong, anyone can help me? regression predicting calories from saturated fat, fiber, and sugar. Based on standardized regression coefficients, identify the strongest predictor.
and at the end assign the unstandardized regression coefficient of the strongest predictor to a variable

Note: Identifying the strongest predictor in a regression predicting calories from saturated fat, fiber, and sugar

Here is my code:

a <- lm(calories ~ sat_fat + fiber + sugar, data = fastfood)

# standardize
a.beta <- lm.beta(a)

print(a.beta)

summary(a.beta)

coef(a.beta)

Result:

Coefficients:
            Estimate Standardized Std. Error t value Pr(>|t|)    
(Intercept)  75.0383           NA    35.9388   2.088  0.03850 *  
sat_fat      44.4337       0.7073     2.9552  15.036  < 2e-16 ***
fiber        13.7060       0.1282     5.2472   2.612  0.00992 ** 
sugar         9.1996       0.2494     1.8230   5.047 1.29e-06 ***

The sat_fat result should be 30.84 but somehow I am getting this result! I am not sure what part of my code is wrong, anyone can help?

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25
Night
  • 1

1 Answers1

0

Welcome to Stack Overflow. In general, when asking questions, please see How to make a great R reproducible example. In particular, note the advice to add which packages you use if you use any. It will be much quicker to get responses if others are able to reproduce your data. In any event, I realised that your fastfood data is in the openintro package.

The reason you need to standardise is because the independent variables are on different scales. There are many ways to manage this, such as scaling your data, but they're off-topic for Stack Overflow (though on topic for Cross Validated). In this case, we can just use the t value, which divides the regression coefficient by the standard error.

my_lm <- lm(calories ~ sat_fat + fiber + sugar, data = openintro::fastfood)

model_coef <- summary(my_lm)$coefficients

t_values <- model_coef[, "t value"]
max_coef <- names(t_values[which.max(t_values)]) # sat_fat

max_coef_value <- model_coef[max_coef, "Estimate"]
max_coef_value # 30.84

This is the result you were trying to replicate. See also How to understand t-value in R's lm()?.

SamR
  • 8,826
  • 3
  • 11
  • 33