-3

orignally i have the data in the form

m n
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

using the following code i convert it to

input<-file('stdin', 'r')

mn <- read.table(input, nrows = 1, as.is = TRUE)

DF <- read.table(input, skip = 0)



m <- mn[[1]]

n <- mn[[2]]

x1<- DF[[1]]

y1<-DF[[2]]

x2<-DF[[3]]

y2<-DF[[4]]

fit1<-lm(x1 ~ poly(y1, 3, raw=TRUE))

fit2<-lm(x2 ~ poly(y2, 3, raw=TRUE))

`

m = the current datas length

n = number of points in the future to be predicted

x1=  1  5  9 13

x2= 2 6 10 14

i would like to predict all the values of x1 y1 x2 y2 for n values after the given values.

i tried to fit with lm but i am not sure how to proceed with all the values of data points to be predicted in the future missing and just getting the coefficients in terms of the other would not be sufficient as all of them need to be predicted

dagger
  • 1
  • 1
  • `px` and `py` not found. Can you provide a minimal working example? – r2evans Sep 01 '19 at 05:59
  • 3
    Please make this question *reproducible*. This includes sample code (including listing non-base R packages), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and expected output. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Sep 01 '19 at 06:00
  • @r2evans the question has been rephrased – dagger Sep 01 '19 at 06:27
  • 1
    Predict both the values and the you give 4 value??? After overwriting ‘fit’??? – IRTFM Sep 01 '19 at 06:43
  • @42- the typo has been fixed – dagger Sep 01 '19 at 08:04

1 Answers1

0

In order to get that to run without error one needs to use skip =1 on the second read.table:

mn <- read.table(text="m n
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16", nrows = 1, as.is = TRUE)

DF <- read.table(text="m n
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16", skip = 1)

m <- mn[[1]]
n <- mn[[2]]
x1<- DF[[1]]
y1<-DF[[2]]
x2<-DF[[3]]
y2<-DF[[4]]
fit1<-lm(x1 ~ poly(y1, 3, raw=TRUE))
fit2<-lm(x2 ~ poly(y2, 3, raw=TRUE))

So those input data are exactly colinear and you would NOT expect there to be any useful information in either the quadratic or cubic terms. That is in fact recognized by the lm machinery:

> fit1

Call:
lm(formula = x1 ~ poly(y1, 3, raw = TRUE))

Coefficients:
             (Intercept)  poly(y1, 3, raw = TRUE)1  poly(y1, 3, raw = TRUE)2  
                      -1                         1                         0  
poly(y1, 3, raw = TRUE)3  
                       0  

Generally one should be using the data argument

> fit3<-lm(x1 ~ poly(y1, 3, raw=TRUE), DF)
> 
> fit4<-lm(x2 ~ poly(y2, 3, raw=TRUE), DF)

But in this case it doesn't seem to matter:

> predict(fit1, newdata = list(y1=20:23))
 1  2  3  4 
19 20 21 22 
> predict(fit3, newdata = list(y1=20:23))
 1  2  3  4 
19 20 21 22 
> predict(fit2, newdata = list(y1=25:28))
 1  2  3  4 
 3  7 11 15 

The way to get predictions is to supply a newdata argument that can be coerced into a dataframe. Using a list value that has items of the same length (in this case a single argument) will succeed.

IRTFM
  • 258,963
  • 21
  • 364
  • 487