1

My Code on R-Fiddle

I have taken a sample dataset consisting Responsibility , Trainees supervising and salary where salary can be generated from Responsibility and Trainee values , I have generated the model formula using multiple regression .

responsibility<-c(2,3,4,1,2,3)
trainee<-c(1,3,7,2,1,5)
salary<-c(7,15,29,8,7,21)

#creating the data frame to be fed into the regresion model
data1<-data.frame(responsibility,trainee,salary)


#creating the relation model between salary and responsibility & trainee
model<-lm(salary~responsibility+trainee,data=data1)

Its working fine! But When I am trying to predict Salary from available values of trainee and responsibility values .. its creating a data frame with more than one values.

intercept<-coef(model)[1]
resp<-coef(model)[2]
train<-coef(model)[3]


# r is responsibity , t is trainee value for finding the new salary...

predicted_salary=intercept+(resp*r)+(trainee*t)


print(predicted_salary)

When T=100, r=100 , predicted_salary should be 500 , but Its giving output like :

[ 1 ] 300 500 900 400 300 700

Partha Roy
  • 1,575
  • 15
  • 16
  • 1
    In your `predicted_salary` you should write `train` not `trainee`. – Marta Jan 14 '16 at 09:48
  • 1
    Use the `predict` function. – jbaums Jan 14 '16 at 09:49
  • @marta didn't get you , according to my variable i have written trainee.. – Partha Roy Jan 14 '16 at 09:56
  • @jbaums thanks i am going through that now , but i am not getting why an arithmetic operation on one integer is generating a data frame ? – Partha Roy Jan 14 '16 at 10:00
  • No, @Partha Roy, according to your variables you should write `train`, which equals `coef(model)[3]`. Your `trainee` is the initial vector (`c(1,3,7,2,1,5)`)... – Marta Jan 14 '16 at 10:01
  • You are multiplying `t` by `trainee`, which in your example is a vector of 6 numbers: `trainee<-c(1,3,7,2,1,5)`. What you should be doing is multiplying `t` by your estimated coefficient, which you assigned to `train` (`train<-coef(model)[3]`) – jbaums Jan 14 '16 at 10:01
  • @jbaums done that !! and its working ... thank you ... – Partha Roy Jan 14 '16 at 10:05

0 Answers0