# Predict a numerical value using regression model

**URL:** <https://community.stardog.com/t/predict-a-numerical-value-using-regression-model/2330>\
**Category:** Support\
**Created:** [March 25, 2020, 8:59am UTC](https://community.stardog.com/t/predict-a-numerical-value-using-regression-model/2330 "2020-03-25T08:59:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![howard84527](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@howard84527](https://community.stardog.com/u/howard84527)\
**Post date:** [March 25, 2020, 8:59am UTC](https://community.stardog.com/t/predict-a-numerical-value-using-regression-model/2330/1 "2020-03-25T08:59:17Z")

</div>

Hi,  
I use regression model to train my data.The predicted results as follows:

| data | rating | predictedRating |
| --- | --- | --- |
| A | 30.45 | 29.770733 |
| B | 27.44 | 25.481995 |
| C | 26.84 | 22.389313 |
| D | 24 | 23.6501 |
| E | 20.65 | 19.64112 |

It seems a good performance. However, The results are the same when I use new data as test set to predict the value.

| data | predictedRating |
| --- | --- |
| A | 6.8025723 |
| B | 6.8025723 |
| C | 6.8025723 |
| D | 6.8025723 |
| E | 6.8025723 |

I use **set** to aggregate my entity. May I know which part is wrong?  
Thanks very much.

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [March 25, 2020, 12:26pm UTC](https://community.stardog.com/t/predict-a-numerical-value-using-regression-model/2330/2 "2020-03-25T12:26:54Z")

</div>

Can you include the commands you ran to train and produce the two tables you included?

---

<div class="post-metadata">

**Author:** ![howard84527](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@howard84527](https://community.stardog.com/u/howard84527)\
**Post date:** [April 1, 2020, 7:54am UTC](https://community.stardog.com/t/predict-a-numerical-value-using-regression-model/2330/3 "2020-04-01T07:54:34Z")

</div>

Hi whitley,  
My architecture is as follows:

```
:data :hasRating :rating;
         :has :item_1.
:item_1 :has :item_2.

```

My command for training is as follows:

```
prefix spa: <tag:stardog:api:analytics:>

INSERT {
    graph spa:model {
        :r1 a spa:RegressionModel ;
            spa:arguments (?new_item_1 ?new_item_2) ;
            spa:predict ?rating ;
            spa:crossValidation 100 ;
            spa:evaluation true ;
            spa:evaluationMetric spa:mae ;
            spa:overwrite True .
    }
}
WHERE {
    SELECT 
    (spa:set(?item_1) as ?new_item_1) 
    (spa:set(?item_2) as ?new_item_2) 
    ?rating
    {
        ?data :hasRating ?rating ;
                :has ?item_1 .
        ?item_1 :has ?item_2 .
    }
    GROUP BY ?data ?rating
}

```

I use the spa:set function to aggregate the **item\_1** and **item\_2**.  
Sorry I still don't understand the meaning of **spa:set** function after reading the manual. Can you please introduce this function?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [April 1, 2020, 1:25pm UTC](https://community.stardog.com/t/predict-a-numerical-value-using-regression-model/2330/4 "2020-04-01T13:25:47Z")

</div>

There are many things to go over here, probably too many for a single reply. First I'd suggest going back over the Stardog documentation in the [Machine Learning](https://www.stardog.com/docs/#_machine_learning) section and read over it carefully. It's not too long.

I can see how the machine learning implementation can be a bit confusing. I think it might help if you don't think if it so much as a data model where triples are added or removed from a graph but as arguments to a stateful model. I'm not even sure what you'd get if you issued something like the following query. I might have to give it a try and see.

```
select * where {
    graph spa:model {
      ?s ?p ?o .
    }
}

```

I would also go back and reexamine your data that you're modeling. I'm not quite sure what the data you have represents or what you're trying to predict.

> [@howard84527](#):
>
> :data :hasRating :rating; :has :item\_1. :item\_1 :has :item\_2.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [April 15, 2020, 1:25pm UTC](https://community.stardog.com/t/predict-a-numerical-value-using-regression-model/2330/5 "2020-04-15T13:25:51Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
