File: preplot.c

package info (click to toggle)
cufflinks 1.3.0-2
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 3,864 kB
  • sloc: cpp: 48,999; ansic: 12,297; sh: 3,381; python: 432; makefile: 209
file content (222 lines) | stat: -rw-r--r-- 5,394 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 *   Copyright (c) 1996-2001 Lucent Technologies.
 *   See README file for details.
 */

#include "local.h"

/*
 preplot():  interpolates the fit to a new set of points.
 lf  -- the fit structure.
 des -- design structure; really only needed for parfit.
 x   -- the points to predict at.
 f   -- vector to return the predictions.
 se  -- vector to return std errors (NULL if not req'd)
 band-- char for conf band type. ('n'=none, 'g'=global etc.)
 n   -- no of predictions (or vector of margin lengths for grid)
 where -- where to predict:
 1 = points in the array x.
 2 = grid defined by margins in x.
 3 = data points from lf (ignore x).
 4 = fit points from lf (ignore x).
 what -- what to predict.
 (PCOEF etc; see lfcons.h file)
 
 cpreplot(): C version front end.
 setpppoints(): (C version) used to set preplot points.
 */

static char cb;
double *sef, *fit, sigmahat;

void predptall(lf,des,x,what,ev,i)
lfit *lf;
design *des;
double *x;
INT what, ev, i;
{ double lik, rdf;
    fit[i] = dointpoint(lf,des,x,what,ev,i);
    if (cb=='n') return;
    sef[i] = dointpoint(lf,des,x,PNLX,ev,i);
    if (cb=='g')
    { sef[i] *= sigmahat;
        return;
    }
    if (cb=='l')
    { lik = dointpoint(lf,des,x,PLIK,ev,i);
        rdf = dointpoint(lf,des,x,PRDF,ev,i);
        sef[i] *= sqrt(-2*lik/rdf);
        return;
    }
    if (cb=='p')
    { sef[i] = sigmahat*sqrt(1+sef[i]*sef[i]);
        return;
    }
}

void prepvector(lf,des,x,n,what) /* interpolate a vector */
lfit *lf;
design *des;
double **x;
INT n, what;
{ INT i, j;
    double xx[MXDIM];
    for (i=0; i<n; i++)
    { for (j=0; j<lf->mi[MDIM]; j++) xx[j] = x[j][i];
        predptall(lf,des,xx,what,lf->mi[MEV],i);
        if (lf_error) return;
    }
}

void prepfitp(lf,des,what)
lfit *lf;
design *des;
INT what;
{ 
    INT i;
    //d = lf->mi[MDIM];
    for (i=0; i<lf->nv; i++)
    { predptall(lf,des,evpt(lf,i),what,EFITP,i);
        if (lf_error) return;
    }
}

void prepgrid(lf,des,x,mg,n,what) /* interpolate a grid given margins */
design *des;
lfit *lf;
double **x;
INT *mg, n, what;
{ INT i, ii, j, d;
    double xv[MXDIM];
    d = lf->mi[MDIM];
    for (i=0; i<n; i++)
    { ii = i;
        for (j=0; j<d; j++)
        { xv[j] = x[j][ii%mg[j]];
            ii /= mg[j];
        }
        predptall(lf,des,xv,what,lf->mi[MEV],i);
        if (lf_error) return;
    }
}

void preplot(lf,des,x,f,se,band,mg,where,what)
lfit *lf;
design *des;
double **x, *f, *se;
INT *mg, where, what;
char band;
{ INT d = 0, i, n;
    double *xx[MXDIM];
    d = lf->mi[MDIM];
    fit = f;
    sef = se;
    cb = band;
    if (cb!='n') sigmahat = sqrt(lf->dp[DRV]);
    
    switch(where)
    { case 1: /* vector */
            n = mg[0];
            prepvector(lf,des,x,n,what);
            break;
        case 2: /* grid */
            n = 1;
            for (i=0; i<d; i++) n *= mg[i];
            prepgrid(lf,des,x,mg,n,what);
            break;
        case 3: /* data */
            n = lf->mi[MN];
            if ((lf->mi[MEV]==EDATA) | (lf->mi[MEV]==ECROS))
                prepfitp(lf,des,what);
            else
            { for (i=0; i<d; i++) xx[i] = dvari(lf,i);
                prepvector(lf,des,xx,n,what);
            }
            break;
        case 4: /* fit points */
            n = lf->nv;
            prepfitp(lf,des,what);
            break;
        default:
            ERROR(("unknown where in preplot"));
            return;
    }
    
    if ((what==PT0)|(what==PVARI))
        for (i=0; i<n; i++) f[i] = f[i]*f[i];
}

#ifdef CVERSION
extern lfit lf;
extern design des;

void cpreplot(pp,vc,band)
pplot *pp;
vari *vc;
char band;
{ double *data[MXDIM];
    INT j, mg[MXDIM];
    for (j=0; j<pp->d; j++)
    { data[j] = vdptr(pp->data[j]);
        mg[j] = pp->data[j]->n;
    }
    j = getarg(vc,"what",0);
    pp->wh = (j>0) ? ppwhat(argval(vc,j)) : PCOEF;
    
    preplot(&lf,&des,data,vdptr(pp->fit),vdptr(pp->se),band,mg,pp->gr,pp->wh);
}

INT setpppoints(pp,where,mg,xl)
pplot *pp;
char *where;
INT *mg;
double *xl;
{ INT d, i, j, n, m;
    varname vn;
    d = pp->d = lf.mi[MDIM];
    if (strcmp(where,"fitp")==0)
    { n = lf.nv;
        for (j=0; j<d; j++)
        { sprintf(vn,"_pred%d",j);
            pp->data[j] = createvar(vn,STPLOTVAR,n,VDOUBLE);
            if (lf_error) return(0);
            for (i=0; i<n; i++)
                vassn(pp->data[j],i,evptx(&lf,i,j));
        }
        pp->gr = 4;
        return(n);
    }
    if (strcmp(where,"data")==0)
    { recondat(1,&n);
        for (j=0; j<d; j++)
        { sprintf(vn,"_pred%d",j);
            pp->data[j] = createvar(vn,STPLOTVAR,n,VDOUBLE);
            if (lf_error) return(0);
            for (i=0; i<n; i++)
                vassn(pp->data[j],i,datum(&lf,j,i));
        }
        pp->gr = 3;
        return(n);
    }
    if (strcmp(where,"grid")==0)
    { n = 1;
        for (j=0; j<d; j++)
        { sprintf(vn,"_pred%d",j);
            m = (mg==NULL) ? 40 : mg[j];
            pp->data[j] = createvar(vn,STPLOTVAR,m,VDOUBLE);
            if (lf_error) return(0);
            if (m==1)
                vassn(pp->data[j],0,(xl[d+j]+xl[j])/2);
            else
                for (i=0; i<m; i++)
                    vassn(pp->data[j],i,xl[j]+i*(xl[d+j]-xl[j])/(m-1));
            n *= m;
            pp->gr = 2;
        }
        return(n);
    }
    ERROR(("setpppoints: invalid where=%s",where));
    return(0);
}

#endif