Set One or More of Coefficients to a Specific Integer

Set one or more of coefficients to a specific integer

You can use the offset term in the formula and include the desired coefficient and variable therein:

df<-data.frame(aa=1:6,bb=2:7,cc=c(4,2,7,5,8,3))

lm(cc ~ aa + offset(647*bb), data = df)

So this is regressing cc on aa plus the fixed term bb * 647. For more than one given coefficient, add the appropriate additional offset() terms.

How to manually set coefficients for variables in linear model?

The following code is a bit complicated because lm() minimizes residual sum of squares and with a fixed, non optimal coefficient it is no longed minimal, so that would be against what lm() is trying to do and the only way is to fix all the rest coefficients too.

To do that, we have to know coefficients of the unrestricted model first. All the adjustments have to be done by changing formula of your model, e.g. we have
price ~ memory + screen_size, and of course there is a hidden intercept. Now neither changing the data directly nor using I(c*memory) is good idea. I(c*memory) is like temporary change of data too, but to change only one coefficient by transforming the variables would be much more difficult.

So first we change price ~ memory + screen_size to price ~ offset(c1*memory) + offset(c2*screen_size). But we haven't modified the intercept, which now would try to minimize residual sum of squares and possibly become different than in original model. The final step is to remove the intercept and to add a new, fake variable, i.e. which has the same number of observations as other variables:

price ~ offset(c1*memory) + offset(c2*screen_size) + rep(c0, length(memory)) - 1

# Function to fix coefficients
setCoeffs <- function(frml, weights, len){
el <- paste0("offset(", weights[-1], "*",
unlist(strsplit(as.character(frml)[-(1:2)], " +\\+ +")), ")")
el <- c(paste0("offset(rep(", weights[1], ",", len, "))"), el)
as.formula(paste(as.character(frml)[2], "~",
paste(el, collapse = " + "), " + -1"))
}
# Example data
df <- data.frame(x1 = rnorm(10), x2 = rnorm(10, sd = 5),
y = rnorm(10, mean = 3, sd = 10))
# Writing formula explicitly
frml <- y ~ x1 + x2
# Basic model
mod <- lm(frml, data = df)
# Prime coefficients and any modifications. Note that "weights" contains
# intercept value too
weights <- mod$coef
# Setting coefficient of x1. All the rest remain the same
weights[2] <- 3
# Final model
mod2 <- update(mod, setCoeffs(frml, weights, nrow(df)))
# It is fine that mod2 returns "No coefficients"

Also, probably you are going to use mod2 only for forecasting (actually I don't know where else it could be used now) so that could be made in a simpler way, without setCoeffs:

# Data for forecasting with e.g. price unknown
df2 <- data.frame(x1 = rpois(10, 10), x2 = rpois(5, 5), y = NA)
mat <- model.matrix(frml, model.frame(frml, df2, na.action = NULL))
# Forecasts
rowSums(t(t(mat) * weights))

How do I parse multiple values

You can leverage the .split() method like so:

String[] roots = input.nextLine().split();
int a=Integer.parseInt(roots[0]);
int b=Integer.parseInt(roots[1]);
int c=Integer.parseInt(roots[2]);

The .split() method can split a String into multiple parts. By default, it splits spaces, so "1 2 3" would become "1", "2" and "3". We can then parse them using Integer.parseInt(), with roots[0], roots[1], roots[2] being the position in the array for 1, 2 and 3 respectively.

Or you can use .nextInt() like so:

int a=input.nextInt();
int b=input.nextInt();
int c=input.nextInt();

You are getting the error because you are tring to parse a number with spaces (which is not a number). Either strip out the spaces or split them into multiple numbers.

constructor for a polynomial class that must initialize the coefficients although the degree is unknown

I suggest creating a struct Term:

struct Term
{
int coefficient;
int power;
};

A polynomial, by definition is a container (or sum) of terms:

class Polynomial
{
public:
std::vector<Term> terms;
Polynomial(int coef1, int constant1);
}

In the above class, the constructor will create two terms:

Polynomial::Polynomial(int coef1, int constant1)
{
Term t1;
Term c;
t1.coefficient = coef1;
t1.power = 1;
c.coefficient = constant1;
c.power = 0;
terms.push_back(c);
terms.push_back(t1);
}

The next constructor, in your requirements, creates 3 terms:

Polynomial::Polynomial(int coef1, int coef2, int constant1)
{
Term t1 = {coef1, 2};
Term t2 = {coef2, 1};
Term constant_term = {constant1, 0};
terms.push_back(constant_term);
terms.push_back(t2);
terms.push_back(t1);
}

One of the theorems of addition is that the terms can be in any order. You can change the order that you append them to the container so you print them in common order (highest exponent term first).

Array of Coefficients

In the requirements, there is double * coef which is supposed to be an array of coefficients (one for each term).

Here's one example of a constructor:

Polynomial::Polynomial(double coef1, double constant)
{
degree = 1;
coef = new double[2]; // 2 terms.
coef[0] = coef1;
coef[1] = constant;
}

The other constructors are similar to the above one.

Remember, your destructor should contain delete[] coef;.

Summation up to a variable integer: How to get the coefficients?

The coefficient by a^k can be viewed as derivative of order k at zero divided by k!. In version 8, there is a function BellY, which allows to construct a derivative at a point for composition of functions, out of derivatives of individual components. Basically, for f[g[x]] and expanding around x==0 we find Derivative[p][Function[x,f[g[x]]][0] as

BellY[ Table[ { Derivative[k][f][g[0]], Derivative[k][g][0]}, {k, 1, p} ] ]/p!

This is also known as generalized Bell polynomial, see wiki.

In the case at hand:

f[a_, n_Integer, m_Integer] := Sum[a^i k[i], {i, 0, n}]^m

With[{n = 3, m = 4, p = 7},
BellY[ Table[{FactorialPower[m, s] k[0]^(m - s),
If[s <= n, s! k[s], 0]}, {s, 1, p}]]/p!] // Distribute

(*
Out[80]= 4 k[1] k[2]^3 + 12 k[1]^2 k[2] k[3] + 12 k[0] k[2]^2 k[3] +
12 k[0] k[1] k[3]^2
*)

With[{n = 3, m = 4, p = 7}, Coefficient[f[a, n, m], a, p]]

(*
Out[81]= 4 k[1] k[2]^3 + 12 k[1]^2 k[2] k[3] + 12 k[0] k[2]^2 k[3] +
12 k[0] k[1] k[3]^2
*)

Doing it this way is more computationally efficient than building the entire expression and extracting coefficients.


EDIT The approach here outlined will work for symbolic orders n and m, but requires explicit value for p. When using it is this circumstances, it is better to replace If with its Piecewise analog, e.g. Boole:

With[{p = 2}, 
BellY[Table[{FactorialPower[m, s] k[0]^(m - s),
Boole[s <= n] s! k[s]}, {s, 1, p}]]/p!]

(* 1/2 (Boole[1 <= n]^2 FactorialPower[m, 2] k[0]^(-2 + m)
k[1]^2 + 2 m Boole[2 <= n] k[0]^(-1 + m) k[2]) *)

Coefficients Reduction in Linear Programming lead to incoherent results

If you are referring to the technique in 4.4.3, then it's clear what's the problem here.

Suppose we are given a constraint of the form
a1*y1+ a2*y2 + ... + ai*yi < b
where yi = 0 or 1

You are not allowed to use this technique, as your coefficients are continuous ( in [1,2]) and not binary as needed here!

how to make specific polynomials in a set with python

It is rather unclear what sort of objects are in R, but you seemto know how to manipulate them...

There is one error that may be causing you trouble:

S = S.add(poly)

is first adding poly to S, then assigning None to S, which is rather unfortunate.

try to replace it with:

S.add(poly)

which accumulates distinct objects into S

Linear fit with a previously known slope

Responding to @jbssm comment, @BenBolker solution seems to give very reasonable results:

vCenter_slope <- 600.7472
flN <- transform(flN,w=1/as.numeric(timeError_4_8)^2)
# slope fixed
m1 <- lm(as.numeric(arrivalTime_4_8) ~ 1 + offset(vCenter*vCenter_slope), data = flN, weights=w)
# slope chosen by lm(...)
m0 <- lm(as.numeric(arrivalTime_4_8) ~ vCenter, data=flN, weights=w)

library(ggplot2)
ggp <- ggplot(flN)
ggp <- ggp + geom_point(aes(x=vCenter,y=arrivalTime_4_8))
ggp <- ggp + geom_errorbar(aes(x=vCenter, ymin=arrivalTime_4_8-timeError_4_8,ymax=arrivalTime_4_8+timeError_4_8),width=0)
ggp <- ggp + geom_errorbarh(aes(x=vCenter, y=arrivalTime_4_8, xmax=vHigh, xmin=vLow),height=0)
ggp <- ggp + geom_abline(slope=600.7472, intercept=coef(m1)[1])
ggp <- ggp + geom_abline(slope=coef(m0)[2], intercept=coef(m0)[1],color="red")
ggp

Sample Image

The red line allows lm(...) to set the slope, the black line uses fixed slope. Note that the black line is further away from the first two vCenter points than you might expect, because you're weighting inversely to error in vCenter. If this is your whole dataset, then using weights is highly questionable.

Finally, you can read up on "errors-in-variables" models here and here. After reading these, you might want to look at the MethComp package, which supports `Deming Regression' (one type of errors-in-variables regression).



Related Topics



Leave a reply



Submit