1

I have a variable yhat which is the output of predict function, and assign it to a dataframe:

x=matrix(rnorm(100*20),100,20)
y=rnorm(100)

cv.fit = cv.glmnet(x, y)
x=matrix(rnorm(100*20),100,20)
yhat = predict(cv.fit, s=cv.fit$lambda.1se, newx=x, type="response")
df = data.frame(ypred = yhat, ytest = y)
head(df)

      X1          y
1 0.06816674  0.1609813
2 0.06816674  1.4414283
3 0.06816674  0.4674309
4 0.06816674  0.3306559
5 0.06816674  0.4578457
6 0.06816674 -0.3337405

The name of the first column is X1 rather than ypred. If I put yhat in the 2nd column, then the 2nd column name is X1. Is this a bug?

df = data.frame(y_test = y, y_pred = yhat)
head(df)
  y_test         X1
1  0.1609813 0.06816674
2  1.4414283 0.06816674
3  0.4674309 0.06816674
4  0.3306559 0.06816674
5  0.4578457 0.06816674
6 -0.3337405 0.06816674

and it works if I change yhat to vector with:

df = data.frame(y_test = y, y_pred = as.vector(yhat))
head(df)
  y_test     y_pred
1  0.1609813 0.06816674
2  1.4414283 0.06816674
3  0.4674309 0.06816674
aynber
  • 22,380
  • 8
  • 50
  • 63
Xin Niu
  • 533
  • 1
  • 5
  • 15
  • could you add your data using `dput(df)` and what you used to make the `cv.fit`? – NelsonGon Dec 17 '18 at 02:36
  • Also add the original dataset's `head` output. – NelsonGon Dec 17 '18 at 02:37
  • What happens if you use DF$yhat = predict(cv.fit, s=cv.fit$lambda.1se, newx=x.test.sort, type="class") – Prasanna S Dec 17 '18 at 02:44
  • if i use DF$yhat = predict(cv.fit, s=cv.fit$lambda.1se, newx=x.test.sort, type="class") , the column name would be 1. It seems the output of predict has its own column name and cannot be redefined when you assign it in a dataframe. – Xin Niu Mar 05 '19 at 00:05

0 Answers0