简体   繁体   中英

Perform linear regression in R with data from SAP HANA database

I am trying to import the dataset into R to apply linear regression model, but am skeptical of my code as am new to R. The dataset is as follows with 5000+ rows of data:

power consumption cputi dbsu

as the column names and the followings integers as their values in the above column:

132 25 654

The sql code to call R function which I wrote is

CREATE COLUMN TABLE "PREDICTIVE ANALYSIS" LIKE "ANAGAPPAN.POWER_CONSUMPTION" WITH NO DATA;

SELECT POWER_APP, POWER_DB,CPUTI,DBTI,DBSU

FROM "ANAGAPPAN.POWER_CONSUMPTION";
DROP PROCEDURE USE_LM;

CREATE PROCEDURE USE_LM( IN train "ANAGAPPAN.POWER_CONSUMPTION", OUT result "PREDICTIVE ANALYSIS")

LANGUAGE

RLANG AS

BEGIN

library(lm)

model_app <- lm( POWER_APP ~ CPUTI + DBTI + DBSU + KBYTES_TRANSFERRED, data = train )

colnames(datOut) <- c("POWER_APP", "CPUTI", "DBTI", "DBSU", "DBSU")

PREDICTIVE ANALYSIS <- as.data.frame( lm(model_App))

END;

The result I obtain is it says the procedure is created but am unable to call the linear model on the data, how would I initiate the linear model?

Although I'm not familiar with SAP products, I will have a stab at the R code I assume is between BEGIN and END; .

library(lm)

is incorrect, as mentioned by @Olli. To access R's linear model capabilities, you have to call - nothing. It's loaded by default through stats package (this may not be true if R is called in --vanilla mode.

model_app <- lm( POWER_APP ~ CPUTI + DBTI + DBSU + KBYTES_TRANSFERRED, data = train )

appears to be OK, at least from a syntax's point of view.

For

colnames(datOut) <- c("POWER_APP", "CPUTI", "DBTI", "DBSU", "DBSU")

I can't see where you define datOut . If this variable is not created by the database, it does not exist and R should complain along the lines of

Error in colnames(notExist) <- "x" : object 'notExist' not found

I will assume you want to predict (means) based on a model. Line

PREDICTIVE ANALYSIS <- as.data.frame( lm(model_App))

will not work because R's variables should not have spaces, as.data.frame will not work on a lm object and model_App doesn't exist (notice the case). I think you should do something along the lines of

# based on http://help.sap.com/hana/sap_hana_r_integration_guide_en.pdf
# you have to specify variable result which will be exported to the database
result <- as.data.frame(predict(model_app))

You can try it out.

x <- 1:10
y <- rnorm(10)

mdl <- lm(y ~ x)

as.data.frame(predict(mdl))

   predict(mdl)
1    0.47866685
2    0.34418219
3    0.20969753
4    0.07521287
5   -0.05927180
6   -0.19375646
7   -0.32824112
8   -0.46272579
9   -0.59721045
10  -0.73169511

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM